apollo(阿波罗)是一款可靠的分布式配置管理中心,诞生于携程框架研发部,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
apollo的官方文档很详细,但是有点过于详细和全面了,对于想快速搭建apollo环境试用的人来说,干扰信息太多。为了给后来者参考,也作为自己部署apollo的笔记,故写下这篇文章。
本文记录从源码编译开始,在单机上以分布式部署的方式,部署单环境单实例apollo服务,以及多环境多实例的apollo服务的过程,并附上简单的使用示例。
系统版本:Windows 10
apollo版本:2.0.1
MySQL版本:5.7.20
Java版本:1.8.0_101
Git Bash:2.12.0.windows.1
github地址:https://github.com/apolloconfig/apollo/tree/v2.0.1
执行apollo-2.0.1/scripts/sql
目录下的两个sql文件:apolloconfigdb.sql
、apolloportaldb.sql
apollo-2.0.1/scripts/build.bat
文件(如果是Linux系统,请修改apollo-2.0.1/scripts/build.sh
)rem apollo config db info
set apollo_config_db_url="jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8"
set apollo_config_db_username="root"
set apollo_config_db_password="root"
rem apollo portal db info
set apollo_portal_db_url="jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8"
set apollo_portal_db_username="root"
set apollo_portal_db_password="root"
(本次部署使用默认配置, 默认环境为dev,此处可以跳过)
rem meta server url, different environments should have different meta server addresses
set dev_meta="http://localhost:8080"
set fat_meta="http://someIp:8080"
set uat_meta="http://anotherIp:8080"
set pro_meta="http://yetAnotherIp:8080"
apollo-2.0.1/scripts/build.bat
,执行完毕后获取以下文件,并拷贝到安装目录apollo-2.0.1/apollo-adminservice/target/apollo-adminservice-2.0.1-github.zip
apollo-2.0.1/apollo-configservice/target/apollo-configservice-2.0.1-github.zip
apollo-2.0.1/apollo-portal/target/apollo-portal-2.0.1-github.zip
进入安装目录,解压:
unzip apollo-adminservice-2.0.1-github.zip -d apollo-adminservice-2.0.1-github
unzip apollo-configservice-2.0.1-github.zip -d apollo-configservice-2.0.1-github
unzip apollo-portal-2.0.1-github.zip -d apollo-portal-2.0.1-github
按configservice -> adminservice -> portal
的顺序依次启动服务,可以使用以下脚本启动:
sh apollo-configservice-2.0.1-github/scripts/startup.sh
sh apollo-adminservice-2.0.1-github/scripts/startup.sh
sh apollo-portal-2.0.1-github/scripts/startup.sh
地址:http://localhost:8070
用户名:apollo
密码:admin
sh apollo-portal-2.0.1-github/scripts/shutdown.sh
sh apollo-adminservice-2.0.1-github/scripts/shutdown.sh
sh apollo-configservice-2.0.1-github/scripts/shutdown.sh
方式一:在apollo页面,依次点击 管理员工具 -> 系统参数 -> 在Key中填入organizations
,点击查询 -> 修改Value的Json字符串,点击保存
方式二:直接修改数据库数据,修改ApolloPortalDB.ServerConfig
表中Key='organizations'
的数据对应的Value值
configservice和meta service在同一个服务中,共用一个端口,所以修改configservice的端口后,也要将meta service信息进行更新
修改apollo-configservice-2.0.1-github/scripts/startup.sh
中的SERVER_PORT
修改meta service信息,包括以下两个内容:
修改apollo-portal-2.0.1-github/config/apollo-env.properties
中的meta service地址的端口为configservice的端口
修改ApolloConfigDB.ServerConfig
表中Key='eureka.service.url'
的数据对应的Value值,修改端口为configservice的端口
apollo-adminservice-2.0.1-github/scripts/startup.sh
中的SERVER_PORT
即可apollo-portal-2.0.1-github/scripts/startup.sh
中的SERVER_PORT
即可步骤一:在apollo-portal-2.0.1-github/config/apollo-env.properties
中添加meta service地址,环境名以.meta
结尾
步骤二:修改支持的环境列表(以下方式任选一种即可)
方式一:在ApolloPortalDB.ServerConfig
表中Key='apollo.portal.envs'
的数据对应的Value值中添加环境名,多个环境间以英文逗号分隔
方式二:在apollo页面,依次点击 管理员工具 -> 系统参数 -> 在Key中填入apollo.portal.envs
,点击查询 -> 在Value中添加环境名,多个环境间以英文逗号分隔,点击保存
多个环境的情况下,portal只需要部署一套即可,但configservice和adminservice需要每个环境部署一套,每个环境也需要一个对应的ApolloConfigDB数据库
本次部署test和production两个环境,其中:
portal部署两个实例,所有环境共用这两个portal,访问其中任一portal即可
configservice和adminservice在test环境部署单实例,production环境部署两个实例
各服务实例的端口占用如下:
portal1:11000
portal2:11001
configservice(test):11002
adminservice(test):11003
configservice1(production):11004
configservice2(production):11005
adminservice1(production):11006
adminservice2(production):11007
github地址:https://github.com/apolloconfig/apollo/tree/v2.0.1
直接执行apollo-2.0.1/scripts/sql/apolloportaldb.sql
复制apollo-2.0.1/scripts/sql/apolloportaldb.sql
为apollo-2.0.1/scripts/sql/apolloportaldb-test.sql
和apollo-2.0.1/scripts/sql/apolloportaldb-production.sql
将apollo-2.0.1/scripts/sql/apolloportaldb-test.sql
中的数据库改为ApolloConfigDBTest
,包括创建和使用数据库的两条语句,然后执行该sql文件
将apollo-2.0.1/scripts/sql/apolloportaldb-production.sql
中的数据库改为ApolloConfigDBProduction
,包括创建和使用数据库的两条语句,然后执行该sql文件
apollo-2.0.1/scripts/build.bat
中关于数据库的内容,被删除部分示例:rem apollo config db info
set apollo_config_db_url="jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8"
set apollo_config_db_username="root"
set apollo_config_db_password=""
rem apollo portal db info
set apollo_portal_db_url="jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8"
set apollo_portal_db_username="root"
set apollo_portal_db_password=""
通过打包脚本传入的环境变量,其优先级高于我们后面要修改的配置文件,所以先删掉这部分内容(apollo加载meta service配置的源码位于com.ctrip.framework.apollo.portal.environment.DefaultPortalMetaServerProvider#initializeDomains
)
被删除部分示例:
rem meta server url, different environments should have different meta server addresses
set dev_meta="http://localhost:8080"
set fat_meta="http://someIp:8080"
set uat_meta="http://anotherIp:8080"
set pro_meta="http://yetAnotherIp:8080"
set META_SERVERS_OPTS=-Ddev_meta=%dev_meta% -Dfat_meta=%fat_meta% -Duat_meta=%uat_meta% -Dpro_meta=%pro_meta%
并删除后面maven打包时使用到META_SERVERS_OPTS
变量的地方
apollo-2.0.1/scripts/build.bat
,执行完毕后获取以下文件,并拷贝到安装目录apollo-2.0.1/apollo-adminservice/target/apollo-adminservice-2.0.1-github.zip
apollo-2.0.1/apollo-configservice/target/apollo-configservice-2.0.1-github.zip
apollo-2.0.1/apollo-portal/target/apollo-portal-2.0.1-github.zip
# 创建文件夹
mkdir -p portal/portal1
mkdir -p portal/portal2
mkdir -p test/configservice
mkdir -p test/adminservice
mkdir -p production/configservice1
mkdir -p production/configservice2
mkdir -p production/adminservice1
mkdir -p production/adminservice2
# 解压
unzip apollo-portal-2.0.1-github.zip -d portal/portal1/
unzip apollo-portal-2.0.1-github.zip -d portal/portal2/
unzip apollo-configservice-2.0.1-github.zip -d test/configservice/
unzip apollo-configservice-2.0.1-github.zip -d production/configservice1/
unzip apollo-configservice-2.0.1-github.zip -d production/configservice2/
unzip apollo-adminservice-2.0.1-github.zip -d test/adminservice/
unzip apollo-adminservice-2.0.1-github.zip -d production/adminservice1/
unzip apollo-adminservice-2.0.1-github.zip -d production/adminservice2/
修改端口
修改test/configservice/scripts/startup.sh
的SERVER_PORT
为11002
修改test/adminservice/scripts/startup.sh
的SERVER_PORT
为11003
添加数据库连接信息
以上两个文件,在JAVA_OPTS
变量被定义后,被使用前,添加如下代码(启动时,JAVA_OPTS
参数被放在-jar
之前,所以此处需要用-D
传参,而不能用--
。下同)
# 数据库连接信息
apollo_config_db_url='jdbc:mysql://localhost:3306/ApolloConfigDBTest?characterEncoding=utf8'
apollo_config_db_username='root'
apollo_config_db_password='root'
JAVA_OPTS="$JAVA_OPTS -Dspring.datasource.url=$apollo_config_db_url -Dspring.datasource.username=$apollo_config_db_username -Dspring.datasource.password=$apollo_config_db_password"
修改端口
修改production/configservice1/scripts/startup.sh
的SERVER_PORT
为11004
修改production/configservice2/scripts/startup.sh
的SERVER_PORT
为11005
修改production/adminservice1/scripts/startup.sh
的SERVER_PORT
为11006
修改production/adminservice2/scripts/startup.sh
的SERVER_PORT
为11007
添加数据库连接信息
以上四个文件,在JAVA_OPTS
变量被定义后,被使用前,添加如下代码
# 数据库连接信息
apollo_config_db_url='jdbc:mysql://localhost:3306/ApolloConfigDBProduction?characterEncoding=utf8'
apollo_config_db_username='root'
apollo_config_db_password='root'
JAVA_OPTS="$JAVA_OPTS -Dspring.datasource.url=$apollo_config_db_url -Dspring.datasource.username=$apollo_config_db_username -Dspring.datasource.password=$apollo_config_db_password"
修改端口
修改portal/portal1/scripts/startup.sh
的SERVER_PORT
为11000
修改portal/portal2/scripts/startup.sh
的SERVER_PORT
为11001
添加数据库连接信息
以上两个文件,在JAVA_OPTS
变量被定义后,被使用前,添加如下代码
# 数据库连接信息
apollo_portal_db_url='jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8'
apollo_portal_db_username='root'
apollo_portal_db_password='root'
JAVA_OPTS="$JAVA_OPTS -Dspring.datasource.url=$apollo_portal_db_url -Dspring.datasource.username=$apollo_portal_db_username -Dspring.datasource.password=$apollo_portal_db_password"
添加环境信息
将portal/portal1/config/apollo-env.properties
和portal/portal2/config/apollo-env.properties
的内容清空,然后添加如下内容
test.meta=http://localhost:11002
production.meta=http://localhost:11004,http://localhost:11005
修改数据库中的配置
修改ApolloConfigDBTest.ServerConfig
中Key=eureka.service.url
的数据,将对应的Value改为http://localhost:11002/eureka/
修改ApolloConfigDBProduction.ServerConfig
中Key=eureka.service.url
的数据,将对应的Value改为http://localhost:11004/eureka/,http://localhost:11005/eureka/
修改ApolloPortalDB.ServerConfig
中Key=apollo.portal.envs
的数据,将对应的Value改为test,production
依次启动各个环境的configservice、adminservice,最后启动portal,可参考如下脚本
sh test/configservice/scripts/startup.sh
sh test/adminservice/scripts/startup.sh
sh production/configservice1/scripts/startup.sh
sh production/configservice2/scripts/startup.sh
sh production/adminservice1/scripts/startup.sh
sh production/adminservice2/scripts/startup.sh
sh portal/portal1/scripts/startup.sh
sh portal/portal2/scripts/startup.sh
由于启动了两个portal服务,所以以下两个地址都可以访问
地址一:
地址:http://localhost:11000
用户名:apollo
密码:admin
地址二:
地址:http://localhost:11001
用户名:apollo
密码:admin
可参考如下脚本
sh portal/portal1/scripts/shutdown.sh
sh portal/portal2/scripts/shutdown.sh
sh production/adminservice1/scripts/shutdown.sh
sh production/adminservice2/scripts/shutdown.sh
sh production/configservice1/scripts/shutdown.sh
sh production/configservice2/scripts/shutdown.sh
sh test/adminservice/scripts/shutdown.sh
sh test/configservice/scripts/shutdown.sh
1.在pom.xml中添加maven依赖
<dependency>
<groupId>com.ctrip.framework.apollogroupId>
<artifactId>apollo-client-config-dataartifactId>
<version>2.0.1version>
dependency>
2.在application.properties中添加如下配置
# app id
app.id=001
# meta service服务地址,多个地址用英文逗号隔开
apollo.meta=http://localhost:11004,http://localhost:11005
apollo.bootstrap.enabled=true
# 多个namespace有相同的key时,从左到右优先级从高到低
apollo.bootstrap.namespace=application,local
3.启动应用后即可接入apolloconfig