1

我正在查看 Spring Boot / Cloud 和 Netflix FW(Eureka,Ribbon)。我正在处理这个例子:

https://spring.io/blog/2015/07/14/microservices-with-spring 基本上是关于一些使用 Eureka Service Registry 的小型 Spring Boot 微服务。

我现在想启动同一个服务的多个实例(在这个例子中是 AccountService,在不同的端口上)。我阅读的所有内容(以上文章,http://callistaenterprise.se/blogg/teknik/2015/04/10/building-microservices-with-spring-cloud-and-netflix-oss-part-1/等)表明,如果我这样做了,所有实例都冗余地向 Eureka 注册,当我调用服务时,应用客户端负载平衡并动态选择要调用的服务。

然而,这不是发生的事情。当我启动第一个服务实例时,它会注册并显示在 Eureka Dashboard 中。当我在不同的端口上启动相同的服务时,它也会注册,但它似乎替换了以前的服务实例:Eureka Dashboard 仍然只显示一个可用区 = 1(应该是 2?)的实例并且所有调用此服务由第二个实例处理。当我查询注册表时,只应用了这个实例。

当我停止第二个实例时,一段时间后尤里卡切换回第一个实例,它仍然有效。所以它似乎保留了所有实例,但只使用最新注册的实例。

我错过了什么重要的事情吗?我认为应该同时使用所有实例?

========== 应用程序属性是(这些实际上与 Spring 站点中的示例相同):

尤里卡服务器

eureka:  
  instance:  
    hostname: localhost  
  client:    
    registerWithEureka: false  
    fetchRegistry: false  

server:  
  port: 1111     

spring:  
  thymeleaf:  
    enabled: false  

帐户服务器

spring:  
  application:  
    name: accounts-service  
  freemarker:  
    enabled: false           
  thymeleaf:  
    cache: false             
    prefix: classpath:/accounts-server/templates/      

eureka:  
  client:  
    serviceUrl:  
      defaultZone: http://localhost:1111/eureka/  

server:  
  port: 4444   # HTTP (Tomcat) port, for the second instance this is changed to a different port
4

2 回答 2

4

@dshuld 如果您使用的是 Angel 发布系列或版本 1.0.x,您需要使每个实例都有一个唯一的 ID。请参阅此处的文档。就像是:

eureka:
  instance:
    metadataMap:
      instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}

对于 Brixton 发布系列 (1.1.x),它应该有一个合理的默认值。

于 2016-06-07T17:32:27.160 回答
0

我认为,由于您在 .Eureka 上使用静态端口号AccountsServer,Eureka 只会注册一个实例......
您可以在客户端上执行类似的操作

server:
  compression.enabled: true
  port: 0

(在这种情况下,我使用 YAML 进行配置,而不是属性)

在网关上,您应该指定您想要一个负载均衡器,就像这样

 @Autowired
 private LoadBalancerClient loadBalancer;
 ....
 private String getUrl(){
    return loadBalancer.choose("your-service-name").getUri().toString();
}

我做过类似的事情,如果你有兴趣可以看看这里

于 2016-06-07T12:46:10.003 回答