config
现在看起来已经很完善了貌似,服务注册到eureka上,通过feign完成远程调用,ribbon负责负载均衡,zuul实现路由规则的制定,但是现在还有一个问题,当我们系统足够大,服务划分颗粒度非常细化的时候,每个服务都有自己的配置文件,那么配置文件管理起来就非常麻烦,因此config就出现了,我们可以通过config把配置文件统一管理,这样管理起来就非常方便了。
config统一管理配置文件一般存放在github svn 本地这3中。下面会分别介绍。
存放到github
环境配置
- demo_configuration 添加依赖
1 <!-- config server -->
2 <dependency>
3 <groupId>org.springframework.cloud</groupId>
4 <artifactId>spring-cloud-config-server</artifactId>
5 </dependency>
- demo_config yml文件
1server:
2 port: 10001
3
4spring:
5 application:
6 name: config
7 cloud:
8 config:
9 server:
10 git:
11 uri: https://github.com/lytw13/springCloud
12 search-paths: config-repo
13 username: #这里我github仓库是公共的,可以留空
14 password: #这里我github仓库是公共的,可以留空
15
16info:
17 name: lytw13
18 web: lytw13.top
19
20eureka:
21 client:
22 service-url:
23 defaultZone: http://localhost:6001/eureka/
24 fetch-registry: true
25 register-with-eureka: true
26 instance:
27 instance-id: config01
28 ip-address: true
上面的配置主要就是指定了git的地址,让系统知道具体从哪儿读取。
- 我们从github上读取文件,首先我们需要建一个啊对吧。这里我们以web项目的配置文件为例,将其写到git上,看能否读到配置文件并启动。
(1) git上web.yml文件
1spring:
2 profiles:
3 active: dev
4---
5uname: lytw13
6spring:
7 profiles: dev
8
9server:
10 port: 8080
11
12eureka:
13 client:
14 fetch-registry: true
15 register-with-eureka: true
16 service-url:
17 defaultZone: http://localhost:6001/eureka
18 #defaultZone: http://eureka01.com:6001/eureka/,http://eureka02.com:6002/eureka/ ,http://eureka03.com:6003/eureka/
19 instance:
20 hostname: localhost
21 ip-address: true
22 instance-id: web
23
24feign:
25 hystrix:
26 enabled: true
27---
28uname: lytw1315
29spring:
30 profiles: test
31
32server:
33 port: 8080
34
35eureka:
36 client:
37 fetch-registry: true
38 register-with-eureka: true
39 service-url:
40 defaultZone: http://localhost:6001/eureka
41 #defaultZone: http://eureka01.com:6001/eureka/,http://eureka02.com:6002/eureka/ ,http://eureka03.com:6003/eureka/
42 instance:
43 hostname: localhost
44 ip-address: true
45 instance-id: web01
46
47feign:
48 hystrix:
49 enabled: true
这个文件如果用properties文件写,得写2个才能表示,所以这里用的yml,现在用yml的还是比较多的。
(2) 重构web项目
- 添加依赖
1 <!--config client -->
2 <dependency>
3 <groupId>org.springframework.cloud</groupId>
4 <artifactId>spring-cloud-starter-config</artifactId>
5 </dependency>
- 首先web项目中配置文件已经放到git上了,所以目前配置文件如下:
1spring:
2 application:
3 name: web
- bootstrap.yml
1spring:
2 cloud:
3 config:
4 name: web #github的资源名称,没有yml后缀名
5 profile: dev
6 uri: http://localhost:10001 #SpringCloudConfig获取的服务地址
上面的文件指定了config的地址,表示我们得通过它来读取配置文件。
现在启动主启动类就成了,需要加上@ConfigServer注解,表示这是一个config服务端。
- 测试:
现在只是可以通过config能访问了,我们还需要我们的web项目还能正常访问user服务么。
这里可能会报这个com.netflix.zuul.exception.ZuulException: Hystrix Readed time out错误,我们修改zuul配置文件中将zuul的超时时间设置长一些就好。
1zuul:
2 host:
3 connect-timeout-millis: 50000
4 socket-timeout-millis: 50000
设置完成后,最好都重启下相关的服务,要不可能加载不到修改的文件,重启后就可以访问到了。
还有其他的访问方式同样能访问到数据:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
这里就不测试了,可以自己试试都。
读取config上的配置文件大概结构如下: