网上先搂一段简介:Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用上。随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。
源码下载路径在:https://download.csdn.net/download/qq_22075041/10869452
先简单的编写一个Config Server,代码参考microservice-config-server模块
pom依赖何在?
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
启动类加注解@EnableConfigServer,配置文件(我们这边是以git为例)如下:
server:
port: 8080
spring:
application:
name: microservice-config-server
cloud:
config:
server:
git:
uri: https://git.oschina.net/itmuch/spring-cloud-config-repo # 配置Git仓库的地址
username: # Git仓库的账号
password:
启动起来就是一个简单的server了。。
=========================================================
流程是这样:config server 连接git,然后应用访问config server获取参数,接下来我们来写一个client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
加一个配置文件bootstrap.yml(不写到application.yml的原因请自行查阅),
spring:
application:
name: microservice-foo # 对应config server所获取的配置文件的{application}
cloud:
config:
uri: http://localhost:8080/
profile: dev # profile对应config server所获取的配置文件中的{profile}
label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
==========================================================================
上面基本案例搞定之后我们来点延伸,比如config server从git获取数据的几种方式。
通过通配符:
server:
port: 8080
spring:
application:
name: microservice-config-server
cloud:
config:
server:
git:
uri: https://git.oschina.net/itmuch/{application}
username: # Git仓库的账号
password: # Git仓库的密码
logging:
level:
org.springframework.cloud: DEBUG
org.springframework.boot: DEBUG
## 测试:可以使用http://localhost:8080/spring-cloud-config-repo-default.yml 获取到http://localhost:8080/spring-cloud-config-repo下的application.properties
模式匹配
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
simple: https://github.com/simple/config-repo
special:
pattern: special*/dev*,*special*/dev*
uri: https://github.com/special/config-repo
local:
pattern: local*
uri: file:/home/configsvc/config-repo
logging:
level:
org.springframework.cloud: DEBUG
org.springframework.boot: DEBUG
# 测试:
# 使用http://localhost:8080/foo-default.yml,可以访问到https://github.com/spring-cloud-samples/config-repo
# 使用http://localhost:8080/special/dev,观察日志及返回结果
子目录查询search-path
spring:
cloud:
config:
server:
git:
uri: http://git.oschina.net/itmuch/spring-cloud-config-repo
search-paths: foo,bar*
logging:
level:
org.springframework.cloud: DEBUG
org.springframework.boot: DEBUG
# 测试:访问http://localhost:8080/application/default
在启动时就clone git仓库,clone-on-start: true (默认为false)如下配置:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
team-a:
pattern: microservice-*
clone-on-start: true
uri: http://git.oschina.net/itmuch/spring-cloud-config-repo
logging:
level:
org.springframework.cloud: DEBUG
org.springframework.boot: DEBUG
# 测试:
# 1.观察启动日志
# 2.访问http://localhost:8080/microservice-foo/dev
============================================================
config server是支持数据加解密的,比如对称加密,代码参考microservice-config-server-encryption模块:
encrypt:
key: foo # 设置对称密钥
非对称加密,要先生成秘钥,代码参考microservice-config-server-encryption-rsa模块,
encrypt:
keyStore:
location: classpath:/server.jks # jks文件的路径
password: letmein # storepass
alias: mytestkey # alias
secret: changeme # keypass
除此之外,我们还可以给config server加上访问密码,这种方式其实我们第一篇文章的一样。不赘述了,参考代码:microservice-config-server-eureka-authenticating模块与microservice-config-client-eureka-authenticating模块。
====================================================
其实config server 和 config client 是可以配合服务发现使用的,client就不需要硬编码了,代码参考microservice-config-server-eureka模块和microservice-config-client-eureka模块,eurake的注册与发现我前面文章说过了,就不赘述了
====================================================================
如果git的配置文件修改了,但是我不想重启client项目,那么我们可以手动刷新(注意是配在client端的),代码参考microservice-config-client-refresh:
先pom文件添加:
<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-actuator</artifactId>
</dependency>
control上加注解@RefreshScope,然后启动项目之后,项目访问根目录/refresh。
=========================================================
手动刷新还是很繁琐,那怎么可以让客户端自动刷新呢?
我们需要借助mq了,此处rabbitMQ为例,怎么安装我就不介绍了,接下来上代码,参考模块是:microservice-config-client-refresh-cloud-bus。
首先加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
然后配置文件中加入mq的配置:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
然后启动客户端,访问:/bus/refresh,即可刷新(当前访问的微服务会发一条消息出去,就是通知其他微服务也刷新配置)。还是不自动么,把这个访问地址配到git的webhook里面,是不是就完全自动了。
有时候,我还不想让所有的都刷新,我只想刷新某个微服务,可以这样子访问/bus/refresh?destination=customers:port,来刷新某个服务的某个端口。还有这个姿势:/bus/refresh?destination=customers:**。
===============================================================================
题外话,我们现在是配置客户端的是吧,意味着某一个微服务要承担起刷新的任务,但是我怕他不愿意,所以呢,我建议大家把这个自动刷新的配置设置在config server端,代码的话和client一样。我就不赘述了,参考模块:microservice-config-server-refresh-cloud-bus