服务端配置
配置中心单独作为一个服务
依赖配置
<dependencies>
<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>
</dependencies>
<build>
<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>
</build>
启动类配置
@EnableConfigServer
配置文件配置
bootstrap.properties
server.port=8085
eureka.client.serviceUrl.defaultZone=http://admin:admin@localhost:8763/eureka/
spring.application.name=config-server
#远程代码仓库
spring.cloud.config.server.git.uri=https://github.com/Asperger12345/micro-config-server
#项目名
spring.cloud.config.server.git.search-paths=/**
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
#本地缓存目录
spring.cloud.config.server.git.basedir=C:/work/config/tmp
#强制从GitHub配置中心中拉取配置信息,不走缓存
spring.cloud.config.server.git.force-pull=true
#加密配置
encrypt.key-store.location=config-server.keystore
encrypt.key-store.alias=config-server
#秘钥库口令
encrypt.key-store.password=
#秘钥口令
encrypt.key-store.secret=
加密
在 JDK 安装文件夹下找到 keytool 工具,生成秘钥文件
keytool -genkeypair -alias config-server -keyalg RSA -keystore config-server.keystore -validity 365
其中,-alias 与配置文件中的 spring.application.name 对应
如果 JDK 在 C 盘,需要以管理员方式运行 cmd
cd C:\Program Files\Java\jdk1.8.0_221\jre\bin
自己记住口令
后面还有一堆需要自己设置的东西,最后输入y,回车确定
放到 resources 目录下
利用 Postman 软件加密/解密
加密:http://localhost:8085/encrypt
解密:http://localhost:8085/decrypt
在中央仓库的配置文件里,写密码的时候,写上面的密文,且前面加上 {cipher}
客户端配置
依赖配置
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>LATEST</version>
</dependency>
<!--如果需要重试功能-->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<!--手动刷新配置文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--消息总线-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
配置文件配置
bootstrap.properties
#环境
spring.cloud.config.profile=dev
#分支
spring.cloud.config.label=master
#spring.cloud.config.uri=http://localhost:8085/
#开启服务发现功能,不用uri的方式
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
#消息总线,每一个端点都要配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
#刷新配置url http://localhost:8085/actuator/bus-refresh
spring.cloud.bus.refresh.enabled=true
spring.cloud.bus.trace.enabled=true
刷新配置文件
EurekaClient 里的 bean 通过 @Value 注入的配置。但 bean 不刷新的话,新配置就无法生效。而且EurekaServer 从 GitHub 上拉取的配置文件也不会自动同步到 EurekaClient 。如何解决呢?
在有 @Value 进行注入值的类上,打上注解
@RefreshScope
请求
http://localhost:8085/actuator/refresh
就能刷新读取配置文件
如果有集群,就要按不同 port 请求多次 http://localhost:port/actuator/refresh
可以使用消息总线解决上面请求多次的问题,在配置文件里可以看到相关配置。
post 请求 http://localhost:port/actuator/bus-refresh
如果一次也不想请求呢?
可以把这个 URL 配置到 GitHub 上的这个项目的 Settings 中,每当这个项目中有更改都会调用这个 URL:
这里的 URL 需要是公网 IP,或者使用内网穿透技术。