前言
在微服务系统中,将我们的系统,根据业务拆分成不同的微服务,此时,几乎每一个微服务中,都会有一个配置文件,这些配置文件中配置了:数据库信息、缓存、参数等,不同的服务上,如果一个配置发生变化,需要修改很多服务的配置,修改之后,系统读取的配置还依然是之前的配置,只好重新启动服务甚至要重新打包进行部署,很繁琐,那么SpringCloudConfig就体现出自己的作用了
SpringCloudConfig
spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。我们的配置文件放在版本控制服务器gitHub或者svn上
- 分为两端Server端和Client端,Server端通过git地址来读取git上所存储的配置文件,Client端用来读取Server端拉去过来的配置文件,同时Server端也要向Eureka服务注册中心注册,也是为了实现集群
那么我们此时项目中需要一个服务发现注册中心,这里我们使用Eureka来做我们的服务发现注册中心
此次使用的是gitHub的git仓库,配置文件使用的yml格式
Eureka注册中心的配置
server:
port: 56799
spring:
application:
name: jwxt-eureka01 #当前应用的名字(对应注册后的应用名,不设置会默认为UNKNOWN)
eureka:
instance:
hostname: server1 #eureka地址
prefer-ip-address: true #是否显示ip
instance-id: my_eureka56799 #注册后的实例id
client:
fetch-registry: false #是否拉取服务表
register-with-eureka: false #是否注册进入eureka(当前的eureka不注册进入注册中心)
service-url: #服务的路径
defaultZone: http://localhost:56798/eureka/ #默认注册地址,当使用security时要加入对应的用户名和密码http://g01:123456@server2:8762/eureka/ 其他端使用方法一致
server:
enable-self-preservation: false #Eureka的自我保护机制关闭,红字提醒(生产环境不推荐关闭)
eviction-interval-timer-in-ms: 60000 # 默认为60 * 1000ms (一分钟)
这时,我们来做我们的SpringCloudConfig配置中心
- 核心依赖 ConfigServer端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 核心配置文件 application.yml
####端口号
server:
port: 8888 #Config端的端口号
spring:
application:
name: jwxt-config
profiles:
# 读取本地配置
# active: native
# 读取Git 读取网络仓库配置
active: git
cloud:
config:
label: master #git仓库分支
server:
git:
#存储配置文件的git地址
uri: https://github.com/***********/.git
#倘若git仓库设置为私有需要配置用户名username和密码password,公开则不需要指定
##用户名
#username: *********@qq.com
##密码
#password: *********
#读取仓库指定的文件夹下,默认不写为根目录下
search-paths: /文件夹名称
#向Eureka服务发现注册中心进行注册
eureka:
client:
service-url:
defaultZone: http://localhost:56798/eureka/,http://localhost:56799/eureka/
instance:
prefer-ip-address: true
instance-id: my_config
- 注解 @EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer //全局配置中心Server端
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
此时我们的Config配置中心Server端就配置完成了,接下来配置Client端,Client顾名思义也就是我们的每一个需要读取配置配置文件的微服务端,ConfigClient端
- 核心依赖 向我们的每一个微服务端pom中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 核心配置文件 application.yml
spring:
cloud:
config:
#configServer端的链接地址
uri: http://localhost:8888
#读取分支
label: master
#名称(文件前缀名称)
##这里得name和profile很重要重要决定是否准确读取到我们在git上存储的配置文件
## git仓库存储的配置文件命名规则 {name}-{profile}
## 前缀name为我们的微服务名称,便于我们理解见名知意
## profile为我们的环境 是生产环境prod 还是开发环境dev
## 笔者在git仓库上的配置文件名称为 application-teacher-dev.yml 后缀 的yml不用管
name: application-teacher
#环境 (例如 name-dev)
profile: dev
此时我们的Config客户端也就完成了,启动试试看
很多场景下,需要在运行期间动态调整配置。如果配置发生了修改,微服务也应该实现配置的刷新
不是config不是还有刷新配置的机制么,config刷新配置有两种方式:
1.手动刷新:人工访问接口,重新获取配置文件信息
2.SpringCloud Bus 自动刷新配置文件信息
这里我们使用手动刷新,在我们的ConfigClient端添加依赖
核心依赖
<!--实时监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml
这里是最新配置,Spring boot 2.0的改动较大,/bus/refresh全部整合到actuador里面了,所以之前1.x的management.security.enabled全部失效,不适用于2.0
##actuador配置
management:
endpoints:
web:
exposure:
include: "*"
倘若我们要获取新的配置信息的话访问actuador提供给我们的接口,记得要POST方式请求哦
actuator/refresh
http://localhost:{端口号}/actuator/refresh
然后ConfigClient会刷新最新的配置
同时我们有时要向配置文件中获取属性
@Value("${testValue}")
private String testValue;
这样子我们就可以拿到我们定义的testValue了,为了配合手动刷新配置,我们要在类上面添加@RefreshScope注解
org.springframework.cloud.context.config.annotation.RefreshScope;
@RefreshScope
配合actuado提供的刷新配置的接口,我们就可以手动刷新并获取我们的配置信息