分布式系统中面临的配置问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

SpringCloud提供了ConfigServer来解决这个问题—我们每一个微服务自己带着一个application.yml,项目中可能会有几十个上百个配置文件。

Spring Cloud Config 分布式配置中心实战详解_SpringCloudConfig

如上图所示,SpringCloudConfig为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。ConfigServer从SVN/Git/本地文件系统中获取配置文件,然后(手动或自动)推送给Client,当然Client也可以自己从ConfigServer拉取。


SpringCloudConfig分为服务端和客户端两个部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。故而服务端作为一个微服务应用在设计高可用时可以使用Eureka注册中心注册多个ConfigServer构成ConfigServer集群。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
Spring Cloud Config 分布式配置中心实战详解_spring_02
SpringCloudConfig持久化方式

使用版本管理工具来进行配置持久化的优点:

  • 权限管理直接通过版本管理来控制
  • 责任人机制
  • 可以进行版本控制

SpringCloudConfig功能:

  • 集中管理配置文件;
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息;
  • 当配置发送变动时,服务不需要重启即可感知到配置的变化并应用新的配置;
  • 将配置信息以REST接口的形式暴露。

中文文档地址:​​https://springcloud.cc/spring-cloud-config.html​源码地址:​​https://github.com/spring-cloud/spring-cloud-config​


【1】模拟运维人员操作

首先模拟运维人员将其本地库中的配置文件推送到远程GitHub上面。

① 在GitHub上建立仓库microservicecloud-config

其Git地址:

HTTPS协议:​​https://github.com/JanusJ/microservicecloud-config.git​

SSH协议:git@github.com:JanusJ/microservicecloud-config.git


② 在本地建立文件夹springcloud,进入git bash 从远程clone到本地

$ git clone https://github.com/JanusJ/microservicecloud-config.git

Spring Cloud Config 分布式配置中心实战详解_git_03
Spring Cloud Config 分布式配置中心实战详解_git_04


③ 在本地库中建立application.yml并保存为UTF-8格式

spring:
profiles:
active:
- dev

---

spring:
profiles: dev # 开发环境
application:
name: microservicecloud-config-dev

---

spring:
profiles: test # 测试环境
application:
name: microservicecloud-config-test

④ 将本地库的yml推送到GitHub远程库

git status
git add application.yml
git commit -m "application.yml" application.yml
git push origin-cloud master

Spring Cloud Config 分布式配置中心实战详解_SpringCloudConfig_05


【2】建立服务配置中心Server

① 新建module-microservicecloud-config-3344

Spring Cloud Config 分布式配置中心实战详解_github_06


② 编辑application.yml文件

server: 
port: 3344

spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: https://github.com/JanusJ/microservicecloud-config.git #GitHub上面的git仓库名字

③ 修改pom文件

<dependencies>
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.10.0.201712302008-r</version>
</dependency>
<!-- 熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 图形化监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 热部署插件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

④ 修改主启动类

@SpringBootApplication
@EnableConfigServer //必不可少
public class Config_3344_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
}
}

⑤ 启动项目,进行测试获取远程GitHub中的yml

分别请求如下:

​http://localhost:3344/application-dev.yml​​​​http://localhost:3344/application-test.yml​

Spring Cloud Config 分布式配置中心实战详解_spring_07


如果请求一个不存在但是符合yml命名规范的资源,将会返回基本配置,如下所示:

Spring Cloud Config 分布式配置中心实战详解_git_08


HTTP服务具有以下格式的资源:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

其中​​“应用程序”​​​作为SpringApplication中的​​spring.config.name​​注入(即常规Spring Boot应用程序中通常为“应用程序”),“配置文件”是活动配置文件(或逗号分隔列表)的属性),“label”是可选的git标签(默认为“master”)。

至此,成功实现了SpringCloud从远程GitHub上获取yml配置信息。


【3】Config客户端配置

首先模拟运维人员操作,在本地创建客户端配置yml,然后推送到GitHub上面。

microservicecloud-config-client.yml 如下:

spring:
profiles:
active:
- dev

---
server:
port: 8201

spring:
profiles: dev
application:
name: microservicecloud-config-client

eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8202

spring:
profiles: test
application:
name: microservicecloud-config-client

eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/

Spring Cloud Config 分布式配置中心实战详解_github_09


其次建立/microservicecloud-config-client-3355 Module

Spring Cloud Config 分布式配置中心实战详解_git_10


