Eureka 节点不同步

Eureka peers not synchronized

我正在制作一组 Spring 云 + Netflix OSS 应用程序的原型,并且 运行 遇到了 Eureka 的问题。在我们的设置中,我们有一个 Spring Cloud Config Server + Eureka Server,然后是 2 个利用该服务器组件进行引导和服务发现的模块。

我 运行 遇到的问题是,如果我启动 2 个 Eureka Server 实例并尝试将它们配对(基于 Two Peer Aware Eureka Serversthe docs) they don't synchronize with each other. See configs below and/or the code on GitHub.

基本上,Peer1 启动并看起来很好。 Peer2 将启动并且看起来很好,两个对等点在服务中相互显示。但是,如果 "UserService" 模块启动并向 Peer1 注册,Peer2 将永远看不到它。如果我们然后启动指向 Peer2 的 "Web" 模块,它永远无法解析 UserService。他们基本上是孤立行动的。

我尝试了几种在服务器和 Eureka 服务器实例上设置 serviceUrl 的组合,但无济于事。我只是配置错了吗?

节点 1 / 默认配置:

server:
  port: 8888

eureka:
  dashboard:
    path: /dashboard
  instance:
    hostname: peer1
    leaseRenewalIntervalInSeconds: 3
  client:
      serviceUrl:
          defaultZone: ${eureka.server.serviceUrl:http://localhost:${server.port}/eureka/}
  server:
    serviceUrl:
        defaultZone: http://localhost:${server.port}/eureka/
        peer2: http://peer2/eureka/
    waitTimeInMsWhenSyncEmpty: 0


spring:
  application:
    name: demo-config-service
  profiles:
    active: native
  # required for Spring Cloud Bus
  rabbitmq:
    host: ${DOCKER_IP:192.168.59.103}
    port: 5672
    username: guest
    password: guest
    virtualHost: /
  cloud:
    config:
      server:
        prefix: /configs
        native:
          searchLocations: /Users/dave/workspace/oss/distributed-spring/modules/config-server/src/main/resources/testConfigs
#        git :
#          uri: https://github.com/joshlong/microservices-lab-configuration

对等 2 配置:

server:
  port: 8889

eureka:
  dashboard:
    path: /dashboard
  instance:
    hostname: peer2
    leaseRenewalIntervalInSeconds: 3
  client:
      serviceUrl:
          defaultZone: ${eureka.server.serviceUrl:http://localhost:${server.port}/eureka/}
  server:
    serviceUrl:
        defaultZone: http://localhost:8888/eureka/
        peer1: http://peer1/eureka/
    waitTimeInMsWhenSyncEmpty: 0


spring:
  application:
    name: demo-config-service
  profiles:
    active: native
  # required for Spring Cloud Bus
  rabbitmq:
    host: ${DOCKER_IP:192.168.59.103}
    port: 5672
    username: guest
    password: guest
    virtualHost: /
  cloud:
    config:
      server:
        prefix: /configs
        native:
          searchLocations: /Users/dave/workspace/oss/distributed-spring/modules/config-server/src/main/resources/testConfigs
#        git :
#          uri: https://github.com/joshlong/microservices-lab-configuration

有一些问题。如文档中所述,defaultZone 需要位于客户端部分。 defaultZone url 需要端口。

/etc/hosts

127.0.0.1       peer1
127.0.0.1       peer2

Peer 1 配置(部分)

eureka:
  instance:
    hostname: peer1
    leaseRenewalIntervalInSeconds: 3
  client:
    serviceUrl:
      defaultZone: http://peer2:8889/eureka/

Peer 2 配置(部分)

eureka:
  dashboard:
    path: /dashboard
  instance:
    hostname: peer2
    leaseRenewalIntervalInSeconds: 3
  client:
    serviceUrl:
      defaultZone: http://peer1:8888/eureka/
  server:
    waitTimeInMsWhenSyncEmpty: 0

用户服务配置(部分)配置端口错误。

spring:
  application:
    name: user-service
  cloud:
    config:
      uri: http://localhost:8888/configs

您可以看到用户服务已复制到 peer1 和 peer2。如果你愿意,我可以 post 对你的代码进行 PR。

同行 1

同行 2

@spencergibb 没有提到为什么需要这种 hack-ish 解决方法。在同一主机上有一个 运行 多个 Eureka 服务器的问题。 Netflix 代码 (com.netflix.eureka.cluster.PeerEurekaNodes.isThisMyUrl) 过滤掉同一主机上的对等 URL。这样做可能是为了防止服务器注册为自己的对等点(我在这里猜测),但是因为它们不检查端口,所以对等点意识不起作用,除非 eureka.client.serviceUrl.defaultZone 中的 Eureka 主机名是不同的。解决这个问题的 hacky 解决方法是定义唯一的主机名,然后将它们映射到 /etc/hosts 文件(或其 Windows 等效文件)中的 127.0.0.1

我创建了一个包含 Eureka here 详细信息的博客 post,它填补了 Spring 文档或 Netflix 博客中缺少的一些细节。这是几天调试和挖掘源代码的结果。