Spring Cloud Config介绍
为什么要Spring Cloud Config?
之前我们搭建的每个微服务配置都是通过全局配置文件进行配置的。但在实际开发中,集群会有很多微服务,每个服务都有相应的配置。那么在项目开始运行后,我们如何对集群的配置进行集中管理,并且做到配置修改后无需重启服务呢? Spring Cloud为这些问题提供了解决方案—Spring Cloud Config。
什么是Spring Cloud Config?
概述:Spring Cloud Config适用于Spring应用程序,当然也可以结合其他语言编写的应用程序配合使用。Spring Cloud Config为分布式系统中的外部配置提供了配置服务器(简称服务器)和配置客户端 (简称客户端),即Config Server和Config Client。
作用:通过对Config Server和Config Client的配置,可以很好地管理集群中的配置文件。
Config Server介绍
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理每个微服务环境下的配置,默认是在Git存储配置内容(本书只对Git存储配置做详解,也可以使用其他存储配置),这样做可以方便实现对配置的版本控制与内容审计。Config Server服务器主要有下列用途:
- 1.分布式配置中心,独立的微服务应用,用来连接配置仓库(Git)并为客户端提供获取配置信息的访问接口。
- 2.对配置文件中的属性进行加密和解密。
- 3.通过使用@EnableConfigServer注解可以简单地嵌入Spring Boot的应用中。
-
Config Client 介绍
- Config Client是Config Server的客户端,用于操作存储在Config Server中的配置属性,它主要有下列用途:
- 1.绑定配置服务器,使用远程仓库的配置文件中的属性来初始化Spring容器。
- 2.对配置文件中的属性进行加密和解密。
-
Spring Cloud Config架构介绍
-
如Spring Cloud Config架构图所示。所有的Config Client都是从Config Server中获取配置信息。因为在实际开发中,我们会先把服务配置都放在远程Git仓库中,Config Client在启动时,会请求Config Server,Config Server中存放了它提前从远程Git仓库中提取的配置信息,Config Client只需从Config Server获取所需要的配置信息,然后缓存这些配置信息以提高性能。
-
Config Server读取配置文件
-
Config Server读取配置文件有两种方式:
- 从本地仓库读取配置文件:将所有的配置文件统一存放在Config Server项目的resource目录下, Config Server通过暴露的HTTP API接口读取配置文件。
- 从Git仓库读取配置文件。
-
Config Server从本地读取配置文件
-
1.创建config-server项目
- pom.xml
-
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaofeng</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在程序的启动类ConfigServerApplication添加@EnableConfigServer注解,开启Config Serve功能。
-
@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
在项目的配置文件application.yml中进行相关配置,包括指定服务名、端口号、本地读取配置以及读取路径。
-
spring : cloud: config: server: native: search-locations: classpath:/shared #读取路径 profiles: active: native #本地读取配置文件 application: name: config-server server: port: 8769
在项目的resource目录下建一个 shared 文件夹,用于存放本地配置文件。在 shared目录下,新建一个config-client-dev.yml文件,用作后续将要创建的config-client工程的dev开发环境的配置文件。在config-client-dev.yml配置文件中,配置端口号和自定义变量。
-
2.创建config-client项目
使用Spring Initializr方式创建一个Spring Boot项目,这里将Group命名为com.itheima,将Artifact命名为config-client,添加Config、Web和Test的起步依赖。其中Config依赖如下。
在resources文件下新建bootstrap.yml文件。<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xiaofeng</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
为了更直观的看到配置文件config-client-dev.yml被读取,我们在启动类中添加一个hi()方法进行测试。spring: application: name: config-client cloud: config: uri: http://localhost:8769 #指定读取配置文件地址 fail-fast: true #是否开启快速失败 profiles: active: dev #指定配置文件开发环境
-
@SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${foo}") //获取配置文件属性 private String foo; @GetMapping("/foo") public String hi(){ return foo; } }
3.项目测试
先启动config-server项目,再启动config-client项目,我们可以在控制台看到如下所示的日志。 -
Config Server从远程GIt仓库读取配置文件
-
创建仓库
- 首先百度搜索码云,进行登录后,进行以下操作
测试
从控制台消息中可以看出,config-client客户端从Git远程仓库https://gitee.com/fanlksanva/xiaofeng.git的cong文件夹下,读取了config-client-dev.yml文件的配置信息。并且浏览器显示如下:
搭建高可用的ConfigServer
服务实例很多时,所有的服务实例需要同时从配置中心Config Server读取配置文件,这时可以考虑将配置中心Config Server做成一个集群化的微服务,从而达到高可用。将Config Server和Config Client注册在Eureka Server。
Config Server多实例集群部署的架构图:
搭建流程:
1.创建Eureka Server(前面已创建过,复制即可)
2.改造Config Server项目
Config Server作为服务器,需要在工程中的pom.xml配置文件中加入 Eureka Client起步依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-
client</artifactId>
</dependency>
在项目的启动类ConfigServerApplicatio添加@EnableEurekaServer和@EnableConfigServer注解,开启Eureka Server和Config Server功能
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
修改配置文件application.yml文件,为Config Server指定服务注册的地址等信息。
server:
port: 8769
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/fanlksanva/xiaofeng.git
username: 码云账号
password: 码云密码
default-label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka
3.改造Config Client项目
1.在pom文件中加入Eureka Client起步依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.在项目启动类上添加@EnableEurekaClient注解启动Eureka Client功能。
2.修改bootstrap.yml文件
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8769
fail-fast: true
profiles:
active: dev
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka
4.搭建Config Server集群
复制多一个config server,再将application.yml的端口号改为8768即可
5.项目测试
整合spring Cloud Bus实现配置自动刷新
在实际开发中,如果我们更改了Config Server的配置,那我们需要重启项目,手动去刷新。如果所有微服务节点的配置都需要手动去刷新,那么工作量会随之加重。不仅如此,随着系统的不断扩张,会越来越难以维护。实现配置的自动刷新是很重要的一件事情,为了解决这个问题,Spring团队推出了Spring Cloud Bus.
Spring Cloud Client与Spring Cloud Config整合的架构图来学习Spring Cloud Bus:
微服务A的所有实例都通过Spring Cloud Bus消息总线连接到了一起,每个实例都会从Config Server订阅配置更新事件并获取配置信息。当其中一个微服务节点的/ous/refresh端点被请求时,该实例就会向Spring Cloud Bus消息总线发送一个配置更新事件,其他实例通过Spring Cloud Bus消息总线获得该事件后也会从Config Server获取最新的配置信息并更新配置。
搭建流程:
1.安装RabbitMQ并启动
RabbitMq(Erlang环境)安装_erling和rebbitmq百度网盘-CSDN博客
一定要启动!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2.添加依赖
改造Config Server与Config Client,在各自的pom.xml配置文件中添加spring-cloud-starter-bus-amqp依赖实现配置自动更新与spring-boot-starter-actuator依赖监控系统健康情况的工具。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3.修改配置文件
在config-client和config-server中加入圈出来的代码
4.改造Config Client
改造Config Client,在需要动态刷新配置的类ConfigClientApplication上加上注解@RefreshScope
@SpringBootApplication
@RestController
@EnableEurekaClient
@RefreshScope
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
private String foo;
@GetMapping("/foo")
public String hi(){
return foo;
}
}
5.项目测试
在码云中修改配置文件中的foo,无需重启服务,我们重新访问会自动更新