① application.yml文件如下

spring:
application:
name: microservicecloud-config-client

② bootstrap.yml文件如下

spring:
cloud:
config:
name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
profile: test #本次访问的配置项
label: master
uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

这里说明一下什么是bootstap.yml,其与application.yml有什么区别。

application.yml是用户级的资源配置项,bootstrap.yml是系统级的,优先级更高。

SpringCloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。初始化的时候Bootstrap Context负责从外部源加载配置并解析配置。这两个上下文共享一个 从外部获取的Environment。Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap Context和Application Context有着不同的约定。

所以新增了一个bootstrap.yml,保证了Bootstrap Context 和Application Context 配置的分离。


③ 新建REST类,验证能否从GitHub上面读取配置

@RestController
public class ConfigClientRest
{

@Value("${spring.application.name}")
private String applicationName;

@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServers;

@Value("${server.port}")
private String port;

@RequestMapping("/config")
public String getConfig()
{
String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
System.out.println("******str: " + str);
return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
}
}

④ 主程序类如下

@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);
}
}

⑤ 启动3344和3355两个项目进行测试

请求http://config-3344.com:3344/application-dev.yml如下:
Spring Cloud Config 分布式配置中心实战详解_SpringCloudConfig_11
请求http://config-3344.com:3344/microservicecloud-config-client-dev.yml如下:
Spring Cloud Config 分布式配置中心实战详解_SpringCloudConfig_12

针对config-client项目的bootstrap.yml配置,请求测试如下:

​http://client-config.com:8201/config​

Spring Cloud Config 分布式配置中心实战详解_spring_13
​​​http://client-config.com:8202/config​​​Spring Cloud Config 分布式配置中心实战详解_git_14

至此成功实现了客户端/microservicecloud-config-client-3355访问SpringCloud Config 3344从远程仓库GitHub上获取配置信息。


【4】Config配置实战

根据上面步骤,我们的Config服务端配置ok且测试通过,可以利用config+github进行配置修改并获取内容。

接下来做一个eureka服务和一个服务提供者dept访问的微服务,两个微服务的配置统一由GitHub获得。实现统一配置,分布式管理,完成多环境的变更。

① 在本地库创建microservicecloud-config-eureka-client.yml和microservicecloud-config-dept-client.yml并推送到GitHub

microservicecloud-config-eureka-client.yml内容如下:

spring:
profiles:
active:
- dev
---
server:
port: 7001

spring:
profiles: dev
application:
name: microservicecloud-config-eureka-client

eureka:
instance:
hostname: eureka7001.com # eureka服务端的实例名称
# prefer-ip-address: true
client:
register-with-eureka: false #false 表示不向注册中心注册自己
fetch-registry: false # false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址 查询服务和注册服务都需要依赖这个地址

---
server:
port: 7001

spring:
profiles: test
application:
name: microservicecloud-config-eureka-client

eureka:
instance:
hostname: eureka7001.com # eureka服务端的实例名称
# prefer-ip-address: true
client:
register-with-eureka: false #false 表示不向注册中心注册自己
fetch-registry: false # false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址 查询服务和注册服务都需要依赖这个地址

microservicecloud-config-dept-client.yml内容如下:

spring:
profiles:
active:
- dev
---
server:
port: 8001

mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.web.springcloud.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件

spring:
profiles: dev
application:
name: microservicecloud-config-dept-client
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
# driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/clouddb01 # 数据库名称
username: root
password: 123456
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500


eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: microservicecloud-dept8001 # 自定义服务实例Id
prefer-ip-address: true #访问路径可以显示IP地址

# http://192.168.2.100:8001/info优化显示
info:
app.name: web-microservicecloud-config-dept-config01
company.name: www.web.com
build.artifactId: $project.artifactId$
build.version: $project.version$
---
server:
port: 8001

mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.web.springcloud.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件

spring:
profiles: test
application:
name: microservicecloud-config-dept-client
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
# driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/clouddb02 # 数据库名称
username: root
password: 123456
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500


eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: microservicecloud-dept8001 # 自定义服务实例Id
prefer-ip-address: true #访问路径可以显示IP地址

# http://192.168.2.100:8001/info优化显示
info:
app.name: web-microservicecloud-config-dept-config02
company.name: www.web.com
build.artifactId: $project.artifactId$
build.version: $project.version$

