建立Eureka集群因配置defaultZone时使用localhost导致失败

Eureka在实际使用过程中,基本上都是使用集群的方式。在本地测试Eureka集群搭建过程中遇到集群不生效的问题,过程如下:

1.搭建2个节点的注册中心

给eureka-server模块添加中心1对应的配置文件application-ser1.yml

server:
  port: 8081
spring:
  application:
    name: eureka-ser1
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8082/eureka/
    fetch-registry: true
 register-with-eureka: true

给eureka-server模块添加中心2对应的配置文件application-ser2.yml

server:
  port: 8082
spring:
  application:
    name: eureka-ser2
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
    fetch-registry: true
    register-with-eureka: true

通过2个注册中心相互注册,搭建了双节点集群。

2.启动注册中心集群

登录http://localhost:8081/查看中心1界面:
image
登录http://localhost:8082/查看中心2界面:
image
从注册中心界面可以看出集群没有生效,服务没有共享。
查看日志找到以下内容:
2021-01-31 19:06:44.285 WARN 316 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : The replica size seems to be empty. Check the route 53 DNS Registry
注册中心找不到复制节点,所以集群建立失败。

3.集群建立失败原因分析

在eureka源码com.netflix.eureka.cluster.PeerEurekaNodes找到问题原因,该类为eureka集群节点相关类,

public boolean isThisMyUrl(String url) {
    String myUrlConfigured = this.serverConfig.getMyUrl();
    return myUrlConfigured != null ? myUrlConfigured.equals(url) : this.isInstanceURL(url, this.applicationInfoManager.getInfo());
}
public boolean isInstanceURL(String url, InstanceInfo instance) {
    String hostName = hostFromUrl(url);
    String myInfoComparator = instance.getHostName();
    if (this.clientConfig.getTransportConfig().applicationsResolverUseIp()) {
        myInfoComparator = instance.getIPAddr();
    }
    return hostName != null && hostName.equals(myInfoComparator);
}

这两个方法会把满足hostName.equals(myInfoComparator)这个条件的集群节点删除掉,即配置文件中的eureka.instance.hostname与eureka.client.serviceUrl.defaultZone中服务器主机名,因为上文中配置的都是localhost,所以集群节点都被删除,导致集群找不到节点。

4.修改eureka.instance.hostname重启服务

application-ser1.yml修改为:

eureka:
  instance:
    hostname: ser1

application-ser2.yml修改为:

eureka:
  instance:
    hostname: ser2

重新启动,查看注册中心1与2界面如下图:
image
查看日志,也可找到集群建立日志:

2021-01-31 20:28:12.251  INFO 1828 --- [           main] c.n.eureka.cluster.PeerEurekaNodes       : Adding new peer nodes [http://localhost:8081/eureka/]
2021-01-31 20:28:12.280  INFO 1828 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2021-01-31 20:28:12.281  INFO 1828 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2021-01-31 20:28:12.282  INFO 1828 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2021-01-31 20:28:12.282  INFO 1828 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2021-01-31 20:28:12.478  INFO 1828 --- [           main] c.n.eureka.cluster.PeerEurekaNodes       : Replica node URL:  http://localhost:8081/eureka/
5.启动客户端进行验证

application.yml配置:

server:
  port: 8890
spring:
  application:
    name: eureka-cli
eureka:
  client:
    register-with-eureka: true
 fetch-registry: true
 service-url:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/

启动后查看注册中心1和2界面如下:
image

大工告成,成功建立了eureka集群,完美解决问题。

你可能感兴趣的:(建立Eureka集群因配置defaultZone时使用localhost导致失败)