Eureka客户端源代码启动流程(springboot)

本文详细解析了Eureka客户端的启动流程,包括EurekaRegistration、EurekaClientAutoConfiguration和DiscoveryClient的实例化。客户端通过EurekaAutoServiceRegistration实现SmartLifecycle接口自动启动,并定期从服务端同步注册信息,使用两个缓存(readOnlyCacheMap和readWriteCacheMap)进行数据同步。此外,客户端通过线程池进行服务注册,涉及缓存同步和心跳机制。
摘要由CSDN通过智能技术生成

EurekaAutoServiceRegistration实现了SmartLifecycle接口,所以spring会自动加载start方法,代码:

@Override
	public void start() {
   
		// only set the port if the nonSecurePort or securePort is 0 and this.port != 0
		if (this.port.get() != 0) {
   
			if (this.registration.getNonSecurePort() == 0) {
   
				this.registration.setNonSecurePort(this.port.get());
			}

			if (this.registration.getSecurePort() == 0 && this.registration.isSecure()) {
   
				this.registration.setSecurePort(this.port.get());
			}
		}

		// only initialize if nonSecurePort is greater than 0 and it isn't already running
		// because of containerPortInitializer below
		if (!this.running.get() && this.registration.getNonSecurePort() > 0) {
   
			//开始注册
			this.serviceRegistry.register(this.registration);

			this.context.publishEvent(new InstanceRegisteredEvent<>(this,
					this.registration.getInstanceConfig()));
			this.running.set(true);
		}
	}

获取eureka客户端实例

EurekaRegistration

public CloudEurekaClient getEurekaClient() {
   
		if (this.cloudEurekaClient.get() == null) {
   
			try {
   
				this.cloudEurekaClient.compareAndSet(null,
						getTargetObject(eurekaClient, CloudEurekaClient.class));
			}
			catch (Exception e) {
   
				log.error("error getting CloudEurekaClient", e);
			}
		}
		return this.cloudEurekaClient.get();
	}

客户端实例

EurekaClientAutoConfiguration

@Bean(destroyMethod = "shutdown")
		@ConditionalOnMissingBean(value = EurekaClient.class,
				search = SearchStrategy.CURRENT)
		@org.springframework.cloud.context.config.annotation.RefreshScope
		@
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值