简介:SpringCloud Config 为微服务架构中心的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供了一个中心化的外部配置;
怎么玩:
SpringCloud Config分为服务端和客户端两部分。
服务端也成为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口;
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务默认采用git来存储配置信息,这样就有助于对环境进行版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容;
服务端:
pom:
<dependency>
<groupId>org.springframework.cloud<groupId>
<artifactId>spring-cloud-config-server<artifacId>
<dependency>
yml:
server:
port:8080
spring:
application:
name:cloud-config-center
cloud:
server:
git:
uri:git@github.com:aaa/springcloud-config.git
search-paths:
- springcloud-config
label:master #读取分支
eureka:
client:
service-url:
defaultZone:http://localhost:7001/eureka
启动类:
@EnableConfigServer
windows修改hosts文件,增加映射:127.0.0.1:config-8080.com
测试:http://config-8080.com:8080/master/config-dev.yml
客户端配置:
pom:
<dependency>
<groupId>org.springframework.cloud<groupId>
<artifactId>spring-cloud-starter-config<artifactId>
<dependency>
yml:
server:
port:8081
spring:
application:
name:config-client
cloud:
#客户端配置
config:
label:master #分支名称
name:config #配置文件名称
profile:dev #读取环境名称
uri:http://localhost:8080 #配置中心地址
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone:http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
Config动态刷新
pom:
<dependency>
<groupId>org.springframework.boot<groupId>
<artifactId>spring-boot-starter-actuator<artifactId>
<dependency>
yml:
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
@RefreshScope 业务类Controller的修改
@RestController
@RefreshScope
public class ConfigClientController{
@Value(“${config.info}”)
private String configInfo;
@GeetMapping("/configInfo")
public String getConfigInfo()
{
return configInfo;
}
}