Spring Cloud Config之Config Server和Config Client
通常情况下,在微服务的架构中,配置管理的要求如下:
集中管理配置:全部微服务配置统一管理
区分环境,不同环境不同配置:区分生产、测试、预生产、开发环境等
运行期可以动态调整:可以根据负载,在不停服的情况下动态调整参数
修改配置后可自动更新:配置内容发生改变,微服务能够实现自动更新配置
Spring Cloud Config介绍
Spring Cloud Config 为分布式系统外部化配置提供服务端和客户端的支持,包括Config Server和Config Client两部分。
Config Server是一个可以横向扩展、集中式的配置服务器,用于集中管理应用程序各个环境的配置,默认使用Git存储,当然也可以使用SVN、本地文件等。
Config Client 是Config Server的客户端,用于操作存储在Config Server中的属性配置。 Config Client在启动的时候,会请求Config Server获取所需要的配置信息,然后缓存到自身以便提高性能。
关系图如下:

搭建Config Server的后端存储
在Github上新建项目 spring-cloud-config-center,新建文件如下:

demo-config.properties
name=demo-default profile=default-1.0
demo-dev.properties
name=demo-dev
profile=dev-1.0
demo-test.properties
name=demo-test
profile=test-1.0
demo-prod.properties
name=demo-prod
profile=prod-1.0
新建个config-v2分支,测试线版本控制
搭建Config Server
1、创建spring cloud config server 工程 config-server
2、添加依赖
主要是spring-cloud-config-server这个依赖
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.smart</groupId> <artifactId>eurekademo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.smart</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> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
添加@EnableConfigServer注解
package com.smart; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
application.yml配置文件
server: port: 9764 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/XiangYuXiHu/spring-cloud-config-center.git username: password:
Config Server的端点映射规则
从文档可知,端点和文档的映射规则如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
上述的规则可以映射到{appplication}-{profile}.properties .
- {appplication} : 微服务的名称
- {profile} : 各种标识,这里是用来区分不同的环境
- {label} : 分支,对于Git来说,默认是master
测试
http://localhost:9764/demo/prod
{"name":"demo","profiles":["prod"],"label":null,"version":"40fd6a1955e2e7efad290aec3748324516ab49e3","state":null,
"propertySources":[{"name":"https://github.com/XiangYuXiHu/spring-cloud-config-center.git/demo-prod.properties",
"source":{"name":"demo-prod","profile":"prod-1.0"}}]}
http://localhost:9764/demo-prod.properties
name=demo-prod
profile=prod-1.0
搭建Config Client
Step1. 新建工程:config-client
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.smart</groupId> <artifactId>eurekademo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.smart</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> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件 bootstrap.yml
server:
port: 9765
spring:
application:
# 对应 config server所获取的配置文件的{application}
name: demo
cloud:
config:
uri: http://localhost:9764/
# profile 对应的config server所获取的配置文件中的{profile}
profile: dev
# 指定git仓库的分支,对应config server说获取的配置文件的{lable}
label: config-v2
spring.application.name 对应 config server所获取的配置文件的{application}
spring.cloud.config.uri 对应config server的地址,默认为http://localhost:8888
spring.cloud.config.profile 对应的config server所获取的配置文件中的{profile}
spring.cloud.config.lable指定git仓库的分支,对应config server说获取的配置文件的{lable}
上述配置信息一定要写到bootstrap.yml中。 如果配到application.yml中,spring.cloud.config.uri 就会访问默认的8888端口,而非配置的端口。
bootstrap.yml 是被一个父级的 Spring ApplicationContext 加载的,在加载application.yml 的 ApplicationContext之前。配置在 bootstrap.yml 中的属性优先级更高,默认情况下不能被本地配置覆盖。
通过Config Client 获取 Config Server端的信息
写个Controller
package com.smart.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ClientController { //config server存储中心对应的 配置文件中得属性名 @Value("${name}") private String name; @Value("${profile}") private String profile; @GetMapping("/getVauleFromConfigServer") public String getValue() { return "config server中的内容:name=" + name + ",profile=" + profile; } }
访问client的控制层 : http://localhost:9765/getVauleFromConfigServer
浙公网安备 33010602011771号