Search code examples
spring-cloud-netflix

Same zone calls in Spring-cloud eureka


I am trying to run some apps in two different zones : office and shahbour

based on my reading if i set preferSameZoneEureka to true then applications within same zone should always talk together but in my case it is doing round robbin . Below is my application.yml that is common to all applications

eureka:
  client:
    preferSameZoneEureka: true
    region: lebanon
    serviceUrl:
      office: http://localhost:8761/eureka/
      shahbour: http://192.168.15.202:8761/eureka/
    availabilityZones:
      lebanon: office
  instance:
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
      zone: office
hystrix:
  command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

---
spring:
  profiles: shahbour
eureka:
  instance:
    metadataMap:
      zone: shahbour
  client:
    availabilityZones:
      lebanon: shahbour

My understanding is that all application that have profile shahbour active should talk to each other unless it is not found they fall back to applications in zone office


Solution

  • i found out that i need two eureka to be able to accomplish the above , one in each zone

    below is my eureka configuration

    server:
      port: ${PORT:8761}
    ---
    spring:
      profiles: office
    eureka:
      instance:
        hostname: office
      client:
        serviceUrl:
          office: http://office:8761/eureka/
          shahbour: http://shahbour:8761/eureka/
    ---
    spring:
      profiles: shahbour
    eureka:
      instance:
        hostname: shahbour
      client:
        serviceUrl:
          office: http://office:8761/eureka/
          shahbour: http://shahbour:8761/eureka/
    

    and for the services

    eureka:
      client:
        preferSameZoneEureka: true
        region: lebanon
        serviceUrl:
          office: http://office:8761/eureka/
          shahbour: http://shahbour:8761/eureka/
        availabilityZones:
          lebanon: office,shahbour
      instance:
        leaseRenewalIntervalInSeconds: 10
        metadataMap:
          instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
          zone: office
    hystrix:
      command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
    
    ---
    spring:
      profiles: shahbour
    eureka:
      instance:
        metadataMap:
          zone: shahbour
      client:
        availabilityZones:
          lebanon: shahbour,office
    

    by doing so , i am able to use any service on the office zone but as soon i start that service on my own environment (zone) i start using it.