尤里卡同行没有同步

时间:2015-05-17 16:02:30

标签: spring-cloud netflix netflix-eureka

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

我遇到的问题是,如果我启动Eureka Server的2个实例并尝试配对它们(基于the docs中的两个Peer Aware Eureka服务器)彼此同步。请参阅下面的配置和/或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

2 个答案:

答案 0 :(得分:7)

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

<强>的/ etc /主机

127.0.0.1       peer1
127.0.0.1       peer2

对等1配置(部分)

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

对等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复制的用户服务。如果您愿意,我可以在您的代码中发布PR。

同行1

Peer1

同行2

Peer2

答案 1 :(得分:2)

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

我创建了一篇博文,其中详细介绍了Eureka here,其中包含了来自Spring doc或Netflix博客的一些缺失细节。这是几天调试和挖掘源代码的结果。