1. Why build Eureka Server cluster
In a distributed system, a service registry is the most important infrastructure, a single point of failure is devastating and should always be available to provide services. To maintain its availability, using clusters is a good solution. Eureka implements high availability deployment through mutual registration, so we only need to configure Eureke Server with other available serviceUrl to achieve high availability deployment. The Eureka cluster framework diagram is as follows:
2. Set up the Eureka Server
In the last article, we built a standalone version of Eureka Server, and then we transformed it on this basis.
2.1 First change the configuration file application.yml of Eureka-server. I use the multi-profile format.
Yml, applicatiom-peer1.yml, applicatiom-peer2.yml, applicatiom-peer3.yml
Applicatiom-peer1. yml configuration is as follows:
# Highly available Eureka-server cluster Peer1
spring:
application:
name: Eureka-Server-peer The application name is also the name of the service registration
server:
port: 8001 # open port
eureka:
instance:
prefer-ip-address: true Submit IP information
status-page-url-path: /actuator/info # Url link for information query
health-check-url-path: /actuator/health Health check url
hostname: peer1 # the hostname
client:
By default, the Eureka servce registers with itself. Set registerWithEureka and fetchRegistry to false to prevent it from registering itself
# In Eureka clustering, allow registries to register themselves as services with other registries, thus forming a set of registries that register with each other and synchronize the list of services with each other, achieving the high availability we want
The default value is true
registerWithEureka: true
Whether to query registration information from Eureka Server
fetchRegistry: true
# service registered address
serviceUrl:
Eureka Server address, which is used to query services and register services.
# default for http://localhost:8761/eureka, separated with ", "multiple addresses
defaultZone: http://peer2:8002/eureka/,http://peer3:8003/eureka/
server:
#enable-self-preservation: true # Enable self-preservation mode
#eviction- interval-timer-in-MS: 1000 #eviction- interval-timer-in-MS: 1000Copy the code
Applicatiom-peer2. yml configuration is as follows:
# Highly available Eureka-server cluster Peer2
spring:
application:
name: Eureka-Server-peer The application name is also the name of the service registration
server:
port: 8002 # open port
eureka:
instance:
prefer-ip-address: true Submit IP information
status-page-url-path: /actuator/info # Url link for information query
health-check-url-path: /actuator/health Health check url
hostname: peer2 # the hostname
client:
By default, the Eureka servce registers with itself. Set registerWithEureka and fetchRegistry to false to prevent it from registering itself
# In Eureka clustering, allow registries to register themselves as services with other registries, thus forming a set of registries that register with each other and synchronize the list of services with each other, achieving the high availability we want
The default value is true
registerWithEureka: true
Whether to query registration information from Eureka Server
fetchRegistry: true
# service registered address
serviceUrl:
Eureka Server address, which is used to query services and register services.
# default for http://localhost:8761/eureka, separated with ", "multiple addresses
defaultZone: http://peer1:8001/eureka/,http://peer3:8003/eureka/
server:
#enable-self-preservation: true # Enable self-preservation mode
#eviction- interval-timer-in-MS: 1000 #eviction- interval-timer-in-MS: 1000Copy the code
Applicatiom-peer3. yml configuration is as follows:
# Highly available Eureka-server cluster Peer2
spring:
application:
name: Eureka-Server-peer The application name is also the name of the service registration
server:
port: 8003 # open port
eureka:
instance:
prefer-ip-address: true Submit IP information
status-page-url-path: /actuator/info # Url link for information query
health-check-url-path: /actuator/health Health check url
hostname: peer3 # the hostname
client:
By default, the Eureka servce registers with itself. Set registerWithEureka and fetchRegistry to false to prevent it from registering itself
# In Eureka clustering, allow registries to register themselves as services with other registries, thus forming a set of registries that register with each other and synchronize the list of services with each other, achieving the high availability we want
The default value is true
registerWithEureka: true
Whether to query registration information from Eureka Server
fetchRegistry: true
# service registered address
serviceUrl:
Eureka Server address, which is used to query services and register services.
# default for http://localhost:8761/eureka, separated with ", "multiple addresses
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/
server:
#enable-self-preservation: true # Enable self-preservation mode
eviction-interval-timer-in-ms: 4000 The default interval is 60*10000Copy the code
Enable self-preservation enable self-preservation In the default configuration, Eureka Server does not get the heartbeat of the client in the default 90s, so the instance is cancelled. However, because of cross-process invocation of micro-service, network communication often faces various problems. For example, when the micro-service status is normal, but the network partition fails, Eureka Server unlogs the service instance, making most microservices unusable, which is dangerous because the service is fine. To solve this problem, Eureka has a self-protection mechanism. You can enable the self-protection mechanism by setting the following parameters on the Eureka Server.
enable-self-preservation: true
Its principle is that when the Eureka Server node loses too many clients in a short period of time (network fault may be sent), the node will enter the self-protection mode and will not cancel any micro-services. When the network fault is recovered, the node will automatically exit the self-protection mode.
(In the development can not set, convenient development)
2.2 Modifying the contents of Application. yml:
# Highly available Eureka-Server cluster
spring:
profiles:
active: peer1 Copy the code
2.3 Because we are built on Windows, we need to modify the HOSTS file and add host name mapping to build the cluster on only one machine.
Modify the C:\Windows\System32\drivers\etc\HOSTS file
127.0.0.1 localhost
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3 Copy the code
2.4 Set three startup modes in IDEA and the configuration file names to be applied during startup. The convention configuration file in SpringBoot is named application-{profiles}. XXX
Localhost :8001, localhost:8002, localhost:8003
3. Modify the configuration file of eureka-Client
The code is as follows:
server:
port: 8762
spring:
The configuration program is called eureka-client
application:
name: eureka-client
eureka:
client:
# service registered address
serviceUrl:
Note: Eureka Server registered address
Register service providers with the three Eureka servers
defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#http://localhost:8761/eureka/ Copy the code
Start the Eureka-Client project with the following result:
For more technical articles, please pay attention to wechat public number: Java Programmers Gathering Place.