Apollo的执行过程
Apollo的原理
Client(Java应用端)通过域名访问Meta Server获取Config Service服务列表(IP+Port),而后直接通过IP+Port访问服务,同时在Client侧会做load balance、错误重试
Apollo的引入
准备Apollo域名
- 新建Springboot项目;
- 1、公司私服地址:https://xxxxxxx.com
- 2、本文中Springboot版本为:spring-boot-starter-parent 2.5.6
- pom引入 Apollo是经过公司技术调整的新包,故需要引入以下pom(==非常重要==)
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.5.1.7</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>1.5.1.7</version>
</dependency>
- 新增配置文件 bootstrap.properties
//相对来说重要、用来匹配配置数据
app.id=vx-tms-order
apollo.bootstrap.enabled=true
apollo.bootstrap.eagerLoad.enabled=true
apollo.bootstrap.namespaces=application.properties
- 新增配置文件 apollo-env.properties 主要用来做环境区分 (==ip地址也很重要、需要找运维要==)
//对应的ip地址也需要找运维要一个,否则本地拉不到
dev.meta=http://40.73.114.140:8040/
- 代码 启动类添加注解 @EnableApolloConfig
@EnableApolloConfig
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
启动
- 正常情况下,本地可以是读取到配置的,并无其他特殊配置;如果有报错、则看常见错误问题解决
- 正常环境上启动为运维在配置启动命令时,直接配置上去
常见错误问题情景
2022-07-27 19:24:45.405 WARN 18980 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 2 seconds. appId: vx-tms-app, cluster: default, namespaces: application.properties, long polling url: null, reason: Get config services failed from http://apollo.meta/services/config?appId=vx-tms-app&ip=10.92.33.45 [Cause: Could not complete get operation [Cause: apollo.meta]]
2022-07-27 19:24:47.424 WARN 18980 --- [ main] c.c.f.a.i.AbstractConfigRepository : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Get config services failed from http://apollo.meta/services/config?appId=vx-tms-app&ip=10.92.33.45 [Cause: Could not complete get operation [Cause: apollo.meta]]
2022-07-27 19:24:49.443 WARN 18980 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 4 seconds. appId: vx-tms-app, cluster: default, namespaces: application.properties+application, long polling url: null, reason: Get config services failed from http://apollo.meta/services/config?appId=vx-tms-app&ip=10.92.33.45 [Cause: Could not complete get operation [Cause: apollo.meta]]
1、是SpringCloud项目、并且没有包含spring-cloud-starter-bootstrap 则需要引入pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
2、引入了本地还爆读不到配置,则再检查是不是读取不到 apollo-env.properties
PS:(目前这个问题没找到原因,替代方案是 在idea的启动命令里,配置上 -Denv=dev)
2、本地能读取apollo的pom包,但是部署时,取不到、报错如下
2022-07-27 19:24:45.405 WARN 18980 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 2 seconds. appId: vx-tms-app, cluster: default, namespaces: application.properties, long polling url: null, reason: Get config services failed from http://apollo.meta/services/config?appId=vx-tms-app&ip=10.92.33.45 [Cause: Could not complete get operation [Cause: apollo.meta]]
2022-07-27 19:24:47.424 WARN 18980 --- [ main] c.c.f.a.i.AbstractConfigRepository : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Get config services failed from http://apollo.meta/services/config?appId=vx-tms-app&ip=10.92.33.45 [Cause: Could not complete get operation [Cause: apollo.meta]]
2022-07-27 19:24:49.443 WARN 18980 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 4 seconds. appId: vx-tms-app, cluster: default, namespaces: application.properties+application, long polling url: null, reason: Get config services failed from http://apollo.meta/services/config?appId=vx-tms-app&ip=10.92.33.45 [Cause: Could not complete get operation [Cause: apollo.meta]]
本地和环境上分属不同的私服库,需要在pom代码中指定对应的仓库;具体的直接在私服中搜寻后,手动替换
<repositories>
<repository>
<id>vxmaven</id>
<name>vxmaven</name>
<url>https://nexus3.vx56.com/repository/maven-releases/</url>
</repository>
<repository>
<id>vx-local-maven</id>
<name>vx-local-maven</name>
<url>http://40.73.68.126:8080/repository/vx-hosted/</url>
</repository>
<repository>
<id>vx-local-maven-proxy</id>
<name>vx-local-maven</name>
<url>http://40.73.68.126:8080/repository/vx-proxy/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>vxmaven</id>
<name>vxmaven</name>
<url>https://nexus3.vx56.com/repository/maven-releases/</url>
</pluginRepository>
<pluginRepository>
<id>vx-local-maven</id>
<name>vx-local-maven</name>
<url>http://40.73.68.126:8080/repository/vx-hosted/</url>
</pluginRepository>
<pluginRepository>
<id>vx-local-maven-proxy</id>
<name>vx-local-maven-proxy</name>
<url>http://40.73.68.126:8080/repository/vx-proxy/</url>
</pluginRepository>
</pluginRepositories>