Spring cloud implements the HA method of Eureka registration center, cloudeureka
Preface:
In the previous example, our Eureka Server is single-node. Once the node fails in production, it cannot provide service registration. To ensure the high availability of the Registration Center, A multi-node service registration center is generally used in production.
1. Add the following configuration to the hosts file:
127.0.0.1 peer1 127.0.0.1 peer2
2. modify the application. yml configuration file
--- Spring: profiles: peer1 # specify profile = peer1 application: name: Eureka-Server1 server: port: 8761 # register the service port number eureka: instance: hostname: peer1 # specify the host name client: serviceUrl: defaultZone: http: // peer2: 8762/eureka/# When profile = peer1, register yourself to the Eureka of peer2 --- spring: profiles: peer2 application: name: Eureka-Server2 server: port: 8762 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http: // peer1: 8761/eureka/# service registration address, register yourself to peer2
3. Create a jar package
Enter the following command in the command line:
mvn clean package
4. Execute jar
java -jar springcloud-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 java -jar springcloud-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
5. Access Eureka Server
Enter http: // localhost: 8761/in the browser/
Enter http: // localhost: 8762/in the browser/
Some problems found: Eureka Server exists in registered-replicas and unavailable-replicas, and the current Eureka Server is unavailable. The reason is as follows:
spring: application: name: Eureka-Server2
The names of the two Eureka servers must be the same. The results are as follows:
6. register the service on the dual Eureka Server
You only need to modify defaultZone.
# Eureka Server registration service address eureka. client. service-url.defaultZone = http: // peer1: 8761/eureka/, http: // peer2: 8762/eureka
VII. High Availability Verification
1. Enter http: // localhost: 7902/user/1 in the browser.
The result is as follows:
{"Id": 1, "username": "user1", "name": "zhangsan", "age": 20, "balance": 100.00}
Indicates Service Availability
2. Stop Eureka Server2 and find Server2 unavailable
3. Enter http: // localhost: 7902/user/1 in the browser.
{"Id": 1, "username": "user1", "name": "zhangsan", "age": 20, "balance": 100.00}
Through the above steps, you can implement Eureka HA. Please pay attention to some pitfalls!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.