Spring Cloud Config:分布式配置中心
一、分布式配置中心简介
1. 什么是分布式配置中心
正如每一个应用程序在运行时都需要相应的配置(目前配置主流应用配置文件的形式),而在分布式架构下多个服务器和应用服务面临着多个配置文件,在修改和发布上难度较大,需要有一个管理中心来统一管理,优雅的解决了配置的动态变更、持久化、运维成本等问题。综上所述,配置中心就是一种统一管理各种应用配置的基础服务组件。
2. 为什么使用分布式配置中心
在常规的开发中,每个微服务都包含代码和配置。其配置包含服务配置、各类开关和业务配置。如果系统结构中的微服务节点较少,那么常规的代码+配置的开发方式足以解决问题。当系统逐步迭代,其微服务会越来越复杂,慢慢演化成网状依赖结构,这个时候常规的代码+配置的开发方式就并不合适了,因为还要考虑整体系统的扩展性、伸缩性和耦合性等。这些问题中,配置的管理也是非常麻烦的。
如果还是以常规开发形式管理配置,则要承担反复修改编译代码、重启系统、重新打包等风险。所以,一个可以集中管理,带有版本控制的配置中心应运而生。
Spring Cloud Config就是一个分布式配置中心解决方案。其采用集中式管理每个微服务的配置信息,并使用GIT等版本仓库统一存储配置内容,实现版本化管理控制。
3. Spring Cloud Config简介
Spring Cloud Config为分布式系统中的配置提供服务器端和客户端支持。可以集中管理所有环境中应用程序的配置文件。其服务器端存储的默认实现使用GIT。
4. Spring Cloud Config优势
- 提供服务端和客户端支持(spring cloud config server和spring cloud config client)
- 集中式管理分布式环境中的配置信息(所有配置文件统一放在了GIT仓库中)
- 基于Spring环境提供配置管理,与Spring系列框架无缝结合
- 可用于任何语言开发环境,基于Http协议。
- 默认基于GIT仓库实现版本控制。
5. Spring Cloud Config架构
二、在Gitee中管理配置文件
配置文件可以在本地创建后使用git上传。在学习过程中更加简单的方式是直接在gitee中创建文件,编辑内容。
1. 新建GIT仓库
新建命名为config的仓库
2. 新建配置文件
新建命名为wck.yml的文件
3. 编辑配置文件
编辑配置文件内容,定义一个内容为: my.content=wck
三、搭建Config Server
Config Server主要的任务是从Git(远程或本地仓库)获取到配置文件,Config Client通过REST获取到配置文件的内容。使用刚刚搭建好的Gitee上得仓库存储配置文件。
1. POM依赖
如果只是访问远程git仓库,对外暴露url的功能,只需要添加spring-cloud-config-server依赖(包含了spring-boot-starter-web)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- spring cloud系列技术中,唯一命名特殊的启动器依赖。 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2. 编辑配置文件
server:
port: 8888
# 增加分布式配置中心服务端配置。连接什么GIT仓库
spring:
application:
name: config-server
cloud: # spring cloud常用配置前置
config: # 分布式配置中心配置前置
server: # 服务端配置
git: # git文件仓库配置
uri: https://gitee.com/wck_test/config.git # git仓库具体地址
#username: wck_test # 私有仓库必须配置用户名和密码。
#password: 123456789 # 公开仓库可以省略用户名和密码配置。
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3. 编辑启动类型
/**
* EnableConfigServer - 开启Spring Cloud Config Server的注解。
* 提供分布式配置中心服务端功能。
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
}
4. 访问测试
在浏览器地址栏输入 http://localhost:8888/name/profile/label ,基于Config Server获取需要的配置文件。
路径地址说明:
-
name-必要restful参数,代表配置文件主体名称。
-
profile-必要restful参数,代表配置文件扩展环境名称。默认读取default环境扩展配置。
-
label-可选参数,代表配置文件所在GIT分支名称。默认null,相当于master分支
访问 http://localhost:8888/wck/default/master 返回如下结果:
四、搭建Config Client
Config Client对于Spring Cloud Config是客户端,对于Eureka来说可以是Application Server 也可以是Application Client。示例代码以加载gitee中的wck.yml文件进行举例,如果加载到里面my.content表示加载成功。
1. POM依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- spring cloud config分布式配置中心客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependency>
</dependencies>
2. 编辑配置文件
新建bootstrap.yml。配置解释如下:
- uri: Config Server访问url。默认值http://localhost:8888
- name:gitee中配置文件名称。name-profile.yml中name
- profile: 配置文件的profile。name-profile.yml中profile,默认值default。如果服务端有一个name-default.yml文件将会加载此文件,不在加载name.yml。可以通过配置profile:’’形式让其加载name.yml。
- label:分支,默认值null,相当于master分支。
# 新配置文件 bootstrap.yml | properties。是spring cloud config技术支持的新配置文件。
# 配置文件由config分布式配置中心客户端读取,并请求分布式配置中心服务端,查询获取配置文件之后,Spring Boot根据配置文件,初始化环境
spring:
cloud:
config: # spring cloud config 客户端配置
uri: http://localhost:8888 # 分布式配置中心服务端地址。 默认http://localhost:8888
name: wck# 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application
profile: default # 要读取的配置文件环境是什么,默认default
label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。
3. 编辑服务接口
public interface ConfigClientService {
String test();
}
4. 编辑服务实现
@Service
public class ConfigClientServiceImpl implements ConfigClientService {
@Value("${my.content}")
private String content;
@Override
public String test() {
System.out.println("content = " + content);
return content;
}
}
5. 编辑控制器
@RestController
public class ConfigClientController {
@Autowired
private ConfigClientService configClientService;
@RequestMapping("/test")
public String test(){
return configClientService.test();
}
}
6. 编辑启动类型
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
7. 启动测试
在浏览器地址访问:http://localhost:8080/test ,观察视图结果。
五、热刷新
Config-client客户端服务应用,在启动的时候,会根据bootstrap配置内容远程访问config-server,获取当时的最新的配置内容。如果config-client运行过程中, GIT仓库中的配置内容发生变更,config-client不会自动的加载刷新配置内容。需要人为干预。重启服务,一定会刷新配置。但是重启服务代价太高。这时就可以使用热刷新。
热刷新指的是服务不重启,不间断对外提供服务,而是重新加载配置内容,初始化应用环境。
因为Spring Boot只有在启动时才加载一次配置文件,所以gitee上配置文件修改了,项目也不会实时跟随变化。既然是Spring Boot的问题,可以使用Spring Boot提供的Actuator来实现刷新项目功能。这就需要依赖spring-boot-starter-actuator启动器来实现。
1. Config Client新增POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. Config Client新增配置内容
需要在bootstrap.yml配置文件中增加配置。management.endpoints.web.exposure.include默认值是info,health。可以使用‘*’代表开启所有的Actuator功能。
management:
endpoints:
web:
exposure:
include: refresh,info,health
3. Config Client修改服务实现
在使用远程配置内容的类上(案例中的Service,放在controler上无效)增加新注解:
@Service
@RefreshScope
public class ConfigClientServiceImpl implements ConfigClientService {
@Value("${my.content}")
private String content;
@Override
public String test() {
System.out.println("content = " + content);
return content;
}
}
4. 测试热刷新
修改GIT仓库中的配置文件内容,把my.content值改成“北京尚学堂”。使用PostMan工具,发送POST请求,访问 http://localhost:8080/actuator/refresh ,再次访问 http://localhost:8080/test
六、基于IDEA中的GIT插件管理配置文件
同学们可能已经发现,如果直接在Gitee中新建配置文件,提示编译等功能都没有,出现编写错误的可能性比较大。如果我们可以在一个项目中把配置文件编写完成后在提交到远程git仓库的话,大大减少出错的概率,同时提升了开发效率。使用前提:在本机中安装git。
1. 新建仓库
新建仓库idea-config。不需要做仓库初始化。
2. 新建工程
新建工程config_manager,工程名称可任意。
3. 初始化仓库
选择工程中的src/main/resources目录,并初始化仓库。
4. 设置远程仓库地址
选中初始化GIT仓库的目录,src/main/resources。鼠标右键,设置远程仓库。
5. 编辑配置文件
新建配置文件wck.yml,并定义内容如下: