Hello大家好,我是初晨,本章我们学习SpringCloud 的统一配置中心的使用。大家有问题和意见可以发邮箱mr_beany@163.com
一:为什么需要统一配置中心
简单来说就是将项目中的配置放到统一的服务下进行管理,既可以保证配置内容的安全和权限有可以保证不重启服务就能更新服务配置。例如线上数据库信息一般不会对开发人员开放等等
二:创建统一配置中心
1:创建过程与 SpringCloud 实战二:Client的创建和高可用 一样
创建好的目录如下
2:打开pom文件,添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3:修改启动类,使该服务变成config的server端
添加注解@EnableConfigServer
4:创建远程git仓库
可以选择gitHub,码云或自己的私服git服务器,这里我使用的是码云
新建一个文件
将client服务的配置文件内容拷贝到文件,并提交
5:配置config服务的git地址
修改application.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/beany/config-juejindome
username: mr_beany@163.com
password: 2ewed678wediwen
其中uri为git的仓库地址,username为git用户名,password为git密码
6:启动项目,浏览器输入
http://localhost:8084/client-dev.yml
我们可以看到就是我们添加到git的内容
查看控制台,我们也可以发现已经将git远程文件拉取到本地git
那么如果我们想修改拉取的文件路径呢?
修改配置文件:
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/beany/config-juejindome
username: mr_beany@163.com
password: zy555566...
basedir: D:\work\file\juejinDome\cloudTest\configBasedir
重新启动之后打开对应的目录已经可以看到拉取的配置文件
说明:如果想将不同环境下相同的配置统一提取出来,我们需要在远程git上新建client.yml。这样每次访问不同环境下的yml时,会将client.yml与client-dev.yml进行合并之后再使用。
7:那么为什么我们的文件是client.yml,地址栏中要访问client-dev.yml呢?
访问指定配置文件,有以下几种形式
/{name}—{profites}.yml
/{tabel}/{åame}—{profiles}.yml
name 服务名
profiles 环境
tabet 分支(branch)
.yml 指定返回的数据格式,也可以写成.json .properties 他会自动转换成指定的格式
8:测试数据格式
9:测试访问不同环境
将git上client.yml重命名为client-dev.yml
修改内容为:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client
evn:
dev
再新建一个文件
ps:注意一下这里应该是env不是evn,手误打错了
三:使用统一配置中心
1:添加依赖
打开client服务的pom文件,添加如下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
2:修改client服务的配置文件,配置config
首先,修改配置文件的名称,由 application.yml 修改为bootstrap.yml
spring:
application:
name: client
cloud:
config:
enabled: true
discovery:
service-id: CONFIG
profile: dev
这里我们删除了对于Eureka的配置,重新启动之后,发现仍然可以注册到Eureka上去,说明配置生效
注意:由于生产环境中eureka肯定不止启动一个,所以在实际情况中,需要将关于eureka的配置也要写在bootstrap.yml中,即:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client
cloud:
config:
discovery:
enabled: true
service-id: CONFIG
profile: dev
四:动态修改配置文件不重启即生效
当我们client服务启动时,会去config配置中心拉取配置,config再从远程git上拉取配置,那么当启动之后我们再修改配置文件时,client服务是不会生效的,那么我们怎么处理这种情况呢?
这里我们使用队列来进行处理,当git上文件发生变化时,由队列来通知各个服务来实现更新配置文件
1:引入依赖
打开config服务的pom文件,添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2:启动RabbitMQ
在上一章我们已经讲解了怎么使用Docker安装启动RabbitMQ,现在我们就把它用起来。
3:修改config服务的配置文件
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/beany/config-juejindome
username: mr_beany@163.com
password: zy555566...
basedir: D:\work\file\juejinDome\cloudTest\configBasedir
rabbitmq:
host: 192.168.88.134
username: user
password: password
添加rabbitmq的地址账号密码信息,重新启动。
登陆rabbitmq的管理页面http://192.168.88.134:15672
我们可以看到这里SpringCloud BUS已经为我们创建好了一个队列
4:修改client服务的pom文件
添加如下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
5:修改git上client服务的配置文件client-dev.yml
添加如下配置
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client
rabbitmq:
host: 192.168.99.100
username: user
password: password
env:
test
6:重新启动client
MQ中已有两个队列消息。
7:创建TestController.java
package com.chuchen.client.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author:
* @CreateDate: 2019/4/24 11:17
*/
@RestController
@RefreshScope
public class TestController {
@Value("${env}")
private String env;
@GetMapping("/printEnv")
public String printEnv(){
return env;
}
}
其中@RefreshScope为配置文件自动刷新
8:浏览器输入
http://localhost:8081/printEnv
可以获取到我们写在git上的配置的。
9:修改config服务的配置文件
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/beany/config-juejindome
username: mr_beany@163.com
password: zy555566...
basedir: D:\work\file\juejinDome\cloudTest\configBasedir
rabbitmq:
host: 192.168.99.100
username: user
password: password
management:
endpoints:
web:
exposure:
include: "*"
这里我们选择开放一个接口,这个接口的作用是当git上文件发生变化时,通知我们系统重新更新配置文件,来实现动态刷新。
10:修改git上配置文件
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client
rabbitmq:
host: 192.168.99.100
username: user
password: password
env:
test1111
11:打开postman 访问config服务
http://localhost:8084/actuator/bus-refresh
12:浏览器输入
http://localhost:8081/printEnv
我们发现在没有重新启动config服务和client服务的情况下,我们实现了动态刷新client服务配置文件的功能
13:git仓库设置WebHooks
在上面,每次git仓库修改过后我们都要访问一下config刷新配置文件的接口,这显然是不合理的。那么我们可以通过在git上的配置,让每次提交代码时都自动访问我们的接口来实现修改后自动刷新。
选择仓库,点击管理,选择WebHooks,点击添加
注意,这里的url为外网访问地址,大家可以部署服务器之后再来添加,
五:结尾
感谢大家支持,系列文章接下来陆续更新,感谢大家。
对于springBoot还不了解的朋友可以看我的SpringBoot系列教程