Spring Cloud Config 分布式配置中心
分布式配置中心应用场景
往往,我们使用配置文件管理一些配置信息,比如application.yml
单体应用架构,配置信息的管理、维护并不会显得特别麻烦,手动操作就可以,因为就一个工程;
微服务架构,因为我们的分布式集群环境中可能有很多个微服务,我们不可能一个一个去修改配置然后重启生效,在一定场景下我们还需要在运行期间动态调整配置信息,比如:根据各个微服务的负载情况,动态调整数据源连接池大小,我们希望配置内容发生变化的时候,微服务可以自动更新。
场景总结如下:
- 集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的(一次修改、到处生效)
- 不同环境不同配置,比如数据源配置在不同环境(开发dev,测试test,生产prod)中是不同的
- 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小等配置修改后可自动更新
- 如配置内容发生变化,微服务可以自动更新配置
那么,我们就需要对配置文件进行集中式管理,这也是分布式配置中心的作用。
Spring Cloud Config
Config简介
Spring Cloud Config是一个分布式配置管理方案,包含了 Server端和 Client端两个部分。
- Server 端:提供配置文件的存储、以接口的形式将配置文件的内容提供出去,通过使用@EnableConfigServer注解在 Spring boot 应用中非常简单的嵌入
Client 端:通过接口获取配置数据并初始化自己的应用
Config分布式配置应用
说明:Config Server是集中式的配置服务,用于集中管理应用程序各个环境下的配置。 默认使用Git存储配置文件内容,也可以SVN。
比如,我们要对某个微服务的application.yml进行管理(区分开发环境、测试环境、生产环境)- 登录码云或Github,创建项目config-repo
- 上传yml配置文件,命名规则如下:
{application}-{profile}.yml 或者 {application}-{profile}.properties
其中,application为应用名称,profile指的是环境(用于区分开发环境,测试环境、生产环境等)
示例:cloud-eureka-server-dev.yml、cloud-eureka-server-test.yml、cloud-eureka-server-prod.yml 构建Config Server统一配置中心
新建SpringBoot工程,引入依赖坐标(需要注册自己到Eureka)<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud-parent</artifactId> <groupId>com.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>config</artifactId> <dependencies> <!--eureka client 客户端依赖引入--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--config配置中心服务端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>配置启动类,使用注解@EnableConfigServer开启配置中心服务器功能
application.yml配置server: port: 9006 spring: cloud: config: server: git: # 配置git服务地址 uri: https://github.com/4561651884/config-repo.git # 配置git用户名 username: [email protected] # 配置git密码 password: 851212834141aaa search-paths: - config-repo # 读取哪个分支 label: master测试访问:http://localhost:9006/master/cloud-eureka-server-dev.yml,查看到配置文件内容
构建Client客户端(在已有的微服务基础上)
已有工程中添加依赖坐标<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>application.yml修改为bootstrap.yml配置文件
bootstrap.yml是系统级别的,优先级比application.yml高,应用启动时会检查这个配置文件,在这个配置文件中指定配置中心的服务地址,会自动拉取所有应用配置并且启用。
(主要是把 与统一配置中心连接的配置信息 放到bootstrap.yml)
注意:需要统一读取的配置信息,从集中配置中心获取
bootstrap.yml:server: port: 8080 spring: cloud: # config客户端配置,和ConfigServer通信,并告知ConfigServer希望获取的配置信息在哪个文件中 config: # 配置文件名称 name: cloud-eureka-server # 后缀名称 profile: dev # 分支名称 label: master # ConfigServer配置中心地址 uri: http://localhost:9006Config配置手动刷新
不用重启微服务,只需要手动的做一些其他的操作(访问一个地址/refresh)刷新,之后再访问即可
此时,客户端取到了配置中心的值,但当我们修改GitHub上面的值时,服务端(Config Server)能实时获取最新的值,但客户端(Config Client)读的是缓存,无法实时获取最新值。Spring Cloud 已经为我们解决了这个问题,那就是客户端使用post去触发refresh,获取最新数据。- Client客户端添加依赖springboot-starter-actuator(已添加)
Client客户端bootstrap.yml中添加配置(暴露通信端点)
management: endpoints: web: exposure: include: refresh也可以暴露所有的端口
management: endpoints: web: exposure: include: "*"- Client客户端使用到配置信息的类上添加@RefreshScope(统一配置中心里的属性值,可以通过@Value注解获取)
- 手动向Client客户端发起POST请求,http://localhost:8080/actuator/refresh,刷新配置信息
注意:手动刷新方式避免了服务重启(流程:Git改配置—>for循环脚本手动刷新每个微服务)
可否使用广播机制,一次通知,处处生效,方便大范围配置刷新?详细请看下一篇——消息总线Bus
评论已关闭