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客戶端使用指南