preface

The previous article, Service Registration and Discovery – Eureka, introduced the single-point EurekaServer. But in the actual environment, this single point model may have many hidden problems. For example, EurekaServer goes down or some unexpected situations occur, which may affect the invocation between other services and seriously affect the availability of the whole system. Therefore, a highly available EurekaServer cluster is generally deployed.

This article briefly introduces EurekaServer high availability simple construction.

Configuring the Local Environment

Because the local environment is a single computer to configure high availability, so need to modify the computer’s host file, this article briefly said that the MAC to modify the host file, other system computers can baidu, are very simple.

1. Access the /private/etc folder

2. Open the host file

3. Add the following two lines to the document

127.0.0.1	server1
127.0.0.1	server2
Copy the code

The complete host file is as follows:

# #
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
# #
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
127.0.0.1	server1
127.0.0.1	server2

Copy the code

New project

After the host file is configured, create a new project, which is exactly the same as the previous eurekaserver project, but the configuration file has been modified briefly (only the configuration file has been modified). Let’s look at the configuration in single-machine mode:

server.port=8761

eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

## Prohibit yourself from registering with yourself
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Copy the code

As can be seen from the configuration, the last two lines above are configured to let the registry not register itself, but in fact, Eurekaserver also registers itself as a service to other registries.

The new project springCloud_eureka_server1 (8761) has nothing different except the configuration, which is not shown here. The configuration file code is as follows:

server.port=8761

eureka.instance.hostname=server1
eureka.client.service-url.defaultZone=http://server2:8762/eureka/

spring.application.name=server1
Copy the code

Create a new project springCloud_eureka_server2 (8762) with the following configuration file code:

server.port=8762

eureka.instance.hostname=server2
eureka.client.service-url.defaultZone=http://server1:8761/eureka/

spring.application.name=server2
Copy the code

The above code is not difficult to understand. Springcloud_eureka_server1 (8761) registers itself with the EUreka of Server2, and SpringCloud_Eureka_server2 (8762) registers with the Eureka of server1.

Take a look at the Eureka admin page, as shown below:

The observation page shows that server1 has nodes for server2, and server2 has nodes for server1.

The application is registered with the high availability EurekaServer

The springCloud_eureka_client (8761) configuration file is as follows:

# # the port number
server.port=8763

spring.application.name=eureka_client

eureka.client.service-url.defaultZone=http://server1:8761/eureka/,http://server2:8762/eureka/
Copy the code

Take a look at the Eureka administration page, as follows:

Even if server1 is disconnected, springCloud_eureka_client (8763) is registered with server2, so other services on Server2 can still access springCloud_eureka_client (8763). Thus high availability of EurekaServer is realized.

Other tests

There is only one Eurekaserver in the springCloud_eureka_client (8763) project configuration file. The code is as follows:

# # the port number
server.port=8763

spring.application.name=eureka_client

eureka.client.service-url.defaultZone=http://server1:8761/eureka/
Copy the code

The Eureka administration page also shows the same effect as configuring two services, because data is synchronized between multiple Eurekas, but it is recommended to configure it on the client as in the first way.

Source code download: elder Yang code cloud

Personal website: www.dalaoyang.cn

Follow the author’s official account