1. 程式人生 > >使用apollo管理多個環境的服務配置

使用apollo管理多個環境的服務配置

本文是對apollo管理多個服務(dev、sit、uat、prod)配置檔案的安裝與配置做了簡單的介紹

具體效果如下圖所示:
apollo使用展示

1,專案預部署(sit環境需要修改apollo程式碼,參見文件:Apollo新增自定義的環境

開發環境: dev
	configservice(包括jar包啟用的eureka):192.168.1.235:18080;
	adminservice:192.168.1.235:18090;

測試環境: sit
	configservice(包括jar包啟用的eureka):192.168.1.235:18081;
	adminservice:192.168.1.235:18091;

UAT環境: uat
	configservice(包括jar包啟用的eureka):192.168.1.235:18082;
	adminservice:192.168.1.235:18092;

portal服務: 192.168.1.235:18070;

2,環境準備

java : 1.8+

mysql : 5.6.5+

下載:https://github.com/ctripcorp/apollo/tree/master/scripts/sql目錄下的兩個sql檔案,

匯入三份apolloconfigdb.sql並命名為ApolloConfigDB , ApolloConfigDB_SIT , ApolloConfigDB_UAT,分別對應開發環境、測試環境、uat環境。

匯入一份apolloportaldb.sql做為客戶端啟動資料準備,可以管理三個環境的資料。

3,準備編譯程式碼

從以下路徑檢出主分支程式碼:https://github.com/ctripcorp/apollo。
修改scripts下的build.bat,修改資料庫配置資訊,和各環境配置
@echo off

rem apollo config db info
set apollo_config_db_url="jdbc:mysql://192.168.1.233:3306/ApolloConfigDB?characterEncoding=utf8"
set apollo_config_db_username="root"
set apollo_config_db_password="
[email protected]
" rem apollo portal db info set apollo_portal_db_url="jdbc:mysql://192.168.1.233:3306/ApolloPortalDB?characterEncoding=utf8" set apollo_portal_db_username="root" set apollo_portal_db_password="[email protected]" rem meta server url, different environments should have different meta server addresses set dev_meta="http://192.168.1.235:8080" set fat_meta="http://192.168.1.235:8081" set uat_meta="http://192.168.1.235:8082" set META_SERVERS_OPTS=-Ddev_meta=%dev_meta% -Dfat_meta=%fat_meta% -Duat_meta=%uat_meta% -Dpro_meta=%pro_meta% rem =============== Please do not modify the following content =============== rem go to script directory cd "%~dp0" cd .. rem package config-service and admin-service echo "==== starting to build config-service and admin-service ====" call mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=github -Dspring_datasource_url=%apollo_config_db_url% -Dspring_datasource_username=%apollo_config_db_username% -Dspring_datasource_password=%apollo_config_db_password% echo "==== building config-service and admin-service finished ====" echo "==== starting to build portal ====" call mvn clean package -DskipTests -pl apollo-portal -am -Dapollo_profile=github,auth -Dspring_datasource_url=%apollo_portal_db_url% -Dspring_datasource_username=%apollo_portal_db_username% -Dspring_datasource_password=%apollo_portal_db_password% %META_SERVERS_OPTS% echo "==== building portal finished ====" echo "==== starting to build client ====" call mvn clean install -DskipTests -pl apollo-client -am %META_SERVERS_OPTS% echo "==== building client finished ====" pause

4,開始編譯程式碼

執行build.bat編譯apollo專案,編譯完成併成功後,取出以下三個壓縮包:

apollo-adminservice/target/apollo-adminservice-0.11.0-github.zip
apollo-configservice/target/apollo-configservice-0.11.0-github.zip
apollo-portal/target/apollo-portal-0.11.0-github.zip

5, 部署linux環境

建立目錄/app/runtimes/apollo/lib,並上傳三個壓縮包到該資料夾,
建立以下目錄並複製對應的壓縮包到該目錄:
/app/runtimes/apollo/dev/configservice/apollo-configservice-0.11.0-github.zip
/app/runtimes/apollo/dev/adminservice/apollo-adminservice-0.11.0-github.zip

/app/runtimes/apollo/sit/configservice/apollo-configservice-0.11.0-github.zip
/app/runtimes/apollo/sit/adminservice/apollo-adminservice-0.11.0-github.zip

/app/runtimes/apollo/uat/configservice/apollo-configservice-0.11.0-github.zip
/app/runtimes/apollo/uat/adminservice/apollo-adminservice-0.11.0-github.zip

/app/runtimes/apollo/portal/apollo-portal-0.11.0-github.zip

6, 配置各環境服務引數

解壓各資料夾壓縮包並修改引數,例如dev環境的引數:

vim /app/runtimes/apollo/dev/configservice/config/application-github.properties

# DataSource
spring.datasource.url = jdbc:mysql://192.168.1.233:3306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = [email protected]

同時最好將各環境的每個服務的app.properties的appid設為惟一值(未測試是否存在影響)

修改configservice日誌路徑和埠:

vim /app/runtimes/apollo/dev/configservice/scripts/startup.sh

## Adjust log dir if necessary
LOG_DIR=/app/logs/apollo/configservice-dev
## Adjust server port if necessary
SERVER_PORT=18080

vim /app/runtimes/apollo/dev/adminservice/config/application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://192.168.1.233:3306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = [email protected]

修改adminservice日誌路徑和埠:

vim /app/runtimes/apollo/dev/adminservice/scripts/startup.sh

## Adjust log dir if necessary
LOG_DIR=/app/logs/apollo/adminservice-dev
## Adjust server port if necessary
SERVER_PORT=18090

sit環境與uat環境和dev環境進行同樣的配置修改,保持不同的資料庫連線地址、日誌路徑和啟動埠
這裡sit環境的資料庫連線地址為jdbc:mysql://192.168.1.233:3306/ApolloConfigDB_SIT?characterEncoding=utf8
埠為18081、18091

uat環境的資料庫連線地址為jdbc:mysql://192.168.1.233:3306/ApolloConfigDB_UAT?characterEncoding=utf8
埠為18082、18092

修改portal服務的配置

vim /app/runtimes/apollo/portal/config/apollo-env.properties

local.meta=http://localhost:18080
dev.meta=http://192.168.1.235:18080
sit.meta=http://192.168.1.235:18081
uat.meta=http://192.168.1.235:18082
lpt.meta=${lpt_meta}
pro.meta=

vim /app/runtimes/apollo/portal/config/application-github.properties

# DataSource
spring.datasource.url = jdbc:mysql://192.168.1.233:3306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = [email protected]

修改portal的日誌路徑和埠

vim /app/runtimes/apollo/portal/scripts/startup.sh

## Adjust log dir if necessary
LOG_DIR=/app/logs/apollo/portal
## Adjust server port if necessary
SERVER_PORT=18070

7, 修改資料庫配置

修改ApolloPortalDB資料庫的ServerConfig表:
	apollo.portal.envs = dev,sit,uat
	organizations = [{"orgId":"saas","orgName":"電商專案"},{"orgId":"WB","orgName":"維保專案"}]

修改ApolloConfigDB資料庫的ServerConfig表:
	eureka.service.url = http://localhost:18080/eureka/

修改ApolloConfigDB_SIT資料庫的ServerConfig表:
	eureka.service.url = http://localhost:18081/eureka/

修改ApolloConfigDB_UAT資料庫的ServerConfig表:
	eureka.service.url = http://localhost:18082/eureka/

這裡的eureka的地址每個環境必須保持不同,因為apollo啟動時相當於在每個環境都啟動了一個eureka服務,如果沒有配置的話,會造成多個環境的adminservice和configservice註冊在同一個eureka,造成修改dev環境配置檔案的資料,sit環境配置檔案資料也會發生修改。

8, 編輯服務啟動指令碼:

#!/bin/bash

echo "啟動開發環境apollo服務..."
/app/runtimes/apollo/dev/configservice/scripts/startup.sh 
/app/runtimes/apollo/dev/adminservice/scripts/startup.sh 

echo "啟動測試環境apollo服務..."
/app/runtimes/apollo/sit/configservice/scripts/startup.sh 
/app/runtimes/apollo/sit/adminservice/scripts/startup.sh 

echo "啟動UAT環境apollo服務..."
/app/runtimes/apollo/uat/configservice/scripts/startup.sh 
/app/runtimes/apollo/uat/adminservice/scripts/startup.sh 

echo "啟動apollo的protal服務..."
/app/runtimes/apollo/portal/scripts/startup.sh 

賦權並執行後可以按順序一個一個啟動服務(這裡需要注意,每個環境需要先啟動configservice,後啟動adminservice)。啟動完成後訪問192.168.1.235:18070,使用使用者名稱密碼apollo/admin登入

9,編輯服務停止指令碼:

#!/bin/bash
echo "關閉apollo所有環境..."
/app/runtimes/apollo/dev/configservice/scripts/shutdown.sh 
/app/runtimes/apollo/dev/adminservice/scripts/shutdown.sh 
/app/runtimes/apollo/sit/configservice/scripts/shutdown.sh 
/app/runtimes/apollo/sit/adminservice/scripts/shutdown.sh 
/app/runtimes/apollo/uat/configservice/scripts/shutdown.sh 
/app/runtimes/apollo/uat/adminservice/scripts/shutdown.sh 
/app/runtimes/apollo/portal/scripts/shutdown.sh 

服務將按順序停止
更多內容請參考:https://github.com/ctripcorp/apollo/wiki