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(==非常重要==)
<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);
	}

}

启动

1、本地启动

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

2、环境上启动

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

常见错误问题情景

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]]
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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值