概述
从下面的架构图可以看到传统的单体应用和一些小的SOA服务因为其服务数量较少,所以其配置能以服务为分割单位保存在每个服务当中,但是这样会面临多个问题:1、如果某个配置修改了,需要对所有服务重新打包。2、因为配置保存不同服务中,即代表着每增加一个服务就要增加对应配置的维护成本。

1.Spring Cloud Config
基于概述中提到的问题,还有在微服务架构中不断增加的服务数量,配置中心就应运而生。
Spring Cloud Config由Spring Cloud官方推出的配置中心。
相比市面上其他配置中心Apollo和Nacos,Spring Cloud Config是最简单易用的配置中心,拥有最基础的配置中心功能
1.1 Spring Cloud Config架构图

因此Spring Cloud Config将分为两个部分
1.提供配置的Spring Cloud Config Server,他通过本地、Git等方式加载到配置文件,并通过HTTP形式提供访问
2.获取配置的Spring Cloud Config Client,通过配置uri统一从一个出口获取配置
2 Spring Cloud Config(本地)
2.1 Spring Cloud Config Server
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
添加配置文件application.yml
server:
port: 1111
spring:
cloud:
config:
server:
native:
search-locations: file:///Users/kirra/Desktop/project/gitconfig/
profiles:
active: native
1.当使用本地配置文件作为加载配置的方法的时候需要配置spring.profiles.active=native。
2.spring.cloud.server.prefix是可选参数添加了这个配置后访问配置的路径即为http://127.0.0.1:端口号/YYYY/xxx
3.spring.cloud.server.native.search-locations 作为必填配置填入配置文件本地所在地
通过在启动类添加@EnableConfigServer使该服务成为Spring Cloud Config服务端
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String args[])
{
SpringApplication.run(ConfigApplication.class,args);
}
}
通过启动日志,我们可以看到Spring Cloud Config Server的EnvironmentController提供了多种HTTP API进行访问:http://127.0.0.1:1111/springConfig/dev

2.2 Spring Cloud Config Client
引入依赖
<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>
追加配置文件bootstrap.yml,不使用application.yml是确保配置能被优先加载
spring:
cloud:
config:
uri: http://127.0.0.1:1111
name: springConfig
profile: dev
1.spring.cloud.config.uri配置拉取文件的Http路径,即为我们Spring Cloud Config Server提供的路径http://127.0.0.1:1111/
2.spring.cloud.config.name与spring.cloud.config.profile是一对配置,如有配置为springConfig-dev.yml的配置需要配置spring.cloud.config.name=springConfig,spring.cloud.config.profile=dev,多组数组就配置成spring.cloud.config.name=springConfig,.......
添加一个测试控制类用@Value来获取配置
@RestController
public class ValueController {
@Value("${spring.config.version}")
private String version;
@RequestMapping(value = "version")
public String getversion()
{
return version;
}
}
启动服务可以看到
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://127.0.0.1:1111/config
c.c.c.ConfigServicePropertySourceLocator : Located environment: name=tf-auth, profiles=[dev], label=null, version=null
b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='file:///C:/Users/Administrator/Desktop/authtest/yml/tf-auth-dev.yml']]]
访问测试类可得:

3 Spring Cloud Config(GIT)
3.1 Spring Cloud Config Server
spring:
profiles:
active: git
cloud:
config:
server:
git:
uri: https://gitee.com/stephanie0823/gitconfig.git
search-paths: / # 读取文件的根地址
default-label: master # 使用的默认分支,默认为 master
username: #git用户名
password: #git密码
1.spring.profiles.active=git,标记使用git
2.git作为配置来源提供了他对应的配置spring.cloud.config.server.git。
uri配置repository的地址
search-paths读取文件的根地址
default-label使用的默认分支,默认为 master
当git为私库的时候需要配置用户名和密码
usernamegit用户名
passwordgit密码
3.2 Spring Cloud Config Client
2021-03-18 20:07:06.435 INFO 1737 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://127.0.0.1:1111
2021-03-18 20:07:07.005 INFO 1737 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=springConfig, profiles=[dev], label=master, version=865cdc79dde78ed39e2e2490993d5ea342ec0093
2021-03-18 20:07:07.005 INFO 1737 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='https://gitee.com/stephanie0823/gitconfig.git/springConfig-dev.yml']]]
访问测试类可得:

