Eureka is an open source service discovery component of Netfilx, which is itself a REST-based service. It consists of EurekaServer and EurekaClient. SpringCloud integrates it into the sub-project SpringCloud Netfilx to realize the registration and discovery of microservices. Eureka as a service registry plays a central role in the integration of the entire microservices architecture. Eureka is simply an open source product provided by Netflix to provide service registration and discovery. The glue code provided by Netflix has changed some initial configuration and added a more user-friendly interface. Spring Cloud Netflix makes Eureka easier to use, so let’s take a look at how to make Eureka highly available.

The fault of one server does not affect the overall service status and cannot cause the service to stop due to the fault of one server. There are three methods of high availability: master/slave mode, dual-node duplex mode, and cluster working mode. Zookeeper adopts the master-slave mode, while Eureka adopts the cluster mode. When multiple servers register with each other, high availability is formed. In this way, when one server stops providing services, the rest will continue to provide services.

We need to change the original registration mode of a single EurekaClient and a single EurekaServer to two single EurekaserVers registering with each other, and then the clients register on the two EurekaServer respectively. So even if one of the single EurekaServer fails, the other can continue to work. Implements the simplest highly available architecture. So how do you do that?

one

We need to annotate @enableEurekaserver in the xxApplication class to indicate that this is a EurekaServer, and then configure it as follows:

Eureka: client: service - url: defaultZone: http://127.0.0.1:8761/eureka/ register - with - eureka: false server: enable-self-preservation: false spring: application: name: eurekaCopy the code

Next I open two EurekaServer ports, 8761 and 8762 respectively:

At the time of startup EurekaServer1, its registered address change to http://127.0.0.1:8762/eureka/, at the time of startup EurekaServer2, It’s registered address is changed to http://127.0.0.1:8761/eureka/, so two EurekaServer is complete registration.

To note here, although 127.0.0.1 localhost in is the same as the one we use at ordinary times, but if wrote http://localhost:8761/eureka/ will not be able to register each other here, be sure to write 127.0.0.1, If not, you need to modify the Windows Host file and replace localhost with a fake domain name.

There is a problem, that is why the registered address is http://127.0.0.1:8761/eureka/? Why is context-path eureka, because in SpringCloud, context-path is Eureka

Starting two Eurekaservers, we can see the result of mutual registration:

two

Next we register a CLient with one of the EurekaServer1:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: clientA
Copy the code

However, when we open EurekaServer2, we find that the Client is registered with EurekaServer2:

The disadvantage of this is that if EurekaServer1 fails, the Client cannot register with EurekaServer2. To avoid this, the Client needs to register with multiple Eurekaservers. The configuration file can be written like this (and don’t forget to annotate @enableDiscoveryClient on the xxApplication class) :

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
spring:
  application:
    name: clientA
Copy the code

In fact, we can also get three EurekaServer to register with each other, as shown in the picture below:

We need to turn on another EurekaServer3:

Then the configuration of these three EurekaServer at startup is as follows:

eureka: client: service-url: defaultZone: http://127.0.0.1:8762/eureka/, http://127.0.0.1:8763/eureka/ register - with - eureka: false server: enable-self-preservation: false spring: application: name: eureka ##################### EurekaServer2 eureka: client: Service - url: defaultZone: http://127.0.0.1:8761/eureka/, http://127.0.0.1:8763/eureka/ register - with - eureka: false server: enable-self-preservation: false spring: application: name: eureka ##################### EurekaServer3 eureka: client: service-url: defaultZone: http://127.0.0.1:8761/eureka/, http://127.0.0.1:8762/eureka/ register - with - eureka: false server: enable-self-preservation: false spring: application: name: eurekaCopy the code

The above describes two-node registries and three-node registries. In a production environment, at least three or more registries are required to ensure high availability of services. However, it is important to understand how Eureka’s heartbeat detection, health checks, and load balancing are done. It is also important to understand the microservices architecture, with service registries being the most fundamental part.