/ Spring Cloud  

Spring Cloud 3: Eureka Clustering

Last post we talked about Eureka. It is one of the key elements of Spring Cloud. Service center as a critical service, if it is a single point and having a failure then it is devastating. In a distributed system, the service center is the most important part and should be in a state that can provide services at any time. To maintain its availability, using clusters is a good solution. Eureka achieves high availability deployment by registering with each other, so we only need to configure Eureke Server with other available serviceUrl to achieve high availability deployment.

Two-node service center

First let’s try to setup a two-node service center.

1. Create application-peer1.properties

Peer1 service center’s configuration. Pointing serviceUrl to peer2.

1
2
3
4
5
6
7
8
spring.application.name=spring-cloud-eureka-server
server.port=8000
eureka.instance.hostname=peer1

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/

2. Create application-peer2.properties

Peer2 service center’s configuration. Pointing serviceUrl to peer1.

1
2
3
4
5
6
7
8
spring.application.name=spring-cloud-eureka-server
server.port=8001
eureka.instance.hostname=peer2

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/

3. Host file

Add the following 2 lines to hosts file

1
2
127.0.0.1 peer1  
127.0.0.1 peer2

Start server

Run the following command

1
2
3
4
5
# package jar file
mvn clean package
# start eureka server using 2 configurations
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

Visit http://localhost:8000/



We can see that peer1‘s service center DS Replicas already has the configuration information of peer2, and peer2 appears in available-replicas. If we manually stopped peer2, then peer2 would move to the unavailable-replicas column, indicating that peer2 was unavailable.

Use eureka cluster

In production, we may need three or more service centers to ensure the stability of the service. The configuration are actually the same: pointing the service centers to other service centers. Here I will only introduce the configuration of only three clusters. In fact, it is similar to the two-node service center. Each service center can point to the other two nodes.

application-peer1.properties

1
2
3
4
5
6
7
8
spring.application.name=spring-cloud-eureka-server
server.port=8000
eureka.instance.hostname=peer1

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/, http://peer3:8002/eureka/

application-peer2.properties

1
2
3
4
5
6
7
8
spring.application.name=spring-cloud-eureka-server
server.port=8001
eureka.instance.hostname=peer2

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/, http://peer3:8002/eureka/

application-peer3.properties

1
2
3
4
5
6
7
8
spring.application.name=spring-cloud-eureka-server
server.port=8002
eureka.instance.hostname=peer3

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/, http://peer2:8001/eureka/

Start peer1, peer2, peer3 eureka server

1
2
3
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3

Visit http://localhost:8000/



We can see peer2 and peer3‘s information in peer1

Check out the source code here: Eureka cluster demo