Eureka Server: How to achieve high availability

26,081

Eureka Discovery Server should be used in the Peer-Aware config mode in production setups. Check: http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_peer_awareness

For instance your first eureka server instance will have config like this:

server:
   port: 1111
eureka:
   instance:
      hostname: peer1
   client:
      serviceUrl:
           defaultZone: http://peer2:1112/eureka/

..and second server instance like this:

server:
   port: 1112
eureka:
   instance:
      hostname: peer2
   client:
      serviceUrl:
           defaultZone: http://peer1:1111/eureka/

When Eureka server instances will boot up they will look for each other. All microservices will register with them automatically, so if one goes down the other server instance will be always there. On both Eureka instances you will be able to see all the registered microservices. Like this you can scale-up and have multiple server instances in a production environment.

Note: If you are trying this on a single system, dont forget to edit the /etc/hosts file:
127.0.0.1 peer1
127.0.0.1 peer2

Share:
26,081
Neo
Author by

Neo

Eat Java.

Updated on July 18, 2022

Comments

  • Neo
    Neo almost 2 years

    I'm new to spring cloud. I've read this doc and it says the client application must specify a service url:

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

    But what if localhost:8761 goes down?

  • Neo
    Neo almost 8 years
    Thanks for your answer. In your example, supposing I start up a new microservice and set the serviceUrl to 'peer1:1111/eureka'. If peer1 happens to go down at this time, my microservice will be failed to startup. I think clients(micorservices) should be given the address of the whole 'cluster' instead of a single eureka instance, which means, all eureka instances should appear to be a single instance to the clients
  • Gurneet Sethi
    Gurneet Sethi almost 8 years
    Yes, if peer1 is down the microservice will fail with exception like this: ERROR DiscoveryClient-<service>- was unable to send heartbeat! com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server". Yes, right, the clients are not given the address of a single instance. I had my last configuration in which I had passed comma separated values of all the server instances. To balance these hits on Eureka servers I have also seen Load Balancers b/w microservices and Eureka Server instances.
  • Andy Brown
    Andy Brown about 7 years
    We do it in our company environment using a load-balancer to front the Eureka pair.
  • Clement.Xu
    Clement.Xu over 6 years
    Hi, is it limited to a pair only? Can I setup 3 instances and if one of them goes down, the other two can take over? Thanks.
  • Gurneet Sethi
    Gurneet Sethi over 6 years
    It is not limited to a pair. You can setup more instances and others will take over.
  • vijay b
    vijay b about 5 years
    How does it works in the case of zuul and eureka. can spring zuul loadbalance by connecting to eureka cluster .
  • samshers
    samshers almost 5 years
    @AndyBrown, will a load balancer fronting the eureka pair help. (1) a client will be speaking with only one eureka instance at startup and register itself with it. Then will the eureka server replicate this info to other peers. (2) what about heart beat, does client sending heart beat to any one eureka server do the trick of letting eureka cluster know about it being alive
  • Andy Brown
    Andy Brown almost 5 years
    @samshers. Yes the two Eureka servers replicate registrations and renewals (heartbeats) between each other.
  • samshers
    samshers almost 5 years
    @AndyBrown, that's wonderful. Incase to the client services I provide only one instance of Eureka (while a cluster of Eureka's exists), does the client have any way to find out about the cluster and add the Eureka peers to it's list of Eureka Instances. Or otherway, can Eureka server let the client know about the Eureka Cluster.
  • Andy Brown
    Andy Brown almost 5 years
    @samshers See this answer for how to make your client side resilient without a load balancer. Basically list all your servers in the client config separated by commas. Your clients will all hit the first one if it's up. The first server will replicate to the other. If the first server then dies then client heartbeats should fail over to the second, which because of replication already knows about the client. When server 1 comes back up it will re-sync automatically with #2. You can randomize the comma separated list order to share load.
  • Sanjiv Jivan
    Sanjiv Jivan over 4 years
    With each eureka server pointing to the other(s), how do you avoid the connectivity exception to the second peer when the first one is started and the second hasn’t been started yet.
  • Priyam
    Priyam over 4 years
    @AndyBrown, Could you please provide the configurations that you did when used load balancer. I am trying the same thing but microservices are not registered on all Eureka instances. You can refer to my setup here