Spring Cloud Config为所有微服务提供一个统一的外部配置的地方。
可以将Spring Cloud Config理解成一个微服务ConfigServce,也是要注册到注册中心euraka中,同时他要与github整合,将配置文件存放到github中,ConfigServce到GitHubs进行拉取,而其他的微服务端到ConfigService上进行获取配置。【ConfigServce也可以用本地作为配置文件的存储仓库,即本项目的property文件】
接下来是怎么配置出这个ConfigServce服务
1、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、application.yml【一个配置是和git适配的配置、一个是注册到euraka的配置】
server:
port: 3344
spring:
application:
name: config-center
cloud:
config:
server:
git:
#仓库地址
uri: https://github.com/xzjyy/springcloud-config.git
search-paths:
- springcloud-config #要扫描的文件
username: xzjyy #github的用户名
#gitHub的令牌
password: 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
label: master #分支
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。
#单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
3、启动类上增加一个配置中心服务的注解
@EnableConfigServer
4、启动服务,可以用get的请求方式,请求对应配置文件
http://{configService.IP}:{port}http://{configService.IP}:
5、其他服务端增加一个BootStrap.yml文件-读取配置中心信息的配置文件
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
#分支名称
label: master
name: config #配置文件名称
profile: dev #读取后缀名称
uri: http://localhost:3344 #配置中心地址k
#上述3个综合:master分支上config-dev.yml的配置文件被读取
#http://config-3344.com:3344/master/config-dev.yml
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7002.com:7002/eureka/