代码地址:
https://gitee.com/xuhx615/apollo-demo.git
🍓目前市面上比较多的配置中心
- ⭐
Disconf百度开源的配置管理中心 - ⭐
Spring Cloud Config - ⭐
Apollo携程开源 - ⭐
Nacos
🍓Apollo组件
- ⭐
Config Service:提供配置的读取、推送等功能,服务对象是Apollo客户端 - ⭐
Admin Service:提供配置的修改、发布等功能,服务对象是Apollo Portal(管理界面) - ⭐
Config Service和Admin Service都是多实例、无状态部署,所以需要将自己注册到Eureka中并保持心跳 - ⭐在
Eureka之上我们架了一层Meta Server用于封装Eureka的服务发现接口 - ⭐
Client通过域名访问Meta Server获取Config Service服务列表(IP+Port),而后直接通过IP+Port访问服务,同时在Client侧会做load balance、错误重试 - ⭐
Portal通过域名访问Meta Server获取Admin Service服务列表(IP+Port),而后直接通过IP+Port访问服务,同时在Portal侧会做load balance、错误重试 - ⭐为了简化部署,我们实际上会把
Config Service、Eureka和Meta Server三个逻辑角色部署在同一个JVM进程中
🍓Apollo特性
- ⭐统一管理不同环境、不同集群的配置
- 📌
Apollo提供了一个统一界面集中式管理不同环境(environment)、不同集群(cluster)、不同命名空间(namespace)的配置。 - 📌同一份代码部署在不同的集群,可以有不同的配置,比如
zk的地址等 - 📌通过命名空间(
namespace)可以很方便的支持多个不同应用共享同一份配置,同时还允许应用对共享的配置进行覆盖 - 📌配置界面支持多语言(中文,
English)
- 📌
- ⭐配置修改实时生效(热发布)
用户在Apollo修改完配置并发布后,客户端能实时(1秒)接收到最新的配置,并通知到应用程序。 - ⭐版本发布管理
所有的配置发布都有版本概念,从而可以方便的支持配置的回滚。 - ⭐灰度发布
支持配置的灰度发布,比如点了发布后,只对部分应用实例生效,等观察一段时间没问题后再推给所有应用实例。 - ⭐权限管理、发布审核、操作审计
- 📌应用和配置的管理都有完善的权限管理机制,对配置的管理还分为了编辑和发布两个环节,从而减少人为的错误。
- 📌所有的操作都有审计日志,可以方便的追踪问题。
- ⭐客户端配置信息监控
可以方便的看到配置在被哪些实例使用 - ⭐提供
java和.net原生客户端 - ⭐提供开发平台
API
🍓Apollo服务端安装
- ⭐官方文档
https://www.apolloconfig.com/#/zh/README - ⭐环境
- 📌
Java 1.8+ - 📌
MySQL 5.6.5+
- 📌
- ⭐下载
apollo-quick-start-2.1.0.zip,解压 - ⭐执行
/sql下面的sql到数据库 - ⭐修改
demo.sh脚本里面的mysql配置 - ⭐
Quick Start脚本会在本地启动3个服务,分别使用8070, 8080, 8090端口,请确保这3个端口当前没有被使用- 📌
8070 Portal管理控制台 - 📌
8080 Meta Server,Eureka,Config Service - 📌
8090 Admin Service
- 📌
- ⭐执行启动脚本
./demo.sh start - ⭐页面打开,端口
8070 apollo/admin
http://192.168.113.102:8070/ - ⭐执行关闭脚本
./demo.sh stop
🍓部署架构
-
⭐部署架构图

-
⭐单机单环境

-
⭐单机双环境

-
⭐高可用双环境

🍓核心概念
- ⭐
application应用 - ⭐
environment环境 - ⭐
cluster集群 - ⭐
namespace命名空间
🍓客户端连接Apollo
-
⭐依赖添加
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.1.0</version> </dependency> -
⭐
vm启动参数-Dapp.id=ApolloDemo -Denv=DEV -Dapollo.cluster=beijing -Ddev_meta=http://192.168.113.102:8080 app.id:应用ID env:环境 apollo.cluster:集群(不指定,默认读取default集群配置) dev_meta:apollo-config地址 -
⭐配置读取
/** * 读取默认namespace=application下面的配置信息 */ @Test public void readDefaultNameSpaceConfig() { Config config = ConfigService.getAppConfig(); //param1:configKey param2:defaultValue String val = config.getProperty("doc.test", ""); System.out.println(val); } /** * 读取指定namespace下面的配置信息 */ @Test public void readAppointNameSpaceConfig() { Config config = ConfigService.getConfig("dba"); String val = config.getProperty("spring.datasource.name", ""); System.out.println(val); } /** * 读取指定公共namespace下面的配置信息 */ @Test public void readPublicNameSpaceConfig() { Config config = ConfigService.getConfig("XUHAIXIANG.common"); String val = config.getProperty("name", ""); System.out.println(val); } /** * 读取指定集群下面的配置信息 * -Dapollo.cluster=beijing */ @Test public void readAppointClusterConfig() { Config config = ConfigService.getConfig("dba"); String val = config.getProperty("spring.datasource.name", ""); System.out.println(val); } -
⭐整合
SpringBoot#集成apollo #应用ID app.id = ApolloDemo #允许接入apollo apollo.bootstrap.enabled = true #名称空间 apollo.bootstrap.namespaces = application,dba,XUHAIXIANG.common #config apollo.meta = http://192.168.113.102:8080 #集群 apollo.cluster = beijing #环境 env = DEV #apollo缓存路径 apollo.cacheDir = classpath:/apollo/cache/DEV #Spring配置自动更新 apollo.autoUpdateInjectedSpringProperties=trueimport com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * SpringBoot启动器 */ @SpringBootApplication @EnableApolloConfig //允许使用Apollo配置 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
🍓配置发布原理

- ⭐用户在
Portal操作配置发布 - ⭐
Portal调用Admin Service的接口操作发布 - ⭐
Admin Service发布配置后,发送ReleaseMessage给各个Config Service - ⭐
Config Service收到ReleaseMessage后,通知对应的客户端