通过apollo源码,手把手教你部署apollo(实战篇)
手把手进行 apollo接入:
自定义apollo环境
自定义一个apollo部署环境也很简单,比如我们加入一个SIT环境,我们需要进行如下操作:
1. 修改apollo-core项目,com.ctrip.framework.apollo.core.enums.Env,在其中加入SIT枚举:
public enum Env{ LOCAL, DEV, FWS, FAT, UAT, LPT, PRO, TOOLS, UNKNOWN, SIT; ........}
2.修改apollo-core项目,com.ctrip.framework.apollo.core.enums.EnvUtils,在其中加入SIT枚举的转换逻辑:
public final class EnvUtils { //新增SIT环境规则 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 incase 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; } } }
3.修改apollo-core项目,com.ctrip.framework.apollo.core.internals.LegacyMetaServerProvider类,增加读取SIT环境的meta server地址逻辑:
public class LegacyMetaServerProvider implements MetaServerProvider { // make it as lowest as possible, yet not the lowest public static final int ORDER = MetaServerProvider.LOWEST_PRECEDENCE - 1; private static final Map domains = new HashMap<>(); public LegacyMetaServerProvider() { initialize(); } 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")); } }
4. 修改log日志文件生成路径:
修改apollo-adminservice项目下/script/startup.sh,日志路径替换为:
LOG_DIR=/usr/local/nlp/logs/apollo-adminservice/100003172。
修改apollo-configservice项目下/script/startup.sh,日志路径替换为:
LOG_DIR=/usr/local/nlp/logs/apollo-configservice/100003171。
修改apollo-portal项目下/script/startup.sh,日志路径替换为:
LOG_DIR=/usr/local/nlp/logs/apollo-portal/100003173。
经过如上四步我们完成了SIT环境的添加。
4.apollo打包
由于我们存在DEV,UAT,SIT,PRO四套环境,apollo-configservice,apollo-adminservice项目需要切换四次环境打包。apollo-portal项目只需打包一次。
#dev 环境
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am
-Dapollo_profile=github
-Dspring_datasource_url=jdbc:mysql://127.0.0.1:3306/apollo_configdb_dev?characterEncoding=utf8
-Dspring_datasource_username=root
-Dspring_datasource_password=cc123456
#daily环境
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am
-Dapollo_profile=github
-Dspring_datasource_url=jdbc:mysql://127.0.0.1:3306/apollo_configdb_daily?characterEncoding=utf8
-Dspring_datasource_username=root
-Dspring_datasource_password=cc123456
#sit环境
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am
-Dapollo_profile=github
-Dspring_datasource_url=jdbc:mysql://127.0.0.1:3306/apollo_configdb_sit?characterEncoding=utf8
-Dspring_datasource_username=root
-Dspring_datasource_password=cc123456
#pro环境
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am
-Dapollo_profile=github
-Dspring_datasource_url=jdbc:mysql://127.0.0.1:3306/apollo_configdb_pro?characterEncoding=utf8
-Dspring_datasource_username=root
-Dspring_datasource_password=cc123456
#portal
mvn clean package
-DskipTests -pl apollo-portal -am -Dapollo_profile=github,auth
-Dspring_datasource_url=jdbc:mysql://127.0.0.1:3306/apollo_portaldb?characterEncoding=utf8
-Dspring_datasource_username=root
-Dspring_datasource_password=cc123456
-Ddev_meta=http://10.xxx.xx.127:8080
-Dsit_meta=http://10.xxx.xx.129:8080
-Duat_meta=http://10.xxx.xx.128:8080
-Dpro_meta=http://10.xxx.xx.130:8080
5.apollo部署
在自己的服务器上新建一个目录 /usr/local/wxt/apollo_xxx/ 将官方提供的安装包直接下载到这个目录下,然后解压:
unzip apollo-adminservice-1.4.0-github.zip -d apollo-adminservice-1.4.0-github
unzip apollo-configservice-1.4.0-github.zip -d apollo-configservice-1.4.0-github
unzip apollo-portal-1.4.0-github.zip -d apollo-portal-1.4.0-github
6. 启动项目
在每一个工程的解压包中,都有一个 scripts 文件夹,这里面是 Apollo 工程的启动脚本。
三个工程分别先后启动:apollo-configservice、apollo-adminservice、apollo-portal,就是分别执行这三个工程下面的 /scripts/startup.sh 脚本即可,关闭执行的是
原文链接:https://blog.csdn.net/weixin_31869579/article/details/112678708