Apollo(阿波羅)是攜程開源的分散式設定管理中心。
本文主要介紹其基於 Docker-Compose 的部署安裝和一些使用的經驗
當前版本:v2.1
apollo-db:mysql 5.6.6+ 資料庫
apollo-configservice:Config Service 提供設定的讀取、推播等功能。
apollo-adminservice: Admin Service 提供設定的修改、釋出等功能
apollo-portal:Portal 提供 Web 介面用來管理設定
Deureka:提供服務註冊和發現
eureka.service.url
服務設定方式的優先順序從高到低分別為:系統引數>環境變數>外部組態檔
本篇文章基於 Docker V24 及 Docker Compose V2,安裝可以參考之前的文章
SPRING_DATASOURCE_URL:'...apollo-db:3306/...'
depends_on:apollo-db
http://192.168.123.214:8080
http://192.168.123.214:809
-Deureka.service.url=``http://192.168.123.214:8080/eureka/
docker network create devopsnetwork
)準備好 compose.yml 及 。/initsql/初始化指令碼,修改其中的 IP
拷貝到伺服器
然後執行docker compose up -d
即可
version: '3.1'
services:
# Apollo資料庫
apollo-db:
image: mysql:5.7
container_name: apollo_db_5_7
restart: always
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 'devops666'
ports:
- "13306:3306"
volumes:
- ./initsql:/docker-entrypoint-initdb.d
- ./data:/var/lib/mysql
networks:
- devopsnetwork
# Apollo 服務發現註冊中心
apollo-configservice:
container_name: apollo_configservice_2_1
image: apolloconfig/apollo-configservice:2.1.0
restart: always
depends_on:
- apollo-db
environment:
SPRING_DATASOURCE_URL: 'jdbc:mysql://apollo-db:3306/ApolloConfigDB?characterEncoding=utf8'
SPRING_DATASOURCE_USERNAME: 'root'
SPRING_DATASOURCE_PASSWORD: 'devops666'
JAVA_OPTS: "-Deureka.instance.homePageUrl=http://192.168.123.214:8080"
# EUREKA_INSTANCE_HOME_PAGE_URL: http://192.168.123.214:8080
# EUREKA_INSTANCE_PREFER_IP_ADDRESS: false
volumes:
- ./logs:/opt/logs
ports:
- "8080:8080"
networks:
- devopsnetwork
#核心介面服務
apollo-adminservice:
container_name: apollo_adminservice_2_1
image: apolloconfig/apollo-adminservice:2.1.0
restart: always
environment:
SPRING_DATASOURCE_URL: 'jdbc:mysql://apollo-db:3306/ApolloConfigDB?characterEncoding=utf8'
SPRING_DATASOURCE_USERNAME: 'root'
SPRING_DATASOURCE_PASSWORD: 'devops666'
JAVA_OPTS: "-Deureka.instance.homePageUrl=http://192.168.123.214:8090 -Deureka.service.url=http://192.168.123.214:8080/eureka/ "
depends_on:
- apollo-db
ports:
- "8090:8090"
volumes:
- ./logs/:/opt/logs
networks:
- devopsnetwork
apollo-portal:
image: apolloconfig/apollo-portal:2.1.0
container_name: apollo_portal_2_1
restart: always
environment:
SPRING_DATASOURCE_URL: 'jdbc:mysql://apollo-db:3306/ApolloPortalDB?characterEncoding=utf8'
SPRING_DATASOURCE_USERNAME: 'root'
SPRING_DATASOURCE_PASSWORD: 'devops666'
APOLLO_PORTAL_ENVS: 'dev'
DEV_META: 'http://192.168.123.214:8080'
# 預設賬號 apollo admin
depends_on:
- apollo-db
ports:
- "8070:8070"
volumes:
- ./logs/:/opt/logs
networks:
- devopsnetwork
networks:
devopsnetwork:
external: true
```
部署機器IP:192.168.123.214
跟著官方檔案一步步來即可,helm 的檔案可以從 apolloconfig/apollo-helm-chart 獲取,這裡只分享下步驟和些注意的地方
初始化資料庫
apollo.portal.envs:pro
organizations:[{"orgId":"xxx","orgName":"xxx公司"}]
使用 helm 新增 apollo repo
安裝 apollo-service
安裝 apollo-portal
k8s 使用
官方:Com.Ctrip.Framework.Apollo.Configuration
新增包:Com.Ctrip.Framework.Apollo.Configuration
appsetting.json 中新增 apollo 設定
獲取引數註冊:可以是設定,也可以是從環境變數中
builder.Configuration.AddApollo(builder.Configuration.GetSection("apollo"));
注入 IConfiguration
使用即可
"apollo": {
"MetaServer": "http://192.168.123.214:8080",
"AppId": "devops.test",
"Namespaces": [ "application" ]
}
dotnet v7.0
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddApollo(builder.Configuration.GetSection("apollo"));
app.MapGet("/config", context =>
{
context.Response.Headers["Content-Type"] = "text/html; charset=utf-8";
//設定服務
var configService = context.RequestServices.GetRequiredService<IConfiguration>();
string? key = context.Request.Query["key"];
if (string.IsNullOrWhiteSpace(key))
{
return context.Response.WriteAsync("獲取設定:/config?key=test");
}
var value = configService[key];
return context.Response.WriteAsync(value ?? "undefined");
});
完整 Demo 範例 :Github 地址
時間充裕的情況最好是過一遍檔案,知道是怎麼設計的,遇到問題真會一頭霧水。
每天進步一點,哪怕只是一點!