The previous article introduced how to use Eureka to build a microservice registration and discovery center. This blog post briefly introduces how to build a multi-node highly available service registration center.
The design of Eureka Server considers the issue of high availability from the very beginning. In the service governance design of Eureka, all service nodes are both service providers and service consumers, and the service registry is no exception. In the single-node configuration in the previous section, we set the following parameters to prevent the registry from registering itself:
eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
The high availability of Eureka Server is actually to register itself as a service with other service registries, so that a set of mutually registered service registries can be formed to achieve mutual synchronization of service lists and achieve the effect of high availability. Let's try to build a cluster of highly available service registry. Extend the program in the previous section to build a two-node service registry cluster.
(1) Create the application-peer1.properties configuration file as the configuration of the peer1 service center, and point the serviceUrl to peer2:
server.port=1111 server.application.name=eureka-server eureka.instance.hostname=peer1 spring.application.name=peer1-service eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
(2) Create the application-peer2.properties configuration file as the configuration of the peer2 service center, and point the serviceUrl to peer1:
server.port=1112 server.application.name=eureka-server eureka.instance.hostname=peer2 spring.application.name=peer2-service eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
(3) Add configuration to the local hosts file so that the above domain name can be accessed:
127.0.0.1 peer1 127.0.0.1 peer2
(4) Delete or comment out the following configuration of the stand-alone version to prevent the node from not registering itself:
eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1 java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2
At this time, visit the panels http://localhost:1111/ and http://localhost:1112/ of the registration center peer1 and peer2. You can see that there are already two registered services in Instances currently registered with Eureka.
After setting up the multi-node registration center, the service provider needs to do some simple configuration to register the service in the Eureka Server cluster. Taking hello-service as an example, modify the eureka.client in the application.properties configuration file. The serviceUrl.defaultZone configuration points the registry to peer1 and peer2 we built earlier:
spring.application.name=hello-service eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/
Now we start hello-service, and we can observe that hello-service is registered with peer1 and peer2 at the same time. At this time, if peer1 hangs, we can still access hello-service on peer2, thus realizing the high availability of the registry.