Preface:
Spring-cloud-based microservice architecture, all microservices need to be registered with the registry. If the registry is blocked or crashed, the entire system cannot continue to provide services normally. Therefore, the registry needs to be clustered here, in other words, high availability (HA)
premise:
Read and complete the project of the first registry without changing the environment. This article is a simulation of high availability, and can copy the projects in two registry centers to modify their respective configuration files separately to achieve the same effect.
Modify hosts and add two lines at the end of the file as follows:
127.0.0.1 peer1127.0.0.1 peer2
It is recommended to use notepad++. If it is win10, it will remind you to give permissions, confirm, and then save it.
Project construction:
Open the project in the registry center and create an application-peer1.properties under src/resources
#Application name spring.application.name=eureka-server#Provide the service port 1111server.port=1111#The domain name that provides the service. Here we modify eureka.instance.hostname=peer1#Register yourself with the second registration center eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
Create an application-peer2.properties under src/resources
#The application name is the same as the first registry, spring.application.name=eureka-server#Provide the service port 1112server.port=1112#The domain name that provides the service. Here we modify eureka.instance.hostname=peer2#Register yourself with the first registry, eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
This article uses idea for testing. First, you need to make this project into a jar package, because the maven compilation tool and packaging tool have been introduced in pom.xml and the packaging format is specified as a jar package. This is done directly here, at the upper right of the screen
Packaging is completed, the jar package is located in the target folder, as shown in the figure
Open terminal as shown in the picture, at the bottom of the screen
After opening terminal, because the jar package is in the target directory and the current directory is the project directory, first cd to the target directory, and then enter the following command:
Tips: In order to prevent the wrong file name when entering the command, you can enter the first few letters of the file name, and then use the tab key to automatically complete it.
Copy the code as follows: java -jar EurekaServerDemo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
After entering this command, the project of peer1 configuration file is started, as shown in the figure
Next we start the second project, here we need to open another terminal, cd to the target directory
Note: We are using the same project here. Readers can use two projects for testing. It is important to note that the ports do not conflict.
Copy the code as follows: java -jar EurekaServerDemo-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
Wait for the project to start and complete to see if there are any errors reported.
test:
Browser input: localhost:1112 for viewing, or you can enter localhost:1111 for viewing
Conclusion:
After setting up a multi-node registration center, high availability is achieved, but at this time our microservice application is only registered with this service, so we need to also allocate the registration service path of the new node to the microservice application.
Use "," to separate the multiple nodes, as shown in the figure
In this way, after the microservice provider disconnects from one of the places, it can also provide services because it has registered in other nodes. If you do not want to use the host name to access the registration center, you can also use ip, but you need to add a configuration first, which defaults to false
eureka.instance.prefer-ip-address=true
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.