分布式配置中心解决了什么问题 ?
A、抽取出各模块公共的部分,做到一处修改处处生效的目标,方便了配置管理
B、做到系统的高可用,修改了配置文件后可用在个模块动态刷新,不需要重启服务器,热加载,配置实时生效
1、springcloud 配置中心服务端搭建(nandao-config-server),jar包 引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2、启动类配置:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication(scanBasePackages = {"com.xiangxue.jack"})
@EnableConfigServer // 配置中心注解
@EnableEurekaClient
public class MicroConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(MicroConfigServerApplication.class,args);
}
}
3、分布式配置中心配置规则
分布式配置中心服务端是需要远程连接代码仓库的,比如 GitHub,gitlab 等,把需要管理的
配置文件放到代码仓库中管理,所以服务端就需要有连接代码仓库的配置:
spring.cloud.config.server.git.uri=https://github.com/nandao158/nandao-config
spring.cloud.config.server.git.search-paths=config-repo
#如果是公共仓库,可以不用配置用户名和密码
spring.cloud.config.server.git.username= xxx
spring.cloud.config.server.git.password= xxx
#本地缓存目录
spring.cloud.config.server.git.basedir=D:/configServerNandao
##强制从GitHub配置中心中拉取配置信息,不走缓存 D:\configServerNandao
spring.cloud.config.server.git.force-pull=true
github 上文件如图:
该配置中心启动后访问一下看看是否连接上Github http://localhost:8085/micro-order-no-test.properties
4、客户端 (nando-order)使用配置中心
客户端只需要指定连接的服务端就行了,从服务端拉取配置信息, jar 包依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>LATEST</version>
</dependency>
<!-- 这是一个重试的jar 包,一会会说-->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
5、客户端bootstrap.properties 配置 和 代码中获取 :
#配置文件的环境
spring.cloud.config.profile=test
#仓库主干或者分支名
spring.cloud.config.label=master
#这种配置是configserver还单机情况,直接连接这个单机服务就行
spring.cloud.config.uri=http://localhost:8085/
#主要核心配置就是以上三行代码
#configserver高可用配置,如果开启后就不要配置 spring.cloud.config.uri=http://localhost:8085/
#开启configserver服务发现功能,前提是开启注册中心
spring.cloud.config.discovery.enabled=true
#服务发现的服务名称
spring.cloud.config.discovery.service-id=config-server
#如果连接不上获取配置有问题,快速响应失败
spring.cloud.config.fail-fast=true
#默认重试的间隔时间,默认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
客户端启动后 访问接口,可以看到从配置中心获取的值
到这里配置中心的服务端和客户端的搭建和简单的演示已经完毕!下面是深入的高级实战和配置加密相关内容!
6、客户端快速失败和重试连接服务端效果(前提是注册中心服务关闭):
7、配置信息加密:
在配置中心中,有些信息是比较敏感的,比如密码信息,在配置密码信息的时候有必要对密
码信息加密以免密码信息泄露,springcloud 配置中心也支持配置信息加密的,这里一 RSA
非对称加密举例。
A、本地生成秘钥对
cd 到 jdk 的 keytool 目录:D:\Program Files\Java\jdk1.8.0_92\jre\bin
里面有一个 keytool.exe 可执行文件
B、执行指令生成秘钥文件
keytool -genkeypair -alias config-server -keyalg RSA -keystore config-server.keystore -validity 365
指令执行成功后会在 bin 目录生成一个 config-server.keystore 文件,把该文件 copy 到配置中
心服务工程中 resources 目录下。
C、服务端工程配置
Properties 配置文件
添加秘钥配置
#加密配置
encrypt.key-store.location=config-server.keystore
encrypt.key-store.alias=config-server
encrypt.key-store.password=123456
encrypt.key-store.secret=123456
pom 中添加静态文件扫描,让能够扫描到.keystore 文件
<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>**/*.keystore</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
D:密码加密和解密接口
在服务端中有提供对信息加密和解密接口的
客户端浏览器验证:http://localhost:8085/encrypt/status
加密接口:http://localhost:8085/encrypt?data=123456,post 请求解密接口:
http://localhost:8085/decrypt,解密接口 post 请求
E:代码仓库密文配置:密文前面一定要加上{cipher}标识这个是密文配置,需要服务端来解密的。
F:配置动态加载刷新
这个是一个革命性的功能,在运行期修改配置文件后,我们通过这个动态刷新功能可以不重
启服务器,这样我们系统理论上可以保证 7*24 小时的工作1、Environment 的动态刷新
动态刷新其实要有一个契机,其实这个契机就是手动调用刷新接口,如果你想刷新哪台主机
的配置,就调用哪台注解的刷新接口
刷新接口为:http://localhost:8088/actuator/refresh
G:@Value 注入的属性动态刷新
其实前面在调用刷新接口后,@Value 注入的属性是没有刷新的还是老的配置,这个也好理
解,@Value 注入的属性是项目启动就已经定了的。如果要使@Value 属性也刷新,就必须要
在类上面加上:@RefreshScope 注解。 如图:
但是调用每台主机的刷新接口显然太麻烦了,如果需要刷新的集群机器有几百台,是不是就
需要手动调用几百次呢,这几乎是一个不能完成的工作量。
Springcloud 中也提供了消息总线的东西,借助 mq 来完成消息的广播,当需要刷新时我们就
只要调用一次刷新接口即可。 配置如下pom.xml 引入jar 和 rabbitMq 配置文件客户端配置:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin # 刷新配置 url http://localhost:8081/actuator/bus-refresh spring.cloud.bus.refresh.enabled=true spring.cloud.bus.trace.enabled=true
H:
刷新接口:http://localhost:8085/actuator/bus-refresh
这个接口也可以配置到 GitHub 中,只要 GitHub 有代码变更,就会调用这个接口
到此分布式配置中心分享结束,大家一定要亲自测试一下,加深印象,如有疑问,欢迎骚扰!!!
下一篇我们会分析网关服务 zuul 组件,敬请期待!