SpringCloud Config分布式配置
在微服务数量越来越多,同时配置文件也变得越来越多,如何管理这些配置文件是一个问题。SpringCloud 提供了解决方案,分布式配置。它支持将配置文件配置在本地和远程的git仓库。
主要功能点:
- 提供服务端和客户端支持
- 集中管理各环境的配置文件
- 配置文件修改快速生效
- 版本管理
简单搭建
新建ConfigServer模块,并创建application.yml配置文件添加配置上传到GitHub
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
新建配置文件bootstrap.yml
server:
port: 7777
spring:
application:
name: fxtahe-cloud-configServer
cloud:
config:
server:
git:
uri: https://github.com/fxtahe/cloud-config.git
username:
password:
新建启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
高可用的分布式配置中心
当微服务数量较多,由于网络原因导致配置中心宕机,会导致服务请求失败问题,所以可以配置多个configServer集群通过Eureka来实现高可用。
ConfigServer
新建三个configServer模块
配置application.yml
server:
port: 7777 #7778/7779
spring:
application:
name: fxtahe-cloud-configServer #不做修改
cloud:
config:
server:
git:
uri: https://github.com/fxtahe/cloud-config.git
username: #密码
password: #账号
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #单机模式
instance:
instance-id: configServer01:7777 #configServer02:7778/configServer03:7779
prefer-ip-address: true
创建主启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
Eureka注册中心
创建Eureka模块添加配置
server:
port: 7001
eureka:
instance:
hostname: eureka01
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: true #是否开启保护模式
eviction-interval-timer-in-ms: 60000 #服务注册表清理时间
创建启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaConfigApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConfigApplication.class,args);
}
}
消费者
新建bootstrap.yml,是读取配置的主要配置文件
spring:
cloud:
config:
#uri: http://configserver.com:7777
label: master
profile: dev
name: application-cloud-provider-client #加载的配置文件名
discovery:
enabled: true
service-id: fxtahe-cloud-configServer #服务id
eureka:
client:
service-url:
defaultZone: http://eureka01:7001/eureka/
启动类
@SpringBootApplication
@EnableEurekaClient
public class DeptProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DeptProviderApplication.class,args);
}
}
动态刷新配置
当修改配置文件后,如何动态刷新服务配置。SpringCloud提供手动刷新,修改git上配置后,可以向被修改配置的应用发送post请求刷新应用配置。
为configClient应用添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置application文件,关闭权限校验,否则会出现没有认证的情况
management:
security:
enabled: false
在controller上添加@RefreshScope
注解
启动项目,修改git配置文件信息。发送post请求
curl -X Post http://localhost:8105/refresh
可以观察到控制台信息更新
批量刷新与自动刷新
手动刷新在微服务数量较少的情况还能接受,但是在数量越来越多手动刷新配置便过于繁琐,而且每次都进行手动刷新也令人烦扰。如何实现批量刷新和自动刷新变成了一个待解决的问题。
SpringCloud BUS
SpringCloud总线将分布式系统的节点与轻量级消息代理链接起来,然后通过消息代理广播实现配置修改或其他管理命令。
架构1
在修改应用配置后,刷新其中一个节点的配置,然后再通过消息总线发布消息,其他应用接收消息实现自身应用配置的刷新。
架构2
上面的架构虽然可行,但是这里违背了微服务应用的设计初衷,微服务不应掺杂其他业务逻辑,比如刷新配置消息,违背了微服务的单一职责性,破坏了微服务各节点间的对等性。
这个架构在上面进行改进,通过ConfigServer去发布消息,保证微服务的单一职责和对等性。
实现批量刷新
- 架构1
分别为配置client添加依赖和配置
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
client配置添加在bootstrap文件中
spring:
rabbitmq:
host: localhost
port: 5672
username: root
password: root
更新git配置文件,向其中一个应用发送post更新请求,会发现所有应用都更新了。
- 架构2
在架构1基础上为ConfigServer添加配置,让其充当应用更新消息的发起者。
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
添加配置
spring:
rabbitmq:
host: localhost
port: 5672
username: root
password: root
management: #暴露refresh
security:
enabled: false
向configServer发送post请求,实现所有应用的刷新
curl -X http://localhost:7777/bus/refresh
自动刷新
在Git仓库配置webhooks,在更新git仓库向配置中心发送post请求实现自动刷新。