Apollo(阿波罗)是携程开源的分布式配置管理中心。 本文主要介绍其基于 Docker-Compose 的部署安装和一些使用的经验
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公司"}]
官方:Com.Ctrip.Framework.Apollo.Configuration
Com.Ctrip.Framework.Apollo.Configuration
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 地址
时间充裕的情况最好是过一遍文档,知道是怎么设计的,遇到问题真会一头雾水。
每天进步一点,哪怕只是一点!