在搭建使用过程中也是遇到各种问题,特写下此文章记录一下,也希望给予看到这篇文章的小伙伴一丝帮助。
1. 搭建
1.1 环境要求
mysql: 5.6.5+
java: 1.8+
1.2 两种搭建方式
1.2.1 Quick Start
apollo服务端共需两个数据库:ApolloPortalDB
和ApolloConfigDB
-
执行 .../代码目录/sql/两个sql文件
apolloportaldb.sql
和apolloconfigdb.sql
-
配置数据库连接信息
编辑demo.sh
文件
#!/bin/bash
# apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3307/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password= //(如果没有密码,留空即可)
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3307/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=
...
-
执行启动脚本
启动会占用8070, 8080, 8090
端口,请确保这3个端口当前没有被使用。
执行./demo.sh start
注://windows用户可以右键空白处git bash here
运行sh ./demo.sh
命令运行
当看到如下日志就说明成功了
==== starting service ====
Service logging file is ./service/apollo-service.log
Started [10768]
Waiting for config service startup.......
Config service started. You may visit http://localhost:8080 for service status now!
Waiting for admin service startup....
Admin service started
==== starting portal ====
Portal logging file is ./portal/apollo-portal.log
Started [10846]
Waiting for portal startup......
Portal started. You can visit http://localhost:8070 now!
如果启动遇到了异常,可以分别查看service和portal目录下的log文件排查问题。
(1.2.1 Quick Start 方式搭建好像是不能修改端口的,修改后不能正常启动 o(╥﹏╥)o )
- 访问 http://localhost:8070 账号:
apollo
密码:admin
1.2.2 使用源码构建
-
GitHub下载: https://github.com/ctripcorp/apollo/releases
创建数据库同上
若apollo不满足你的需求,需修改apollo源码,请执行如下步骤
a) GitHub下载: https://github.com/ctripcorp/apollo ;
b) 根据你的需求修改源码;
c) 运行scripts/build.sh
文件,windows运行build.bat
文件。运行完成之后每个模块下target
目录会生成xxx-版本号-github.zip
的文件;
d) 取出apollo-adminservice-xxx-github.zip
、apollo-configservice-xxx-github.zip
、apollo-portal-xxx-github.zip
,即上面下载的zip。修改配置
解压下载的或者build的三个zip文件,得到adminservice
、configservice
、portal
文件夹
a) adminservice
i) 修改端口号:adminservice/scripts/startup.sh
SERVER_PORT=7030
#!/bin/bash
SERVICE_NAME=apollo-adminservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/100003172
## Adjust server port if necessary
SERVER_PORT=7030
## Create log directory if not existed because JDK 8+ won't do that
ii) 修改数据库连接: adminservice/config/application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
b) configservice
i) 修改端口号:configservice/scripts/startup.sh
SERVER_PORT=7020
#!/bin/bash
SERVICE_NAME=apollo-configservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/100003172
## Adjust server port if necessary
SERVER_PORT=7020
## Create log directory if not existed because JDK 8+ won't do that
ii) 修改数据库连接: configservice/config/application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
c) portal
i) 修改端口号: portal/scripts/startup.sh
SERVER_PORT=7010
#!/bin/bash
SERVICE_NAME=apollo-portal
## Adjust log dir if necessary
LOG_DIR=/opt/logs/100003173
## Adjust server port if necessary
SERVER_PORT=7010
## Create log directory if not existed because JDK 8+ won't do that
ii) 修改数据库连接: portal/config/application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
iii) 配置环境列表端口 portal/config/apollo-env.properties
local.meta=http://localhost:8080
dev.meta=http://localhost:7020
#fat.meta=http://localhost:7020
#uat.meta=http://localhost:7020
#lpt.meta=${lpt_meta}
#pro.meta=http://localhost:7020
2. 部署
- 运行刚修改的三个
startup.sh
脚本,顺序无所谓; - 访问 http://localhost:7010 即可进入apollo portal登录页;
- 每次启动停止都要运行三个脚本,着实麻烦,可以写个简单的脚本,一次性启动停止所有服务;
启动
#!/bin/sh
echo "==== 启动开发环境apollo服务 ===="
/home/zhanhao/service/apollo1.4/dev/configservice/scripts/startup.sh
/home/zhanhao/service/apollo1.4/dev/adminservice/scripts/startup.sh
echo "==== 启动sit环境apollo服务 ===="
/home/zhanhao/service/apollo1.4/sit/configservice/scripts/startup.sh
/home/zhanhao/service/apollo1.4/sit/adminservice/scripts/startup.sh
echo "==== 启动uat环境apollo服务 ===="
/home/zhanhao/service/apollo1.4/uat/configservice/scripts/startup.sh
/home/zhanhao/service/apollo1.4/uat/adminservice/scripts/startup.sh
echo "==== 启动portal apollo服务 ===="
/home/zhanhao/service/apollo1.4/portal/scripts/startup.sh
停止
#!/bin/sh
echo "==== Stopping Apollo Service ===="
/home/zhanhao/service/apollo1.4/portal/scripts/shutdown.sh
/home/zhanhao/service/apollo1.4/dev/adminservice/scripts/shutdown.sh
/home/zhanhao/service/apollo1.4/dev/configservice/scripts/shutdown.sh
/home/zhanhao/service/apollo1.4/sit/adminservice/scripts/shutdown.sh
/home/zhanhao/service/apollo1.4/sit/configservice/scripts/shutdown.sh
/home/zhanhao/service/apollo1.4/uat/adminservice/scripts/shutdown.sh
/home/zhanhao/service/apollo1.4/uat/configservice/scripts/shutdown.sh
echo "==== All apollo Service Stoped ===="
3. 自定义添加环境
-
效果如下:
3.1 创建数据库
- 导入三份
apolloconfigdb.sql
并命名为ApolloConfigDB
,ApolloConfigDB_SIT
,ApolloConfigDB_UAT
- 修改
ServiceConfig
表,eureka.service.url
指向自己的Eureka地址
- 修改
ApolloPortalDB
ServiceConfig
apollo.portal.envs
配置dev,uat,sit
3.2 修改每个环境端口号及数据库连接
3.2.1 DEV
a) adminservice
i) 修改端口号:adminservice/scripts/startup.sh
SERVER_PORT=7030
#!/bin/bash
SERVICE_NAME=apollo-adminservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/100003172
## Adjust server port if necessary
SERVER_PORT=7030
## Create log directory if not existed because JDK 8+ won't do that
ii) 修改数据库连接: adminservice/config/application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
b) configservice
i) 修改端口号:configservice/scripts/startup.sh
SERVER_PORT=7020
#!/bin/bash
SERVICE_NAME=apollo-configservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/100003172
## Adjust server port if necessary
SERVER_PORT=7020
## Create log directory if not existed because JDK 8+ won't do that
ii) 修改数据库连接: configservice/config/application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
adminservice
SERVER_PORT=7030
adminservice
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB? characterEncoding=utf8
configservice
SERVER_PORT=7020
configservice
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB? characterEncoding=utf8
3.2.2 SIT
adminservice
SERVER_PORT=7031
adminservice
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB_SIT? characterEncoding=utf8
configservice
SERVER_PORT=7021
configservice
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB_SIT? characterEncoding=utf8
3.2.3 UAT
adminservice
SERVER_PORT=7032
adminservice
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB_UAT? characterEncoding=utf8
configservice
SERVER_PORT=7022
configservice
spring.datasource.url = jdbc:mysql://localhost:3307/ApolloConfigDB_UAT? characterEncoding=utf8
3.3 修改配置文件及代码
无论多少个环境都只需要一个 portal 服务,每个环境都需要自己的adminservice、configservice和数据库
例如:SIT环境都要额外再部署adminservice、configservice服务,并设置自己端口号;还需自己的ApolloConfigDB_SIT数据库
3.3.2 修改配置文件
-
build.bat
修改连接数据库及各环境IP、端口号;
apollo-env.properties
添加sit
local.meta=http://localhost:8080
dev.meta=${dev_meta}
fat.meta=${fat_meta}
uat.meta=${uat_meta}
lpt.meta=${lpt_meta}
pro.meta=${pro_meta}
sit.meta=${sit_meta}
3.3.2 修改代码
-
Env
com.ctrip.framework.apollo.core.enums
添加sit
public enum Env{
LOCAL, DEV, FWS, FAT, UAT, LPT, PRO, TOOLS, UNKNOWN, SIT;
public static Env fromString(String env) {
Env environment = EnvUtils.transformEnv(env);
Preconditions.checkArgument(environment != UNKNOWN, String.format("Env %s is invalid", env));
return environment;
}
}
-
EnvUtils
com.ctrip.framework.apollo.core.enums
添加sit
public final class EnvUtils {
public static Env transformEnv(String envName) {
if (StringUtils.isBlank(envName)) {
return Env.UNKNOWN;
}
switch (envName.trim().toUpperCase()) {
case "LPT":
return Env.LPT;
case "FAT":
case "FWS":
return Env.FAT;
case "UAT":
return Env.UAT;
case "PRO":
case "PROD": //just in case
return Env.PRO;
case "DEV":
return Env.DEV;
case "LOCAL":
return Env.LOCAL;
case "TOOLS":
return Env.TOOLS;
case "SIT":
return Env.SIT;
default:
return Env.UNKNOWN;
}
}
}
-
LegacyMetaServerProvider
com.ctrip.framework.apollo.core.internals
添加sit
private void initialize() {
Properties prop = new Properties();
prop = ResourceUtils.readConfigFile("apollo-env.properties", prop);
domains.put(Env.LOCAL, getMetaServerAddress(prop, "local_meta", "local.meta"));
domains.put(Env.DEV, getMetaServerAddress(prop, "dev_meta", "dev.meta"));
domains.put(Env.FAT, getMetaServerAddress(prop, "fat_meta", "fat.meta"));
domains.put(Env.UAT, getMetaServerAddress(prop, "uat_meta", "uat.meta"));
domains.put(Env.LPT, getMetaServerAddress(prop, "lpt_meta", "lpt.meta"));
domains.put(Env.PRO, getMetaServerAddress(prop, "pro_meta", "pro.meta"));
domains.put(Env.SIT, getMetaServerAddress(prop, "sit_meta", "sit.meta"));
}
3.4 执行脚本启动服务
#!/bin/sh
echo "==== 启动开发环境apollo服务 ===="
/home/zhanhao/service/apollo1.4/dev/configservice/scripts/startup.sh
/home/zhanhao/service/apollo1.4/dev/adminservice/scripts/startup.sh
echo "==== 启动sit环境apollo服务 ===="
/home/zhanhao/service/apollo1.4/sit/configservice/scripts/startup.sh
/home/zhanhao/service/apollo1.4/sit/adminservice/scripts/startup.sh
echo "==== 启动uat环境apollo服务 ===="
/home/zhanhao/service/apollo1.4/uat/configservice/scripts/startup.sh
/home/zhanhao/service/apollo1.4/uat/adminservice/scripts/startup.sh
echo "==== 启动portal apollo服务 ===="
/home/zhanhao/service/apollo1.4/portal/scripts/startup.sh