微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer【配置中心】来解决这个问题,我们每一个微服务自己都带着一个application.yml,上百个配置文件的管理就要管理上百个application.yml

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置
官网: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式。
SpringCloud Config分为服务端和客户端两部分。
https://gitee.com/jm1107/springcloud-config.git

在D:\workspace\SpringCloud2022\springcloud-config创建自己的配置文件.
该文件可以从周阳老师的github仓库克隆一份,复制到自己的仓库 https://github.com/zzyybs/springcloud-config
模块名:cloud-config-center-3344,它即为Cloud的配置中心模块CloudConfig Center
cloud2022 com.jm.springcloud 1.0-SNAPSHOT 4.0.0 cloud-config-center-3344 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
server:port: 3344spring:application:name: cloud-config-center #注册进Eureka服务器的微服务名cloud:config:server:git:uri: https://gitee.com/jm1107/springcloud-config.git #Gitee上面的git仓库名字####搜索目录search-paths:- springcloud-config####读取分支label: master#服务注册到eureka地址
eureka:client:service-url:defaultZone: http://localhost:7001/eureka
package com.jm.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain3344 {public static void main(String[] args) {SpringApplication.run(ConfigCenterMain3344.class, args);}
}

1) 读取master分支
2)读取dev分支
没有了分支/那一项,是因为在application.yml中配置了 label: master
所以会先去读取master的 (就算不配label 也会先去master寻找)
这样读取的是 JSON串
cloud2022 com.jm.springcloud 1.0-SNAPSHOT 4.0.0 cloud-config-client-3355 org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高
Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。
初始化的时候,BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml。
server:port: 3355spring:application:name: config-clientcloud:# config 配置config:label: master #分支名称name: config # 配置文件名称profile: prod # 读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://localhost:3344/master/config-dev.ymluri: http://localhost:3344 # 配置中心地址
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka

package com.jm.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {public static void main(String[] args) {SpringApplication.run(ConfigClientMain3355.class, args);}
}
@Value("${config.info}')是从git上的配置文件中获取到的
@RestController
@RefreshScope
public class ConfigClientController {@Value("${config.info}")private String configInfo;@GetMapping("/configInfo")public String getConfigInfo(){return configInfo;}
}
启动Config配置中心3344微服务并自测
启动3355作为Client准备访问
避免每次更新配置都要重启客户端微服务3355
org.springframework.boot spring-boot-starter-actuator
# 暴露监控端点
management:endpoints:web:exposure:include: "*"
@RestController
@RefreshScope//<------------
public class ConfigClientController {@Value("${config.info}")private String configInfo;@GetMapping("/configInfo")public String getConfigInfo(){return configInfo;}
}
此时修改gitee配置文件内容 -> 访问3344 -> 访问3355
访问 http://localhost:3355/configInfo,发现并没有修改,需要运维人员发送Post请求刷新3355
curl -X POST "http://localhost:3355/actuator/refresh"
再次访问http://localhost:3355/configInfo,发现配置已经需改了,成功实现了客户端3355刷新到最新配置内容,避免了服务重启。
但动态手动刷新存在多个问题:
上一篇:晚春诗句的意思