SpringCloud从零开始-微服务客户端-DiscoveryClient[EurekaClient]

说明

SpringBoot版本:2.3.4.RELEASE
SpringCloud版本: Hoxton.SR8

遇到的问题

  1. 没有引入spring-boot-starter-web导致 com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient 客户端启动失败
    在这里插入图片描述

项目结构

在这里插入图片描述

POM内容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.xerbeauty</groupId>
		<artifactId>spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>spring-cloud-eureka-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-cloud-eureka-client</name>
	<description>SpringCloud 客户端</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<!-- 这个组件如果注释,客户端就会启动失败,什么原因暂未找到我觉得可以加个 //TODO: -->
<!--		<dependency>-->
<!--			<groupId>org.springframework.boot</groupId>-->
<!--			<artifactId>spring-boot-starter-web</artifactId>-->
<!--		</dependency>-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

application.yml

server:
  port: 10001
spring:
  application:
    name: EurekaClient-10001
  profiles:
    active: 10001
  cloud:
    loadbalancer:
      ribbon:
        enabled: false
eureka:
  instance:
    appname: EurekaClient-10001
    app-group-name: EurekaClientGroup_1
  client:
#    register-with-eureka: true
# 同时向两个注册中心去注册自己,但是我觉得这样有点问题,如果注册中心有不止两个,三个或者更多,这里岂不是要爆炸
# 如果有大佬知道这里怎么解决这个问题,请留言或者私聊我,感激不尽 //TODO:
    service-url:
      defaultZero: http://localhost:8761/eureka/,http://localhost:8762/eureka/

访问http://127.0.0.1:8762/

在这里插入图片描述

访问http://127.0.0.1:8761/

在这里插入图片描述

试着跟踪一下客户端的心跳机制

/Users/${user}/.m2/repository/org/springframework/cloud/spring-cloud-netflix-eureka-client/2.2.5.RELEASE/spring-cloud-netflix-eureka-client-2.2.5.RELEASE.jar!/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.netflix.eureka.config.EurekaClientConfigServerAutoConfiguration,\
org.springframework.cloud.netflix.eureka.config.DiscoveryClientOptionalArgsConfiguration,\
org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration,\
org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration,\
org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration,\
org.springframework.cloud.netflix.eureka.reactive.EurekaReactiveDiscoveryClientConfiguration,\
org.springframework.cloud.netflix.eureka.loadbalancer.LoadBalancerEurekaAutoConfiguration

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.netflix.eureka.config.EurekaConfigServerBootstrapConfiguration

挨个儿点进去看实现,EurekaClientAutoConfiguration这个配置类好像是那么回事儿
在这里插入图片描述
控制台出现最多的一个字眼 DiscoveryClient,再联系一下EurekaClientAutoConfiguration的内容
在这里插入图片描述

@EurekaClientAutoConfiguration.RefreshableEurekaClientConfiguration#eurekaClient
@Configuration(proxyBeanMethods = false)
@ConditionalOnRefreshScope
protected static class RefreshableEurekaClientConfiguration {
	@Autowired
	private ApplicationContext context;
	@Autowired
	private AbstractDiscoveryClientOptionalArgs<?> optionalArgs;
	@Bean(destroyMethod = "shutdown")
	@ConditionalOnMissingBean(value = EurekaClient.class,
			search = SearchStrategy.CURRENT)
	@org.springframework.cloud.context.config.annotation.RefreshScope
	@Lazy
	public EurekaClient eurekaClient(ApplicationInfoManager manager,
			EurekaClientConfig config, EurekaInstanceConfig instance,
			@Autowired(required = false) HealthCheckHandler healthCheckHandler) {
		// If we use the proxy of the ApplicationInfoManager we could run into a
		// problem
		// when shutdown is called on the CloudEurekaClient where the
		// ApplicationInfoManager bean is
		// requested but wont be allowed because we are shutting down. To avoid this
		// we use the
		// object directly.
		ApplicationInfoManager appManager;
		if (AopUtils.isAopProxy(manager)) {
			appManager = ProxyUtils.getTargetObject(manager);
		}
		else {
			appManager = manager;
		}
		CloudEurekaClient cloudEurekaClient = new CloudEurekaClient(appManager,
				config, this.optionalArgs, this.context);
		// 注册健康检测  语义化的命名真是太重要了
		cloudEurekaClient.registerHealthCheck(healthCheckHandler);
		return cloudEurekaClient;
	}
	@Bean
	@ConditionalOnMissingBean(value = ApplicationInfoManager.class,
			search = SearchStrategy.CURRENT)
	@org.springframework.cloud.context.config.annotation.RefreshScope
	@Lazy
	public ApplicationInfoManager eurekaApplicationInfoManager(
			EurekaInstanceConfig config) {
		InstanceInfo instanceInfo = new InstanceInfoFactory().create(config);
		return new ApplicationInfoManager(config, instanceInfo);
	}
	@Bean
	@org.springframework.cloud.context.config.annotation.RefreshScope
	@ConditionalOnBean(AutoServiceRegistrationProperties.class)
	@ConditionalOnProperty(
			value = "spring.cloud.service-registry.auto-registration.enabled",
			matchIfMissing = true)
	public EurekaRegistration eurekaRegistration(EurekaClient eurekaClient,
			CloudEurekaInstanceConfig instanceConfig,
			ApplicationInfoManager applicationInfoManager, @Autowired(
					required = false) ObjectProvider<HealthCheckHandler> healthCheckHandler) {
		return EurekaRegistration.builder(instanceConfig).with(applicationInfoManager)
				.with(eurekaClient).with(healthCheckHandler).build();
	}

}

其实看到这里 已经知道接下来的代码该怎么去看了。具体的详细的步骤,我也不是很清楚,慢慢看咯,这里给大家一个寻找工具核心代码的方法。请大佬点赞,谢谢!!!!!!!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码ge寂寞

谢谢老板,老板大气,老板入大厂

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值