SYSTEM
服务配置中心
src/content/notes/system/服务配置.md 服务配置
config
基本介绍
SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持(Git/GitHub),为各个不同微服务应用的所有环境提供了一个中心化的外部配置(Config Server)

SpringCloud Config 分为服务端和客户端两部分
- 服务端也称为分布式配置中心,是一个独立的微服务应用,连接配置服务器并为客户端提供获取配置信息,加密/解密等信息访问接口
- 客户端通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动时从配置中心获取和加载配置信息,配置服务器默认采用 Git 来存储配置信息,这样既有助于对环境配置进行版本管理,也可以通过 Git 客户端来方便的管理和访问配置内容
优点:
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署比如 dev/test/prod/beta/release
- 运行期间动态调整配置,服务向配置中心统一拉取配置的信息,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以 Rest 接口的形式暴露
官网: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
服务端
构建 Config Server 模块
-
引入 pom 依赖:
<!--springCloud Config Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> -
application.yml:
server: port: 3344 spring: application: name: cloud-config-center #注册进Eureka服务器的微服务名 cloud: config: server: git: # GitHub上面的git仓库名字 这里可以写https地址跟ssh地址,https地址需要配置username和 password uri: [邮箱已隐藏]:seazean/springcloud-config.git default-label: main search-paths: - springcloud-config # 搜索目录 # username: # password: label: main # 读取分支,以前是master #服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka #集群版search-paths 表示远程仓库下有一个叫做 springcloud-config 的,label 则表示读取 main分支里面的内容
-
主启动类:
@SpringBootApplication @EnableEurekaClient @EnableConfigServer //开启SpringCloud的 public class ConfigCenterMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344.class, args); } }
配置读取规则:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
- label:分支
- name:服务名
- profile:环境(dev/test/prod)
比如:http://localhost:3344/master/config-dev.yaml
客户端
基本配置
配置客户端 Config Client,客户端从配置中心(Config Server)获取配置信息
-
引入 pom 依赖:
<!--这里就是客户端的SpringCloud config 因为是客户端所以没有server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> -
bootstrap.yml:系统级配置,优先级更高,application.yml 是用户级的资源配置项
Spring Cloud 会创建一个 Bootstrap Context 作为 Spring 应用的 Application Context 的父上下文,初始化的时候 Bootstrap Context 负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的 Environment,为了配置文件的加载顺序和分级管理,这里使用 bootstrap.yml
server: port: 3355 # 构建多个微服务,3366 3377 等 spring: application: name: config-client cloud: #Config客户端配置 config: label: main #分支名称 以前是master name: config #配置文件名称 profile: dev #读取后缀名称 # main分支上config-dev.yml的配置文件被读取 http://config-3344.com:3344/master/config-dev.yml uri: http://localhost:3344 # 配置中心地址k #服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka -
主启动类:
@SpringBootApplication @EnableEurekaClient public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run(ConfigClientMain3355.class, args); } } -
业务类:将配置信息以 REST 窗口的形式暴露
@RestController public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
动态刷新
分布式配置的动态刷新问题,修改 GitHub 上的配置文件,Config Server 配置中心立刻响应,但是 Config Client 客户端没有任何响应,需要重启客户端
-
引入 pom 依赖:
<!--web/actuator这两个一般一起使用,写在一起--> <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> -
修改 yml,暴露监控端口:SpringBoot 的 actuator 启动端点监控 Web 端默认加载默认只有两个 info,health 可见的页面节点
management: endpoints: web: exposure: include: "*" # 表示包含所有节点页面 exclude: env,beans # 表示排除env、beans -
业务类:加 @RefreshScope 注解
@RestController @RefreshScope public class ConfigClientController { // 从配置文件中取前缀为server.port的值 @Value("${config.info}") private String configInfo; // config-{profile}.yml @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
此时客户端还是没有刷新,需要发送 POST 请求刷新 3355:curl -X POST "http://localhost:3355/actuator/refresh
引出问题:
- 在微服务多的情况下,每个微服务都需要执行一个 POST 请求,手动刷新成本太大
- 可否广播,一次通知,处处生效,大范围的实现自动刷新
解决方法:Bus 总线