Preface:
In the previous example, our Eureka Servers are all single-nodes. Once the node is dead in production, it is impossible to provide service registration. In order to ensure 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 when profile=peer1, the host name client: serviceUrl: defaultZone: http://peer2:8762/eureka/ # Register yourself to peer2 Eureka --- 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. Get a jar bag
Enter the following command on 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. Visit Eureka Server
Enter in the browser: http://localhost:8761/
Enter in the browser: http://localhost:8762/
I found some problems: Eureka Server exists in both registered-replicas and unavailable-replicas, and the current Eureka Server is not available for the following reasons: When registering, the
spring: application: name: Eureka-Server2
It must be consistent. Let's change the names in both Eureka Servers to Eureka-Server, and the results are as follows:
6. Register the service on dual Eureka Servers
Just modify the defaultZone
# The address of the Eureka Server registration service is eureka.client.service-url.defaultZone=http://peer1:8761/eureka/,http://peer2:8762/eureka
7. High availability verification
1. Enter in the browser: http://localhost:7902/user/1
The results are as follows:
{"id":1,"username":"user1","name":"Zhang San","age":20,"balance":100.00}
Instructions Services available
2. Stop Eureka Server2 and find that Server2 is not available
3. Enter again in the browser: http://localhost:7902/user/1
{"id":1,"username":"user1","name":"Zhang San","age":20,"balance":100.00}
Through the above steps, Eureka's HA can be realized. You should pay attention to some small pitfalls!
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.