Apollo(阿波罗)是携程框架部门研发的分布式配置中心,可以集中化管理应用不一样环境、不一样集群的配置,配置修改后可以实时推送到应用端,而且具有规范的权限、流程治理等特性,适用于微服务配置管理场景。
github地址为:
https://github.com/ctripcorp/apollo
该项目提供了两种部署方式:本地部署和分布式部署。生产环境建议使用“分布式部署”。
因最近项目有使用配置中心的需求,在综合分析了apollo、Qconf、SpringCloud Config等一系列分布式配置中心后,初步选定apollo。
官方提供的分布式部署架构适合大规模集群环境。在其整体架构基础上作了精简,力求先跑起来,给开发部门提供环境,测试。html
主机名称 | IP | 备注 |
---|---|---|
hadoop02.ok.com | 10.150.27.65 | Portalserver/Configserver/adminserver and MySQL |
APPCAN-T-APP-6 | 10.150.27.65 | Configserver/adminserver and MySQL |
软件版本:
JAVA:java
[root@appcan-t-app-7 scripts]# java -version java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
MAVEN:mysql
[root@appcan-t-app-7 scripts]# mvn -version Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00) Maven home: /usr/local/apache-maven-3.5.3 Java version: 1.8.0_60, vendor: Oracle Corporation Java home: /usr/local/jdk1.8.0_60/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-327.el7.x86_64", arch: "amd64", family: "unix"
MySQL:linux
[root@appcan-t-app-7 scripts]# mysql -uroot -p mysql> select version(); +---------------+ | version() | +---------------+ | 5.7.19-17-log | +---------------+ 1 row in set (0.01 sec)
架构图以下:git
说明:github
clone https://github.com/ctripcorp/apollo.git
Apollo服务端共须要两个数据库:ApolloPortalDB和ApolloConfigDB,数据库、表的建立和样例数据的sql文件在项目当中(/home/ok/apollo/scripts/sql),只须要导入数据库便可。
须要注意的是ApolloPortalDB只须要部署一个便可,而ApolloConfigDB须要在每一个环境部署一套,如本案例中dev和pro分别部署两套ApolloConfigDB。sql
导入sql文件:数据库
mysql> source /home/ok/apollo/scripts/sql/apolloportaldb.sql;
验证:apache
mysql> select `Id`, `Key`, `Value`, `Comment` from `ApolloPortalDB`.`ServerConfig` limit 1; +----+--------------------+---------+--------------------------+ | Id | Key | Value | Comment | +----+--------------------+---------+--------------------------+ | 1 | apollo.portal.envs | dev | 可支持的环境列表 | +----+--------------------+---------+--------------------------+ 1 row in set (0.00 sec)
导入sql文件:服务器
mysql> source /home/ok/apollo/scripts/sql/apolloconfigdb.sql;
验证:
mysql> select `Id`, `Key`, `Value`, `Comment` from `ApolloConfigDB`.`ServerConfig` limit 1; +----+--------------------+-------------------------------+------------------------------------------------------+ | Id | Key | Value | Comment | +----+--------------------+-------------------------------+------------------------------------------------------+ | 1 | eureka.service.url | http://localhost:8080/eureka/ | Eureka服务Url,多个service以英文逗号分隔 | +----+--------------------+-------------------------------+------------------------------------------------------+ 1 row in set (0.00 sec)
5.1调整ApolloPortalDB配置
apollo.portal.envs - 可支持的环境列表
默认值是dev,本例须要增长pro环境。
首先在数据库apolloportaldb-serverconfig-apollo.portal.envs内新增pro。
mysql> select `Id`, `Key`, `Value`, `Comment` from `ApolloPortalDB`.`ServerConfig` limit 1; +----+--------------------+---------+--------------------------+ | Id | Key | Value | Comment | +----+--------------------+---------+--------------------------+ | 1 | apollo.portal.envs | pro,dev | 可支持的环境列表 | +----+--------------------+---------+--------------------------+ 1 row in set (0.00 sec)
其次配合修改/home/ok/apollo/scripts/build.sh文件才能生效:
#meta server url, different environments should have different meta server addresses pro_meta=http://10.150.27.65:8080 dev_meta=http://10.160.27.67:8080 META_SERVERS_OPTS="-Ddev_meta=$dev_meta -Dpro_meta=$pro_meta"
organizations - 部门列表
在数据库apolloportaldb-serverconfig-proorganizations内修改:
mysql> select `Id`, `Key`, `Value`, `Comment` from `ApolloPortalDB`.`ServerConfig` where Id=2; +----+---------------+------------------------------------------------------------------------------------+--------------+ | Id | Key | Value | Comment | +----+---------------+------------------------------------------------------------------------------------+--------------+ | 2 | organizations | [{"orgId":"BACKEND","orgName":"JAVA"},{"orgId":"TEST2","orgName":"样例部门2"}] | 部门列表 | +----+---------------+------------------------------------------------------------------------------------+--------------+ 1 row in set (0.00 sec)
5.2调整ApolloConfigDB配置
配置项统一存储在ApolloConfigDB.ServerConfig表中,须要注意每一个环境的ApolloConfigDB.ServerConfig都须要单独配置。
eureka.service.url - Eureka服务Url
每一个环境只填入本身环境的eureka服务地址.
在DEV环境的ApolloConfigDB.ServerConfig表中设置eureka.service.url为:http://localhost:8080/eureka/
在PRO环境的ApolloConfigDB.ServerConfig表中设置eureka.service.url为:http://localhost:8080/eureka/
编辑/home/ok/apollo/scripts/build.sh文件,修改ApolloPortalDB和ApolloConfigDB相关的数据库链接串信息。
10.150.27.65:
#apollo config db info apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 apollo_config_db_username=root apollo_config_db_password=bobo365 #apollo portal db info apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8 apollo_portal_db_username=root apollo_portal_db_password=bobo365
10.150.27.67:
#apollo config db info apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 apollo_config_db_username=root apollo_config_db_password=bobo365 #apollo portal db info apollo_portal_db_url=jdbc:mysql://10.160.27.65:3306/ApolloPortalDB?characterEncoding=utf8 apollo_portal_db_username=root apollo_portal_db_password=bobo365
Portal和Apollo客户端须要在不一样的环境访问不一样的meta service(apollo-configservice)地址,因此须要在打包时提供这些信息。
#meta server url, different environments should have different meta server addresses pro_meta=http://10.150.27.65:8080 dev_meta=http://10.160.27.67:8080 META_SERVERS_OPTS="-Ddev_meta=$dev_meta -Dpro_meta=$pro_meta"
执行脚本:/home/ok/apollo/scripts/build.sh
该脚本会依次打包apollo-configservice, apollo-adminservice, apollo-portal和apollo-client。
将apollo-configservice/target/目录下的apollo-configservice-x.x.x-github.zip上传到服务器上,解压后执行scripts/startup.sh便可。如需中止服务,执行scripts/shutdown.sh.
[root@appcan-t-app-7 target]# pwd /home/ok/apollo/apollo-configservice/target [root@appcan-t-app-7 target]# unzip apollo-configservice-0.9.1-SNAPSHOT-github.zip -d test
将apollo-adminservice/target/目录下的apollo-adminservice-x.x.x-github.zip上传到服务器上,解压后执行scripts/startup.sh便可。如需中止服务,执行scripts/shutdown.sh.
[root@appcan-t-app-7 target]# pwd /home/ok/apollo/apollo-adminservice/target [root@appcan-t-app-7 target]# unzip apollo-adminservice-0.9.1-SNAPSHOT-github.zip -d test
将apollo-portal/target/目录下的apollo-portal-x.x.x-github.zip上传到服务器上,解压后执行scripts/startup.sh便可。如需中止服务,执行scripts/shutdown.sh.
apollo-portal的默认端口是8080,和apollo-configservice一致,因此若是须要在一台机器上同时启动apollo-portal和apollo-configservice的话,须要修改apollo-portal的端口。直接修改startup.sh中的SERVER_PORT便可,如SERVER_PORT=8070。
[root@appcan-t-app-7 target]# pwd /home/ok/apollo/apollo-portal/target [root@appcan-t-app-7 target]# unzip apollo-portal-0.9.1-SNAPSHOT-github.zip -d test
登陆系统后在 http://{portal地址}/user-manage.html 页面添加用户,只有超级管理员才能添加用户, 不然会报403错误。
系统默认帐号:apollo/admin.
Eureka(10.150.27.67)页面以下:
Eureka(10.150.27.65)页面以下:
Portal页面以下:
以上仅仅是服务端的搭建和部署,真正使用须要结合客户端使用。以下连接为Java客户端使用指南,供后续研究。
JAVA客户端使用指南