分布式配置中心
项目搭建参考https://blog.youkuaiyun.com/qq_40977118/article/details/104738485
1. Spring Cloud Config
- 分布式配置中心解决了什么问题
1、抽取出各模块的公共的配置,做到一处修改各处生效的目标 ;
2、做到系统的高可用,修改了配置文件后可用在个模块动态刷新,不需要重启服务器。 - 分为服务端和客户端两个部分
1、服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用;
2、客户端可以通过配置中心来获取配置信息,在启动时加载配置 。
2. 配置仓库
- dev开发环境配置文件,test测试环境配置文件
- username和password
3. 创建一个新的module
4. pom引入依赖
<?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>
<groupId>com.spring.fisher</groupId>
<artifactId>springcloud-config-server</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springcloud-config-server</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!--springcloud版本号-->
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--springbootweb启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--健康检查-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!--springcloud-->
<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>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
5. 开启配置中心功能
@EnableConfigServer
6. 服务端配置
server.port=8085
eureka.client.serviceUrl.defaultZone=http://root:root@localhost:8763/eureka/
spring.application.name=config-server
#分布式配置中心服务端配置
#git地址
spring.cloud.config.server.git.uri=https://gitee.com/fisher3652/springcloud-config
#指定仓库下的哪个文件夹
spring.cloud.config.server.git.search-paths=configFiles
#git用户名密码
spring.cloud.config.server.git.username=Fisher3652
spring.cloud.config.server.git.password=123456
#本地缓存目录(需要自己提交创建)
spring.cloud.config.server.git.basedir=D:/config-server/tmp
#强制从GitHub配置中心中拉取配置信息,不走缓存
spring.cloud.config.server.git.force-pull=true
#激活所有的端点的web方式请求
management.endpoints.web.exposure.include=*
7. 客户端配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>LATEST</version>
</dependency>
- 通过ip和端口号连接
- 或者通过服务名称连接(高可用情况下使用此方法)
#指定git上配置文件的后缀,可以有多个环境配置文件
spring.cloud.config.profile=dev
#指定git上的分支名称
spring.cloud.config.label=master
#分布式配置中心的ip和端口号
spring.cloud.config.uri=http://localhost:8085/
#开启configserver服务发现功能
#spring.cloud.config.discovery.enabled=true
#服务发现的服务名称
#spring.cloud.config.discovery.service-id=config-server
8. 客户端获取远端配置信息
- 可以使用@Value方式,或者Environment对象
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Autowired
Environment environment;
9. 依次启动服务
10. 请求接口http://localhost:8084/queryContent
11. 断点查看获取情况
12. 客户端快速失败
- 当客户端连服务端失败时,客户端就快速失败,不进行加载其他的 spring 容器
spring.cloud.config.fail-fast=true
13. 客户端重试
- 引入依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
#默认重试的间隔时间,默认1000ms
spring.cloud.config.retry.multiplier=1000
#下一间隔时间的乘数,默认是1.1
#spring.cloud.config.retry.initial-interval=1.1
#最大间隔时间,最大2000ms
spring.cloud.config.retry.max-interval=2000
#最大重试次数,默认6次
spring.cloud.config.retry.max-attempts=6
- 关闭配置中心服务,重新启动micro-order服务
- 连接超时,又重新连接
14. 配置信息加密
- 在配置中心中,有些信息是比较敏感的,比如密码信息,在配置密码信息的时候有必要对密码信息加密以免密码信息泄露,springcloud 配置中心支持配置信息加密,这里以 RSA 非对称加密举例。
- 使用此非堆成加密前,需要下载下面这个包
- 下载后放到jdk的security目录,比如:D:\Program Files\Java\jdk1.8.0_144\jre\lib\security
- 如果没有替换上面2个jar,调用加密接口会报错
java.security.InvalidKeyException: Illegal key size
java.lang.IllegalArgumentException: Unable to initialize due to invalid secret key
14.1 本地生成秘钥对
https://docs.spring.io/spring-cloud-config/docs/2.2.5.RELEASE/reference/html/#_key_management
- jdk的D:\Program Files\Java\jre\bin目录有一个keytool.exe 可执行文件
- 执行指令生成秘钥文件
keytool -genkeypair -alias config-server -keyalg RSA -dname “CN=Web Server,OU=Unit,O=Oragnization,L=City,S=State,C=CN” -keypass fisher -keystore config-server.jks -storepass 123456
- config-server为别名,可以自定义;fisher为secret; config-server.jks为文件名;123456为password
- 将文件防止resources目录下
- pom.xml添加配置,防止静态文件扫描不到
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.txt</include>
<include>**/*.jks</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
14.2 密钥配置
#加密配置
encrypt.key-store.location=config-server.jks
encrypt.key-store.alias=config-server
encrypt.key-store.password=123456
encrypt.key-store.secret=fisher
14.3 加密解密接口
- 查看接口状态
http://localhost:8085/encrypt/status
- 加密
http://localhost:8085/encrypt
- 解密
http://localhost:8085/decrypt
14.4 代码仓库密文配置
- 密文前一定要加{cipher},表示需要服务端解密。
14.5 查看解密结果
- 请求http://localhost:8084/queryContent
15. 配置动态刷新
- 在运行期修改配置文件后,我们通过这个动态刷新功能可以不重启服务器。
15.1 Environment 的动态刷新
手动调用刷新接口,如果你想刷新哪台主机 的配置,就调用哪台注解的刷新接口
http://localhost:8086/actuator/refresh
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 添加properties配置
management.endpoints.web.exposure.include=*
- 修改配置文件
- 请求刷新接口前
- 请求刷新接口后
- @Value 注入的值没有被刷新,Environment的值成功被刷新
15.2 @Value 注入的属性动态刷新
- @Value 注入的属性是项目启动就已经定了的。如果要使@Value 属性也刷新,就必须要在类上面加上:@RefreshScope 注解。
- 修改配置文件,请求刷新接口,查看打印
16. 消息总线
- 在一个服务中调用请求刷新接口,然后所有的在消息总线中的端点都能接到刷新的消息,所有我们必须把每一个端点都拉入到消息总线中来,这里只配置了web,order,config3个工程。
16.1 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
16.2 properties配置
- 连接 mq 的配置,刷新配置 url
#rabbitMq
spring.rabbitmq.host=192.168.42.111
spring.rabbitmq.port=5672
spring.rabbitmq.username=fisher
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=fisher
# 刷新配置url http://localhost:port/actuator/bus-refresh
spring.cloud.bus.refresh.enabled=true
spring.cloud.bus.trace.enabled=true
16.3 刷新配置
- 刷新前
- 修改git配置
- 请求刷新接口
http://localhost:8085/actuator/bus-refresh
- 向rabbit中发送消息
- 再次请求查询数据
- 刷新成功
- 接口也可以配置到 Git 中,只要 Git 有代码变更,就会调用这个接口,URL 需要时生产环境的域名地址