The registration center we built above is only suitable for local development , In the production environment, we must build a cluster to ensure high availability .Eureka The method of building cluster is very simple ： Each station Eureka Just specify more than one... In the configuration Eureka You can build a cluster with the address of .
Now let's 2 A node is taken as an example to illustrate the construction method . Suppose we have master and slaveone Two machines , What needs to be done is ：
- take master Sign up to slaveone above .
- take slaveone Sign up to master above .
If it is 3 Taiwan machine , And so on ：
- take master Sign up to slaveone and slavetwo above .
- take slaveone Sign up to master and slavetwo above .
- take slavetwo Sign up to master and slaveone above .
Steps to build
Create a new project eureka-server-cluster, Configure the following eureka-server equally .
First , We need to increase 2 Property files , Start different instances in different environments . increase application-master.properties：
server.port=8761 # To your slave node Eureka eureka.client.serviceUrl.defaultZone=http:// user name : password @localhost:8762/eureka/
server.port=8762 # Pointing to your master node Eureka eureka.client.serviceUrl.defaultZone=http:// user name : password @localhost:8761/eureka/
stay application.properties Add the following to ：
spring.application.name=eureka-server-cluster # Because the app is a registry , So set to false, Representative does not register himself with the registry eureka.client.register-with-eureka=false # Because the registry's job is to maintain service instances , There's no need to retrieve Services , So it's also set to false eureka.client.fetch-registry=false spring.security.user.name=zhangsan spring.security.user.password=123456 # Specify different environments spring.profiles.active=master
stay A The default on the machine is master start-up , And then in B Add on the machine --spring.profiles.active=slaveone Start it up .
This will be master Registered to slaveone in , take slaveone Registered to master in , No matter who has a problem , Applications can continue to use the surviving registry .
Previously, in the client, we configured eureka.client.serviceUrl.defaultZone To specify the corresponding registry , When our registry has multiple nodes , It needs to be modified eureka.client.serviceUrl.defaultZone Is configured as the address of multiple nodes , Multiple addresses can be separated by English commas ：