Preface:

In spring-Cloud-based microservices architecture, all microservices need to be registered with a registry. If the registry is blocked or collapsed, the entire system cannot continue to provide services properly. Therefore, the registry needs to be clustered, in other words, high availability (HA) premise:

Read and complete the first registry project without changing the environment. This article is a simulation of high availability, you can copy the project of two registries and modify their configuration files separately to achieve the same effect. Modify hosts by adding two lines at the end of the file: 127.0.0.1 peer1127.0.0.1 Peer2

Recommended use of notepad++, if win10 will remind mention permissions, confirm, and then save. Project construction:

Open the registry project and create an application-peer1.properties URL = under SRC/Resources

Port = 1111server.port=1111# Here in the hosts file to modify the eureka. The instance. The hostname = peer1 # to the second registry his eureka. The client. The service – url. DefaultZone = http://peer2:1112/eureka/ url=

Create an application-peer2.properties file under SRC /resources [url=]


[/url]

Name =eureka-server# service port 1112server.port=1112# service domain name Here in the hosts file to modify the eureka. The instance. The hostname = peer2 # to eureka first registry itself. The client. The service – url. DefaultZone = http://peer1:1111/eureka/ [url=]


[/url]

This article uses IDEA to test, first need to jar the project, because in POM.xml has introduced maven compilation tool and packaging tool and specified the packaging format as jar package, here directly operation, the top right of the screen

The jar package is packaged and is located in the Target folder, as shown

Open Terminal at the bottom of the screen

After opening terminal, the jar package is in the target directory, and the current directory is the project directory, so first CD to the target directory and then enter the following command: Tip: When entering a command, you can enter the first few letters of the file name to avoid typing the wrong file name. Then use TAB to automatically complete Java -jar eurekaserverDemo-0.0.1 – snapshot. jar — spring-profiles. active=peer1

After entering this command, the project for the peer1 profile is launched, as shown in the figure

Note: we are using the same project here, so you can use both projects to test, and you need to be careful that the ports do not conflict

Java jar EurekaServerDemo – 0.0.1 – the SNAPSHOT. Jar — spring. Profiles. The active = peer2

Wait for the project to complete and check whether any error is reported. test

Enter localhost:1112 or localhost:1111

conclusion

After setting up the multi-node registry, high availability is realized. However, at this moment, our micro-service application is only registered in this service. Therefore, we need to match the registration service path of the new node to the micro-service application. Use “, “to separate multiple nodes, as shown in the figure

After disconnecting from one of these places, the microservice provider can still provide services because it is registered on other nodes. If you don’t want to use a host name to access the registry, you can also use IP, but you need to add a configuration first. The default value is false eureka.instance.prefer-ip-address=true