当前位置:   article > 正文

Apollo搭建使用

apollo搭建

Apollo的执行过程

image

Apollo的原理

Client(Java应用端)通过域名访问Meta Server获取Config Service服务列表(IP+Port),而后直接通过IP+Port访问服务,同时在Client侧会做load balance、错误重试

image

Apollo的引入

1、准备工作

准备Apollo域名

2、项目引入

  • 新建Springboot项目;
  • 1、公司私服地址:https://xxxxxxx.com
  • 2、本文中Springboot版本为:spring-boot-starter-parent  2.5.6
  • pom引入 Apollo是经过公司技术调整的新包,故需要引入以下pom(==非常重要==)
  1. <dependency>
  2. <groupId>com.ctrip.framework.apollo</groupId>
  3. <artifactId>apollo-client</artifactId>
  4. <version>1.5.1.7</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.ctrip.framework.apollo</groupId>
  8. <artifactId>apollo-core</artifactId>
  9. <version>1.5.1.7</version>
  10. </dependency>
  • 新增配置文件 bootstrap.properties
  1. //相对来说重要、用来匹配配置数据
  2. app.id=vx-tms-order
  3. apollo.bootstrap.enabled=true
  4. apollo.bootstrap.eagerLoad.enabled=true
  5. apollo.bootstrap.namespaces=application.properties
  • 新增配置文件 apollo-env.properties 主要用来做环境区分 (==ip地址也很重要、需要找运维要==)
  1. //对应的ip地址也需要找运维要一个,否则本地拉不到
  2. dev.meta=http://40.73.114.140:8040/
  • 代码 启动类添加注解 @EnableApolloConfig
  1. @EnableApolloConfig
  2. @SpringBootApplication
  3. public class OrderApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(OrderApplication.class, args);
  6. }
  7. }

启动

1、本地启动

  • 正常情况下,本地可以是读取到配置的,并无其他特殊配置;如果有报错、则看常见错误问题解决

2、环境上启动

  • 正常环境上启动为运维在配置启动命令时,直接配置上去

常见错误问题情景

1、本地读取不到配置

错误截图:

  1. 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]]
  2. 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]]
  3. 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. 1、是SpringCloud项目、并且没有包含spring-cloud-starter-bootstrap 则需要引入pom
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-bootstrap</artifactId>
  5. </dependency>
  6. 2、引入了本地还爆读不到配置,则再检查是不是读取不到 apollo-env.properties
  7. PS:(目前这个问题没找到原因,替代方案是 在idea的启动命令里,配置上 -Denv=dev)

2、本地能读取apollo的pom包,但是部署时,取不到、报错如下

  1. 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]]
  2. 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]]
  3. 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代码中指定对应的仓库;具体的直接在私服中搜寻后,手动替换

  1. <repositories>
  2. <repository>
  3. <id>vxmaven</id>
  4. <name>vxmaven</name>
  5. <url>https://nexus3.vx56.com/repository/maven-releases/</url>
  6. </repository>
  7. <repository>
  8. <id>vx-local-maven</id>
  9. <name>vx-local-maven</name>
  10. <url>http://40.73.68.126:8080/repository/vx-hosted/</url>
  11. </repository>
  12. <repository>
  13. <id>vx-local-maven-proxy</id>
  14. <name>vx-local-maven</name>
  15. <url>http://40.73.68.126:8080/repository/vx-proxy/</url>
  16. </repository>
  17. </repositories>
  18. <pluginRepositories>
  19. <pluginRepository>
  20. <id>vxmaven</id>
  21. <name>vxmaven</name>
  22. <url>https://nexus3.vx56.com/repository/maven-releases/</url>
  23. </pluginRepository>
  24. <pluginRepository>
  25. <id>vx-local-maven</id>
  26. <name>vx-local-maven</name>
  27. <url>http://40.73.68.126:8080/repository/vx-hosted/</url>
  28. </pluginRepository>
  29. <pluginRepository>
  30. <id>vx-local-maven-proxy</id>
  31. <name>vx-local-maven-proxy</name>
  32. <url>http://40.73.68.126:8080/repository/vx-proxy/</url>
  33. </pluginRepository>
  34. </pluginRepositories>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/899838
推荐阅读
相关标签
  

闽ICP备14008679号