一、概述
二、Config
三、Refresh
四、配置中心服务化
五、基于Webhook和消息总线的解决方案
一、概述
SpringCloud配置中心包括Config和Bus两个组成部分,只要这样,才能保证主动推送。
下面主要分为四个部分,
Config讲了基本的配置中心,但这样如果修改配置客户端在运行中是无法改变的。
Refresh讲了我们修改配置文件后可以向客户端发送一个POST请求,可以利用WebHook的方式。
配置中心服务化讲了我们把配置中心独立成一个服务(最好多挂几个)挂到Eureka
基于Webhook和消息总线的解决方案,让我们客户端可以实时的感知配置变化,从而做出改边。
我们先看Config,然后集成Bus来达到我们配置中心的目的。
二、Config
1.Config概述
当一个系统配置文件修改,我们需要重启系统,但是微服务状态下系统数量太多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
引入spring cloud config后,我们的外部配置文件就可以集中放置在一个git仓库里,再新建一个config server,用来管理所有的配置文件,维护的时候需要更改配置时,只需要在本地更改后,推送到远程仓库,所有的服务实例都可以通过config server来获取配置文件,这时每个服务实例就相当于配置服务的客户端config client,为了保证系统的稳定,配置服务端config server可以进行集群部署,即使某一个实例,因为某种原因不能提供服务,也还有其他的实例保证服务的继续进行。
2.示例
⑴server
①添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
②配置文件
server:
port: 8001
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/cGitHub123/Springcloud.git # 配置git仓库的地址
search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
username: # git仓库的账号
password: # git仓库的密码
Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。
③启动类
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
④测试
直接访问:http://localhost:8001/neo-config/dev
返回值:
{
"name": "neo-config",
"profiles": [
"dev"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "https://github.com/cGitHub123/Springcloud.git/config-repo/neo-config-dev.properties",
"source": {