说明
本次部署采用的 apollo-config :2.2.0 版本,mysql8.0.31(2.2.0版本源码的驱动应该是最高能支持到 mysql8.2.0),参考官方的 部署架构 单机,三个环境 架构,在本地使用 docker-compose 完成 dev、fat、uat 和 pro 四个环境的部署,仅用于测试。生产部署,可以参考官方的高可用部署。
建议先看看官方的部署架构,比较重要的是,configservice 和 adminservice 是一一对应的,每多一套环境,至少需要部署一个 configservice 和 adminservice
其他一些参考的官方文档:
apollo 配置中心 github repo
apollo 配置中心文档
docker 部署文档
使用docker-compose搭建高可用Apollo配置中心
客户端sdk: shima-park/agollo
部署流程
- 创建数据库
Apollo服务端共需要两个数据库:ApolloPortalDB 和 ApolloConfigDB,数据库、表的创建和样例数据都分别准备了 sql 文件,只需要导入数据库即可。
需要注意的是 ApolloPortalDB 只需要在生产环境部署一个即可,而 ApolloConfigDB 需要在每个环境部署一套,如 dev、fat、uat 和 pro 分别部署4套 ApolloConfigDB。
导入,以原生mysql客户端为例:
source /your_local_path/scripts/sql/profiles/mysql-default/apolloportaldb.sql
source /your_local_path/scripts/sql/profiles/mysql-default/apolloconfigdb.sql
导入成功后执行sql进行验证:
# ApolloPortalDB
select `Id`, `Key`, `Value`, `Comment` from `ApolloPortalDB`.`ServerConfig` limit 1;
# ApolloConfigDB
select `Id`, `Key`, `Value`, `Comment` from `ApolloPortalDB`.`ServerConfig` limit 1;
- 修改数据库配置
修改 ApolloConfigDB dev、fat、uat、pro 四个库的配置,找到ServerConfig 表的 eureka.service.url 这项配置,修改其 value
如 pro 环境修改为 http://apollo-configservice-pro:8080/eureka/ ,通过docker 网络访问,默认的 http://localhost:8080/eureka/ 可能导致eureka 注册不上
- 使用到的 docker 镜像
docker pull apolloconfig/apollo-configservice:2.2.0
docker pull apolloconfig/apollo-adminservice:2.2.0
docker pull apolloconfig/apollo-portal:2.2.0
- 使用 docker-compose 编排容器(本地)
docker-compose.yml 配置参考:
最终的目录如下:
- .env 文件内容:
# local 环境 .env 文件
# 数据库配置,默认 config 和 portal 在一个集群里
SPRING_DATASOURCE_HOST_PORT=mysql8.0.31:3306
SPRING_DATASOURCE_PORTAL_DBNAME=ApolloPortalDB
SPRING_DATASOURCE_CONFIG_DBNAME_DEV=ApolloConfigDBDev
SPRING_DATASOURCE_CONFIG_DBNAME_FAT=ApolloConfigDBFat
SPRING_DATASOURCE_CONFIG_DBNAME_UAT=ApolloConfigDBUat
SPRING_DATASOURCE_CONFIG_DBNAME_PRO=ApolloConfigDBPro
SPRING_DATASOURCE_USERNAME=root
SPRING_DATASOURCE_PASSWORD=root
# config server 配置
SERVER_PORT_CONFIG_DEV=8080
SERVER_PORT_CONFIG_FAT=8082
SERVER_PORT_CONFIG_UAT=8083
SERVER_PORT_CONFIG_PRO=8084
# admin server 配置
SERVER_PORT_ADMIN_DEV=8090
SERVER_PORT_ADMIN_FAT=8092
SERVER_PORT_ADMIN_UAT=8093
SERVER_PORT_ADMIN_PRO=8094
# portal server 配置
SERVER_PORT_PORTAL=8070
# 宿主机地址
MY_HOST=localhost
# 是否重启 always、no、on-failure
RESTART=no
# 网络
NETWORK=mysql_default
- docker-compose-local.yml 文件内容:
到 local 目录下运行命令:docker-compose -f docker-compose-local.yml up -d
ps: 程序启动比较慢,容器运行起来2-3分钟左右,功能才可完全使用
version: "3"
services:
# ================================ dev 环境 ================================
apollo-configservice-dev: # Config Service提供配置的读取、推送等功能,服务对象是Apollo客户端
image: apolloconfig/apollo-configservice:2.2.0
container_name: apollo-configservice-dev
volumes:
- ./logs/apollo-configservice-dev:/opt/logs
ports:
- "${SERVER_PORT_CONFIG_DEV}:8080"
environment:
#- SERVER_PORT=8080
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-configservice-dev:8080
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-dev
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_DEV}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
apollo-adminservice-dev: # Admin Service提供配置的修改、发布等功能,服务对象是Apollo Portal(管理界面)
image: apolloconfig/apollo-adminservice:2.2.0
container_name: apollo-adminservice-dev
volumes:
- ./logs/apollo-adminservice-dev:/opt/logs
ports:
- "${SERVER_PORT_ADMIN_DEV}:8090"
depends_on:
- apollo-configservice-dev
environment:
#- SERVER_PORT=8090
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-adminservice-dev:8090
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-dev
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_DEV}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
# ================================ fat 环境 ================================
apollo-configservice-fat: # Config Service提供配置的读取、推送等功能,服务对象是Apollo客户端
image: apolloconfig/apollo-configservice:2.2.0
container_name: apollo-configservice-fat
volumes:
- ./logs/apollo-configservice-fat:/opt/logs
ports:
- "${SERVER_PORT_CONFIG_FAT}:8080"
environment:
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-configservice-fat:8080
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-fat
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_FAT}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
apollo-adminservice-fat: # Admin Service提供配置的修改、发布等功能,服务对象是Apollo Portal(管理界面)
image: apolloconfig/apollo-adminservice:2.2.0
container_name: apollo-adminservice-fat
volumes:
- ./logs/apollo-adminservice-fat:/opt/logs
ports:
- "${SERVER_PORT_ADMIN_FAT}:8090"
depends_on:
- apollo-configservice-fat
environment:
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-adminservice-fat:8090
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-fat
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_FAT}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
# ================================ uat 环境 ================================
apollo-configservice-uat: # Config Service提供配置的读取、推送等功能,服务对象是Apollo客户端
image: apolloconfig/apollo-configservice:2.2.0
container_name: apollo-configservice-uat
volumes:
- ./logs/apollo-configservice-uat:/opt/logs
ports:
- "${SERVER_PORT_CONFIG_UAT}:8080"
environment:
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-configservice-uat:8080
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-uat
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_UAT}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
apollo-adminservice-uat: # Admin Service提供配置的修改、发布等功能,服务对象是Apollo Portal(管理界面)
image: apolloconfig/apollo-adminservice:2.2.0
container_name: apollo-adminservice-uat
volumes:
- ./logs/apollo-adminservice-uat:/opt/logs
ports:
- "${SERVER_PORT_ADMIN_UAT}:8090"
depends_on:
- apollo-configservice-uat
environment:
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-adminservice-uat:8090
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-uat
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_UAT}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
# ================================ prod 环境 ================================
apollo-configservice-pro: # Config Service提供配置的读取、推送等功能,服务对象是Apollo客户端
image: apolloconfig/apollo-configservice:2.2.0
container_name: apollo-configservice-pro
volumes:
- ./logs/apollo-configservice-pro:/opt/logs
ports:
- "${SERVER_PORT_CONFIG_PRO}:8080"
environment:
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-configservice-pro:8080
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-pro
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_PRO}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
apollo-adminservice-pro: # Admin Service提供配置的修改、发布等功能,服务对象是Apollo Portal(管理界面)
image: apolloconfig/apollo-adminservice:2.2.0
container_name: apollo-adminservice-pro
volumes:
- ./logs/apollo-adminservice-pro:/opt/logs
ports:
- "${SERVER_PORT_ADMIN_PRO}:8090"
depends_on:
- apollo-configservice-pro
environment:
- EUREKA_INSTANCE_HOME_PAGE_URL=http://apollo-adminservice-pro:8090
- EUREKA_INSTANCE_IP_ADDRESS=apollo-configservice-pro
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_CONFIG_DBNAME_PRO}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
networks:
- ${NETWORK}
restart: ${RESTART}
# ================================ 管理界面 ================================
apollo-portal:
image: apolloconfig/apollo-portal:2.2.0
container_name: apollo-portal
volumes:
- ./logs/apollo-portal:/opt/logs
ports:
- "${SERVER_PORT_PORTAL}:8070"
depends_on:
- apollo-adminservice-dev
environment:
#- SERVER_PORT=8070
#- EUREKA_INSTANCE_IP_ADDRESS=http://apollo-portal:8080/eureka/
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/${SPRING_DATASOURCE_PORTAL_DBNAME}?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
- APOLLO_PORTAL_ENVS=dev,fat,uat,pro
- DEV_META=${MY_HOST}:${SERVER_PORT_CONFIG_DEV}
- FAT_META=${MY_HOST}:${SERVER_PORT_CONFIG_FAT}
- UAT_META=${MY_HOST}:${SERVER_PORT_CONFIG_UAT}
- PRO_META=${MY_HOST}:${SERVER_PORT_CONFIG_PRO}
networks:
- ${NETWORK}
restart: ${RESTART}
# 配置使用本地的 mysql 所在网络
networks:
mysql_default:
external: true
- 登录验证
登录地址:http://localhost:8070/
eureka 注册信息:http://localhost:8080/
默认账户密码:
apollo
admin
修改下 portalDB 配置:
如果创建一个项目后未自动加载出四个环境,可以修改下 portalDB 配置,这两项不为空时会优先使用系统配置里的值,否则使用 docker-compose 启动时定义的值
# apollo.portal.envs
dev,fat,uat,pro
# apollo.portal.meta.servers
{"dev": "http://apollo-configservice-dev:8080","fat": "http://apollo-configservice-fat:8080","uat": "http://apollo-configservice-uat:8080","pro": "http://apollo-configservice-pro:8080"}
- go 客户端 sdk :
可以考虑使用 shima-park/agollo,适配了 viper ,能够方便的把配置映射到golang 结构体,使用体验比较好
package main
import (
"fmt"
"github.com/shima-park/agollo/viper-remote"
"github.com/spf13/viper"
"time"
)
func main() {
NewClient()
}
func NewClient() {
remote.SetAppID("6xdh8k9kuy") // apollo 配置中心里创建的应用id
v := viper.New()
v.SetConfigType("prop") // 根据 namespace 实际格式设置对应 type
err := v.AddRemoteProvider("apollo", "localhost:8080", "application")
if err != nil {
panic(fmt.Sprintf("2e8ukhn7sq AddRemoteProvider from apollo error: %+v \n", err))
}
err = v.ReadRemoteConfig()
if err != nil {
panic(fmt.Sprintf("hp2wuvemae ReadRemoteConfig from apollo error: %+v \n", err))
}
// 直接反序列化到结构体中
var conf kmgMysqlV2.Config
err = v.Unmarshal(&conf)
if err != nil {
panic(fmt.Sprintf("xsjefu3cyg Unmarshal from apollo to kmgMysqlV2.Config error, conf: %+v err: %+v \n", conf, err))
}
// 各种基础类型配置项读取
// fmt.Println("Host:", v.GetString("db.host"))
// fmt.Println("Port:", v.GetInt("db.port"))
// fmt.Println("Timeout:", v.GetDuration("db.timeout"))
// 获取所有key,所有配置
// fmt.Println("AllKeys", v.AllKeys(), "AllSettings", v.AllSettings())
// 启动一个goroutine来同步配置更改
_ = v.WatchRemoteConfigOnChannel()
for {
time.Sleep(60 * time.Second)
fmt.Println("jvmba3hxtb WatchRemoteConfigOnChannel AllSettings:", v.AllSettings())
}
}
// 一个简易 mysql 配置
type Config struct {
Database string `mapstructure:"database"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
Retry int `mapstructure:"retry"`
MaxOpen int `mapstructure:"maxopen"`
MaxIdle int `mapstructure:"maxidle"`
Lifetime int `mapstructure:"lifetime"`
Timeout int `mapstructure:"timeout"`
}