随着程序功能的日益复杂,程序的配置日益增多:各类功能的开关、参数的配置、服务器的地址等等。html
对程序配置的指望值也愈来愈高:配置修改后实时生效,灰度发布,分环境、分集群管理配置,完善的权限、审核机制等等。java
在这样的大环境下,传统的经过配置文件、数据库等方式已经愈来愈没法知足开发人员对配置管理的需求。mysql
Apollo 配置中心应运而生!Apollo - 一个可靠的配置管理系统。git
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,可以集中化管理应用不一样环境、不一样集群的配置,配置修改后可以实时推送到应用端,而且具有规范的权限、流程治理等特性,适用于微服务配置管理场景。服务端基于 Spring Boot 和 Spring Cloud 开发,打包后能够直接运行,不须要额外安装 Tomcat 等应用容器。github
Apollo 支持 4 个维度管理 Key-Value 格式的配置:web
同时,Apollo 基于开源模式开发,开源地址:https://github.com/ctripcorp/...spring
官网文档:https://github.com/ctripcorp/...sql
演示环境(Demo):shell
上图是Apollo配置中心中一个项目的配置首页数据库
application (应用)
environment (环境)
cluster (集群)
namespace (命名空间)
统一管理不一样环境、不一样集群的配置
配置修改实时生效(热发布)
版本发布管理
灰度发布
权限管理、发布审核、操做审计
客户端配置信息监控
提供Java和.Net原生客户端
提供开放平台API
部署简单
官方文档:https://github.com/ctripcorp/apollo/wiki/Apollo配置中心设计
上图简要描述了 Apollo 的整体设计,咱们能够从下往上看:
提供配置更新推送接口(基于Http long polling)
上图简要描述了配置发布的大体过程:
上图简要描述了Apollo客户端的实现原理:
客户端还会定时从Apollo配置中心服务端拉取应用的最新配置。
apollo.refreshInterval
来覆盖,单位为分钟。客户端会把从服务端获取到的配置在本地文件系统缓存一份
点击连接观看:Apollo 搭建服务端视频(获取更多请关注公众号「哈喽沃德先生」)
因为须要同时启动服务端和客户端,因此建议安装Java 1.8+。
Apollo的表结构对timestamp
使用了多个default声明,因此须要5.6.5以上版本。
Apollo 给咱们准备好了一个Quick Start安装包,你们只须要下载到本地,就能够直接使用,免去了编译、打包过程。
安装包共50M,若是访问github网速不给力的话,能够从百度网盘下载。
从Github下载
因为Quick Start项目比较大,因此放在了另外的repository,请注意项目地址
从百度网盘下载
为啥安装包要58M这么大?
Apollo 服务端共须要两个数据库:ApolloPortalDB
和ApolloConfigDB
,咱们把数据库、表的建立和样例数据都分别准备了 sql 文件,只须要导入数据库便可。
注意:若是你本地已经建立过Apollo数据库,请注意备份数据。咱们准备的sql文件会清空Apollo相关的表。
经过各类MySQL客户端导入sql/apolloportaldb.sql便可。
经过各类MySQL客户端导入sql/apolloconfigdb.sql便可。
Apollo 服务端须要知道如何链接到你前面建立的数据库,因此须要编辑demo.sh,修改 ApolloPortalDB 和 ApolloConfigDB 相关的数据库链接串信息。
注意:填入的用户须要具有对 ApolloPortalDB 和 ApolloConfigDB 数据的读写权限。
#apollo config db info apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 apollo_config_db_username=用户名 apollo_config_db_password=密码(若是没有密码,留空便可) # apollo portal db info apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8 apollo_portal_db_username=用户名 apollo_portal_db_password=密码(若是没有密码,留空便可)
注意:不要修改 demo.sh 的其它部分
Quick Start脚本会在本地启动3个服务,分别使用8070, 8080, 8090端口,请确保这3个端口当前没有被使用。
./demo.sh start
Apollo 提供的脚本文件为 .sh
文件,若是你的安装环境是在 Linux 系统下直接运行以上命令便可,若是你想在 Windows 环境下运行该脚本,先安装 Git 而后在 demo.sh
所在目录下鼠标右键点击 Git Bash Here
,而后再经过以上命令运行脚本便可。
当看到以下输出后,就说明启动成功了!
==== starting service ==== Service logging file is ./service/apollo-service.log Started [10768] Waiting for config service startup....... Config service started. You may visit http://localhost:8080 for service status now! Waiting for admin service startup.... Admin service started ==== starting portal ==== Portal logging file is ./portal/apollo-portal.log Started [10846] Waiting for portal startup...... Portal started. You can visit http://localhost:8070 now!
若是启动遇到了异常,能够分别查看 service 和 portal 目录下的 log 文件排查问题。
注:在启动 apollo-configservice 的过程当中会在日志中输出 eureka 注册失败的信息,如
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused
。须要注意的是,这个是预期的状况,由于 apollo-configservice 须要向 Meta Server(它本身)注册服务,可是由于在启动过程当中,本身还没起来,因此会报这个错。后面会进行重试的动做,因此等本身服务起来后就会注册正常了。
访问:http://localhost:8070/ Quick Start 集成了 Spring Security,输入用户名 apollo,密码 admin 后登陆。
登陆成功后,首页以下,Apollo 还提供了一个 SampleApp
样本案例供咱们学习使用。
点击对应按钮建立项目。
这里先经过默认的样例部门演示(后面我会讲如何添加部门),AppId 对应客户端配置文件中 app.id。
建立成功以下图。
点击连接观看:Apollo 客户端接入服务端视频(获取更多请关注公众号「哈喽沃德先生」)
下面经过最经常使用、便捷的方式,即基于 Spring Boot 的集成方式来接入服务端。
apollo-demo
聚合工程。Spring Boot 2.2.4.RELEASE
order-service
:订单服务,端口 9090
order-service02
:订单服务,端口 9091
<!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-client --> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.6.0</version> </dependency>
order-service
和 order-service02
的配置信息除端口外一致。
server: port: 9090 # 端口 spring: application: name: order-service # 应用名称 # apollo 相关配置 app: id: order-service # 与 Apollo 配置中心中的 AppId 一致 apollo: meta: http://localhost:8080 # Apollo 中的 Eureka 注册中心地址 #cluster: # 指定 Apollo 集群,相同集群实例使用对应集群的配置 #cacheDir: # 配置缓存目录,网络不可用时任然可提供配置服务 bootstrap: enable: true # 启用 apollo env: DEV # 指定环境 # 自定义配置 name: order-service-dev mysql: host: localhost port: 3306 username: root password: root
order-service
和 order-service02
的配置文件实体类代码一致。
package com.example.config; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component @Data @NoArgsConstructor @AllArgsConstructor public class ConfigProperties { @Value("${name}") private String name; @Value("${mysql.host}") private String mysqlHost; @Value("${mysql.port}") private Integer mysqlPort; @Value("${mysql.username}") private String mysqlUsername; @Value("${mysql.password}") private String mysqlPassword; }
order-service
和 order-service02
的控制层代码一致。
package com.example.controller; import com.example.config.ConfigProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController public class ConfigController { @Autowired private ConfigProperties configProperties; @Value("${name}") private String name; @GetMapping("/name") public String getName() { return configProperties.getName(); } @GetMapping("/mysql") public Map<Object, Object> getMySQLProperties() { // JDK9中的新特性,快速建立只读集合。 return Map.of("host", configProperties.getMysqlHost(), "port", configProperties.getMysqlPort(), "username", configProperties.getMysqlUsername(), "password", configProperties.getMysqlPassword()); } }
启动类须要添加 @EnableApolloConfig
注解。
order-service
和 order-service02
的启动类代码一致。
package com.example; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableApolloConfig @SpringBootApplication public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } }
访问:http://localhost:9090/name 和 http://localhost:9091/name 结果以下:
访问:http://localhost:9090/mysql 和 http://localhost:9091/mysql 结果以下:
进入项目后点击右上角的 新增配置
。
添加配置项 name
、mysql.username
、mysql.password
。
将刚才添加的配置信息批量发布至应用。
控制台打印信息以下:
c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: order-service-dev-2.0, key: name, beanName: configController, field: com.example.controller.ConfigController.name c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: root123, key: mysql.password, beanName: configProperties, field: com.example.config.ConfigProperties.mysqlPassword c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: root123, key: mysql.username, beanName: configProperties, field: com.example.config.ConfigProperties.mysqlUsername
访问:http://localhost:9090/name 和 http://localhost:9091/name 结果以下:
访问:http://localhost:9090/mysql 和 http://localhost:9091/mysql 结果以下:
以上只是 Apollo 的入门教程,后面咱们会学习 Apollo 的更多高级玩法,好比多环境部署,高可用环境搭建等等。下一篇咱们讲解 Apollo 部门管理、用户管理、配置管理、集群管理,记得关注噢~
本文采用 知识共享「署名-非商业性使用-禁止演绎 4.0 国际」许可协议
。
你们能够经过 分类
查看更多关于 Spring Cloud
的文章。
? 您的点赞
和转发
是对我最大的支持。
? 扫码关注 哈喽沃德先生
「文档 + 视频」每篇文章都配有专门视频讲解,学习更轻松噢 ~