服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
1. 集中管理配置文件
2. 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod
3. 运行期间动态调整配置,不再需要在每个服务器部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
4. 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
5. 将配置信息以REST接口的形式暴露

Config原理图
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>
server:port: 3344spring:application:name: cloud-config-centercloud:config:server:git:# 这个就是你平时clone项目的地址,https和ssh地址都可以uri: https://github.com/Lmg12580/springcloud.git# 这个地址是进入仓库后配置文件的目录地址search-paths:- configlabel: master #git分支eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://eureka8761.com:8761/eureka
@SpringBootApplication@EnableConfigServerpublic class ConfigCenterApplication {public static void main(String[] args) {SpringApplication.run(ConfigCenterApplication.class, args);}}
5. 我把配置文件放到我仓库的config目录下了,具体的内容可以在github上查看,大概就是下面这样的内容格式
config:info: "master branch,springcloud-config/config-dev.yml version=1"

1. /{application}/{profile}[/{label}]
对应: http://localhost:3344/config/dev/master
这个返回的结果是一个json串
2. /{application}-{profile}.yml
对应: http://localhost:3344/config-dev.yml
如果没有输入分支,默认取配置文件中的label配置
3. /{label}/{application}-{profile}.yml
对应: http://localhost:3344/master/config-dev.yml
二、Config客户端配置
工程名:cloud-config-client
2. pom文件添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency>
3. 新建bootstrap.yml文件,知识点:
application.yml是用户级的资源配置项
因为bootstrap有高优先级,比application.yml先加载,且不会被本地配置覆盖,保证了配置的分离。springboot基础知识,不多聊了。
server:port: 3355spring:application:name: cloud-config-clientcloud:config: # config客户端配置label: master # 分支名称name: config # 配置文件名称profile: dev # 读取后缀名称 综合:master分支上的config-dev.yml配置文件uri: http://localhost:3344 #配置中心地址 合起来就是http://localhost:3344/master/config-dev.ymleureka:client:service-url:defaultZone: http://eureka8761.com:8761/eureka
4. 新增业务类,用来获取服务端配置信息
@RestControllerpublic class ConfigClientController {@Value("${config.info}")private String configInfo;@GetMapping("/configInfo")public String getConfigInfo(){return configInfo;}}
5. 启动客户端3355,在地址栏请求访问配置文件,发现这时候我们可以读取到配置信息

6. 我们修改一下github的该配置文件,将版本号改为2,看3355返回的信息
config:info: "master branch,springcloud-config/config-dev.yml version=2"
7. 在访问3355之前,先看一下服务端3344的返回信息,发现已经取到了最新的配置

8. 这时候再看一下3355的返回信息,刷新后仍然是原先的配置

问题:分布式配置的动态刷新问题
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
management:endpoints:web:exposure:include: "*"
@RefreshScope
config:info: "master branch,springcloud-config/config-dev.yml version=3"
5. 请求访问3355,发现修改还是没有生效

Lmg12580:~ wangyg$ curl -X POST "http://localhost:3355/actuator/refresh"