文件编码格式记得保存为UTF-8。
Spring Cloud Config 分布式配置中心实战详解_spring_15
Spring Cloud Config 分布式配置中心实战详解_git_16


② 参考/microservicecloud-eureka-7001新建Module /microservicecloud-config-eureka-client-7001

Spring Cloud Config 分布式配置中心实战详解_spring_17

application.yml文件如下:

spring:
application:
name: microservicecloud-config-eureka-client

bootstrap.yml文件如下:

spring: 
cloud:
config:
name: microservicecloud-config-eureka-client #需要从github上读取的资源名称,注意没有yml后缀名
profile: dev
label: master
uri: http://config-3344.com:3344 #SpringCloudConfig获取的服务地址

pom文件如下:

<dependencies>
<!-- SpringCloudConfig配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 热部署插件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

主启动类如下:

/**
* EurekaServer服务器端启动类,接受其它微服务注册进来
*
*/
@SpringBootApplication
@EnableEurekaServer
public class Config_Git_EurekaServerApplication
{
public static void main(String[] args)
{
SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
}
}

③ 参考/microservicecloud-provider-dept-8001新建Module /microservicecloud-config-provider-dept-client-8001

Spring Cloud Config 分布式配置中心实战详解_spring_18
application.yml文件如下:

spring:
application:
name: microservicecloud-config-dept-client

bootstrap.yml文件如下:

spring:
cloud:
config:
name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名
#profile配置是什么就取什么配置dev or test
profile: dev
#profile: test
label: master
uri: http://config-3344.com:3344 #SpringCloudConfig获取的服务地址

pom文件如下:

<dependencies>
<!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<dependency>
<groupId>com.web.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- spring cloud config-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

④ 测试eureka7001

首先启动/microservicecloud-config-3344,然后启动/microservicecloud-config-eureka-client-7001,访问http://eureka7001.com:7001/。

如下所示,表明eureka-client-7001通过microservicecloud-config-3344从远程GitHub上获取到了配置信息并正常启动:

Spring Cloud Config 分布式配置中心实战详解_SpringCloudConfig_19


⑤ 测试/microservicecloud-config-provider-dept-client-8001

其④的基础上启动/microservicecloud-config-provider-dept-client-8001。

访问http://eureka7001.com:7001/如下所示:
Spring Cloud Config 分布式配置中心实战详解_spring_20

默认spring.profiles=dev,连接的数据库为clouddb01。访问http://localhost:8001/dept/list如下:

Spring Cloud Config 分布式配置中心实战详解_spring_21

将config-provider-dept-client项目的bootstrap.yml文件中 profile切换为test,再次测试如下:

Spring Cloud Config 分布式配置中心实战详解_spring_22

可以看到,此时连接的数据库为clouddb02,客户端实现了动态配置切换!!


⑥ 客户端动态配置切换正规玩法

正规玩法应该是运维工程师本地修改了配置(如切换了数据库),然后上传到远程GitHub。客户端项目在不做修改的情况下,自动使用新的配置新的数据库!!

  • 本地修改microservicecloud-config-dept-client.yml,将clouddb01切换为clouddb03
    Spring Cloud Config 分布式配置中心实战详解_github_23
  • 提交到本地库并推送到远程GitHub上

Spring Cloud Config 分布式配置中心实战详解_SpringCloudConfig_24

  • 发送POST请求http://localhost:8001/refresh进行配置刷新(属于客户端手动刷新)
  • 客户端再次测试http://localhost:8001/dept/list

Spring Cloud Config 分布式配置中心实战详解_github_25


【5】Git配置修改与客户端获取最新配置

① Full authentication is required to access this resource

解决办法:

#忽略权限拦截
management:
security:
enabled: false

或者:

security:
basic:
enabled: false

pom中有监控依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

② refresh与restart

  • refresh
    post方式执行http://localhost:port/refresh 会刷新env中的配置
  • restart

如果配置信息已经注入到bean中,由于bean是单例的,不会去加载修改后的配置
需要通过post方式去执行http://localhost:prot/restart。

添加如下设置:

endpoints:
restart:
enabled: true

③ RefreshScope

该注解源码如下:

/**
* Convenience annotation to put a <code>@Bean</code> definition in
* {@link org.springframework.cloud.context.scope.refresh.RefreshScope refresh scope}.
* Beans annotated this way can be refreshed at runtime and any components that are using
* them will get a new instance on the next method call, fully initialized and injected
* with all dependencies.
*
* @author Dave Syer
*
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Scope("refresh")
@Documented
public @interface RefreshScope {
/**
* @see Scope#proxyMode()
*/
ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;

}

