前言
为了更深的理解apollo架构原理, 首先需要模拟生产对其进行分布式多环境部署
根据官网 分布式多环境部署文档 推荐每个环境单独部署configservice以及adminservice
鉴于当前研究学习,没有必要多台虚拟机消耗资源,故单机部署
思路
理解apollo的整体架构后,对比nacos配置中心功能点,其设计的复杂度以及完整度相较nacos而言更为丰富
对于不同的环境,例如 FAT, PRO 在部署时进程已经完全物理隔离,并且每个环境config/adminservice依赖的数据也完全物理隔离(连接不同的db),在数据隔离与安全方面做的很完善,但同时也提升了架构的复杂度
而 portal 可以管理多个环境,单节点部署虽然有单点问题,但并不会影响业务从configservice读取配置, 挂掉只要重启就可以,只是在一段时间内无法修改配置
所以整体的思路为:
- 部署一个portal服务, 配置管理多个环境
- 每个环境单独一个文件夹,修改每个环境configservice、adminservice的端口避免冲突
- 每个环境单独连接一个db,config/admin 都依赖db管理数据
- (重要)由于configservice中内嵌了eureka server, 并且同一个环境内的configservice, adminservice也需要注册到 configservice中的eureka, 而改变了config的端口,也就意味着内嵌的eureka sever地址变化,所以需要修改 configservice 以及 adminservice 作为eureka client 注册到服务端的地址
configservice 源码配置如下:
adminservice 源码配置如下
部署实践
这里部署 2个环境 FAT 以及 PRO, 更多环境同理
部署一个portal 管理这两个环境,并提供界面操作
版本为 apollo 2.3.0
FAT 环境 configservice 端口 8080, adminservice 端口 8090, FAT环境统一注册到 8080 configservice内嵌的eurekaserver中
PRO环境 configservice 端口 8081,adminservice 端口 8091,PRO环境统一注册到 8081 configservice内嵌的eurekaserver中
1. 部署db
1.1. 自行准备一台mysql
1.2. 用官方提供的ApolloConfigDB.sql 创建两个数据库,apolloconfigdb_fat
以及 apolloconfigdb_pro
, 分别存放两个不同环境的配置数据
1.3 用官方提供的ApolloPortalDB.sql 创建apolloportaldb 数据库,用来存放portal数据
2. 下载安装包规划文件夹
sudo mkdir /opt/apollo-2.3.0
cd /opt/apollo-2.3.0
# 链接失效在github上下载
wget https://gh.api.99988866.xyz/https://github.com/apolloconfig/apollo/releases/download/v2.3.0/apollo-adminservice-2.3.0-github.zip
wget https://gh.api.99988866.xyz/https://github.com/apolloconfig/apollo/releases/download/v2.3.0/apollo-configservice-2.3.0-github.zip
wget https://gh.api.99988866.xyz/https://github.com/apolloconfig/apollo/releases/download/v2.3.0/apollo-portal-2.3.0-github.zip
mkdir -p ./fat/adminservice ./fat/configservice
unzip -d ./fat/adminservice/ apollo-adminservice-2.3.0-github.zip
unzip -d ./fat/configservice/ apollo-configservice-2.3.0-github.zip
mkdir -p ./fat/configservice/logs ./fat/adminservice/logs
cp -R ./fat ./pro
unzip -d ./portal apollo-portal-2.3.0-github.zip
mkdir -p ./portal/logs
rm -f apollo-*
3. 修改 fat configservice 环境配置以及脚本
3.1 修改configservice log地址
将其改为刚刚创建的日志地址 /opt/apollo/fat/configservice/logs
vim /opt/apollo/fat/configservice/apollo-configservice.conf
3.2 修改configservice数据库配置
vim /opt/apollo/fat/configservice/config/application-github.properties
3.3 修改 configservice 启动脚本
- 修改掉gc log 日志地址
- 新增eureka server 地址
- jar启动添加参数, 替换默认配置的eureka server 地址
vim /opt/apollo/fat/configservice/scripts/startup.sh
4. 修改 fat adminservice 环境配置以及脚本
4.1 修改adminservice log地址
vim /opt/apollo/fat/adminservice/apollo-adminservice.conf
4.2 修改adminservice数据库配置 (与 fat configservice 连接的是同一个库)
vim /opt/apollo/fat/adminservice/config/application-github.properties
4.3 修改 adminservice 启动脚本
vim /opt/apollo/fat/adminservice/scripts/startup.sh
5. 修改 pro configservice 环境配置以及脚本
5.1 修改 configservice log地址
vim /opt/apollo/pro/configservice/apollo-configservice.conf
5.2 修改 configservice 数据库配置
vim /opt/apollo/pro/configservice/config/application-github.properties
5.3 修改 configservice 启动脚本
vim /opt/apollo/pro/configservice/scripts/startup.sh
6. 修改 pro adminservice 环境配置以及脚本
5.1 修改 adminservice log地址
vim /opt/apollo/pro/adminservice/apollo-adminservice.conf
5.2 修改 adminservice 数据库配置
vim /opt/apollo/pro/adminservice/config/application-github.properties
5.3 修改 adminservice 启动脚本
vim /opt/apollo/pro/adminservice/scripts/startup.sh
7. 修改 portal 配置
7.1 修改 portal log地址
vim /opt/apollo/portal/apollo-portal.conf
7.2 修改 portal 数据库配置
vim /opt/apollo/portal/config/application-github.properties
7.3 配置 apollo env
vim /opt/apollo/portal/config/apollo-env.properties
7.4 配置 portal 启动脚本
vim /opt/apollo/portal/scripts/startup.sh
8. 准备一键启动脚本
8.1 一键启动脚本
vim /opt/apollo/startup.sh
chmod +x /opt/apollo/startup.sh
内容如下
#!/bin/bash
# 先启动configservice, 再启动adminservice
/opt/apollo/fat/configservice/scripts/startup.sh
/opt/apollo/fat/adminservice/scripts/startup.sh
/opt/apollo/pro/configservice/scripts/startup.sh
/opt/apollo/pro/adminservice/scripts/startup.sh
/opt/apollo/portal/scripts/startup.sh
8.2 一键停止脚本
vim /opt/apollo/shutdown.sh
chmod +x /opt/apollo/shutdown.sh
内容如下
#!/bin/bash
/opt/apollo/portal/scripts/shutdown.sh
/opt/apollo/fat/adminservice/scripts/shutdown.sh
/opt/apollo/fat/configservice/scripts/shutdown.sh
/opt/apollo/pro/adminservice/scripts/shutdown.sh
/opt/apollo/pro/configservice/scripts/shutdown.sh
执行一键启动
bash /opt/apollo/startup.sh
部署配置以及验证
访问服务器 http://ip:8070
账号密码:
apollo
admin
至此部署成功,但是要使用还需要配置环境
apollo.portal.meta.servers 官方配置详解
notice: ProtalDB 配置完不会生效,必须重启 portal 服务
bash /opt/apollo/portal/scripts/shutdown.sh
bash /opt/apollo/portal/scripts/startup.sh
重启完,ConfigDB配置能看到正确的对应配置
至此整个环境部署配置完成,可以接入项目使用
结语及tips
部署过程中遇到在portal中切换环境,端口显示不正确,很可能是两个环境都注册到 同一个eureka中
访问 http://你的ip:8080 以及 访问 http://你的ip:8081 查看eureka中注册是否正确
查看启动进程端口配置以及 eureka server 地址有没有改对
ps -ef | grep java
最后Gitee上apollo client 使用项目
apollo-client-api demo