4 配置刷新
添加actuator依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
添加刷新注解@RefreshScope
@RestController
@RefreshScope
public class ValueController {
@Value("${spring.config.version}")
private String version;
@RequestMapping(value = "version")
public String getversion()
{
return version;
}
}
基于不同版本的actuator配置和访问接口不一样,所以根据实际使用的actuator版本进行配置。在当前版本通过使用POST方式访问/refresh接口来刷新配置。

5 配置加密
基于配置中可能存在密码等重要信息,因此给配置文件进行加密是必须的,从官网中可以了解到Spring Cloud Config可以支持对配置参数进行对称或非对称加密,下面将演示线上常用的非对称加密。
5.1 生成keystore
keytool -genkeypair -alias mytestkey -keyalg RSA -dname "CN=KenhoCommon,OU=OU,O=O,L=L,S=S,C=CN" -keypass stephanie -keystore secret.jks -storepass stephanie
-genkeypair:创建密钥对
-alias:别名
-keyalg:指定密钥的算法,非对称性使用的是RSA
-dname:指定证书拥有者信息,做过生成证书的人都知道"CN=KenhoCommon,OU=OU,O=O,L=L,S=S,C=CN"就是我们证书上的拥有者信息
-keypass:私钥的密码
-keystore:生成文件名
-storepass:keystore密码
后面获得secret.jks放入配置服务的resources中
5.2 加密
添加配置在configserver
encrypt:
keyStore:
location: classpath:/secret.jks #store文件
password: stephanie #私钥密码
alias: mytestkey #别名
secret: stephanie #store密码
访问curl http://127.0.0.1:1111/encrypt -d v3.0,获取到加密代码
AQAen4Bmf3mEzJfII43Eu7GEVPLv6qJYeDuBfk4+zZfo7S7HQHOGgA6j7TFuISeGUDY/A8ruDbjJvv45qiN86WDN+OZWXQIyUvyX7iqidrhxO5ClLoZWuJ0yRSrwoFPVthuyggfou/xSgKKGS7dQnaEI8blME1p5I1mthEKB01PoYT7MtEyN+89UqrqQb8DnNzKjFYNod59WEZvwqnEhceP6MZ9ImmgU0llJsgGF77Tvgy8hzt7WQKDom1B/7G214vCfzjiBRAlJrBTI68DgZ2s123ac3ek0jKnTIwuk45uXC1LqHcAonCnna4Qmoe5xwR5qBDxo1jFQ+vpmmoXzNjRFYvP9ETB0OQrIldo1n7fvuNu1Z5s6qa96zifPJxpTEpo=kirradeM
修改配置文件,Spring Cloud Config通过{cipher}来定义该配置加密
spring:
config:
version: '{cipher}AQAen4Bmf3mEzJfII43Eu7GEVPLv6qJYeDuBfk4+zZfo7S7HQHOGgA6j7TFuISeGUDY/A8ruDbjJvv45qiN86WDN+OZWXQIyUvyX7iqidrhxO5ClLoZWuJ0yRSrwoFPVthuyggfou/xSgKKGS7dQnaEI8blME1p5I1mthEKB01PoYT7MtEyN+89UqrqQb8DnNzKjFYNod59WEZvwqnEhceP6MZ9ImmgU0llJsgGF77Tvgy8hzt7WQKDom1B/7G214vCfzjiBRAlJrBTI68DgZ2s123ac3ek0jKnTIwuk45uXC1LqHcAonCnna4Qmoe5xwR5qBDxo1jFQ+vpmmoXzNjRFYvP9ETB0OQrIldo1n7fvuNu1Z5s6qa96zifPJxpTEpo=kirradeM'
访问client

参考:https://docs.spring.io/spring-cloud-config/docs/2.2.7.RELEASE/reference/html/
github:https://github.com/tale2009/Distributed-Learning/tree/main/springcloudconfig-learning
167万+