需要额外注意的是,该注解并不能很好的实现你想要的强大刷新功能!


④ 消息总线实现全节点的半自动刷新

之所以称之为半自动,是因为你需要手动发POST请求。

⑤ GitHub的Webhooks

Spring Cloud Config 分布式配置中心实战详解_github_26

综合第四步和第五步,实现真正的自动刷新!!


【5】SpringCloudConfig连接SVN

上面默认版本工具为Git,当然也支持SVN。
Spring Cloud Config 分布式配置中心实战详解_spring_27
客户端配置如下:
Spring Cloud Config 分布式配置中心实战详解_SpringCloudConfig_28


【6】SpringCloudConfig使用进阶

① SpringCloudConfig SVN配置文件管理(添加项目子目录)

如果配置文件有几十个上百个呢?当然需要使用目录来管理,如下图:
Spring Cloud Config 分布式配置中心实战详解_git_29


② 接口安全

如果不想任意Client都能拉取我们ConfigServer的配置文件,则可以结合SpringSecurity做接口安全。

如下所示,可以直接在代码里面写死(当然你可以在数据库里面配置):
Spring Cloud Config 分布式配置中心实战详解_spring_30
参考博文:​SpringBoot - 安全入门与SpringSecurity


③ 配置文件加密

如果不想配置文件是明文显示,那么可以对配置文件内容进行加密。

Spring Cloud Config 分布式配置中心实战详解_spring_31

准备工作做好之后,还有如下操作需要完成:

  • 使用encrypt、decrypt进行加密、解密操作;
  • 获取密文,配置到配置文件中,格式为{cipher}xxxxxxxxx;
  • 客户端无需更改,直接使用。

④ 动态刷新

配置文件修改后,客户端可以主动发送post请求刷新配置文件(这种方式可以实现灰度更新),也可以让服务端进行推送。其中服务端推送又分为手动推送和自动推送。

Spring Cloud Config 分布式配置中心实战详解_github_32

Bus(消息总线)+RabbitMQ架构图如下所示:
Spring Cloud Config 分布式配置中心实战详解_github_33


【7】如果非SpringCloud下可以使用哪些配置中心呢?

比如你使用的是Dubbo体系,那么市场上也是有不少国内开源的配置中心的,如阿里的Diamond,百度的Dis-conf以及携程的Apollo。

三者对比分析如下:

对比项目

Diamond

Dis-conf

Apollo

比较

介绍

Diamond是淘宝开源的一种分布式配置管理服务的实现。Diamond本质上是一个java写的web应用,其对外提供接口都是基于HTTP协议的。

Disconf是一套完整的基于ZK的分布式配置统一解决方案它支持配置(配置项+配置文件)的分布式化管理

Apollo是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景

社区活跃

活跃

活跃

活跃

都很活跃

依赖软件

tomcat mysql

tomcat mysql nginx redis zk

服务端基于SpringBoot和SpringCloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器,唯一依赖是mysql

disconf依赖软件比较多,环境搭建相对最简单的是Apollo

使用案例

淘宝内部绝大多数系统的配置,由diamond来进行统一管理

百度、滴滴出行、银联、网易、拉勾网、苏宁易购等

携程、泸江、平安银行

持久化村塾

mysql

mysql

mysql

都持久化到数据库,易于管理

同步模型

拉模型,默认每15秒拉一次全量数据

可才用拉模型,基于zk的推模型,有修改实时推送

默认拉模型,配合zk可以实现推模型

disconf基于分布式的zk实现,在时效性、易用性上均优于diamond Apollo

配置读写

支持实例对配置读写。支持某台实例写配置数据,并可以广播到其他实例上

只支持实例对配置读。通过在disconf-web上更新配置到达广播写到所有应用实例

只支持实例对配置读

从目前场景来看,实例对配置的写需求不是那么明显

容灾

多级容灾模式,配置数据在dump在本地,避免中心服务挂机时无法使用。每一级都有各自的本地存储

多级容灾模式,优先读取本地配置文件

多级容灾模式,优先读取本地配置文件

双方均支持在中心服务挂机时配置实例

配置形式

KV结构的数据

支持KV结构数据(配置项),也支持传统的配置文件

KV结构的数据

使用配置文件的方式可能与程序相似,更易于接受的使用