Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Construction method of Spring Eureka Cluster

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the method of building Spring Eureka cluster". In the daily operation, I believe that many people have doubts about the method of building Spring Eureka cluster. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to build Spring Eureka cluster". Next, please follow the editor to study!

Preface

Eureka is mainly used for service registration and discovery. Let's learn how to use Eureka today. For more information about Eureka, please refer to Netflix / eureka and Spring-Eureka.

The main architecture diagram of Eureka is as follows:

Eureka has three main roles, Eureka Server and Eureka Client, in which Eureka Client includes Service Provider and Service Consumer

1. Eureka Server: registry, which provides service registration and discovery. The registry can be built into a cluster mode to achieve high availability of services.

2. Service Provider: the service provider registers its own service with Eureka so that the service consumer can find the

3. Service Consumer: the service consumer obtains the list of registered services from Eureka, so that the services can be consumed

Examples

Next, write an example manually based on the official documentation of Spring Eureka.

Eureka Server

First, to implement the Eureka Server registry, first create a new maven project for Spring. The pom file is as follows:

4.0.0 com.tsmyk eureka_server 0.0.1-SNAPSHOT eureka_server Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE 1.8 UTF-8 UTF-8 Greenwich.SR1 org.springframework.cloud spring-cloud -starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import Org.springframework.boot spring-boot-maven-plugin

Then add the following configuration to the application.properties configuration file:

The spring.application.name=spring-cloud-eureka# port server.port=8761# IPserver.ip=localhost# indicates whether to register yourself with Eureka Server. The default is true. Eureka.client.register-with-eureka=false# indicates whether to obtain registration information from Eureka Server. The default is true. Eureka.client.fetch-registry=false# sets the address to interact with Eureka Server, query service and registration service all need to rely on this address, multiple addresses can be used, separated. Eureka.client.serviceUrl.defaultZone= http://${server.ip}:${server.port}/eureka/logging.level.com.netflix.eureka=OFFlogging.level.com.netflix.discovery=OFF

Because it is a service registry, you do not need to register with yourself, nor do you need to obtain services from the registry, so you need to set these two items to false:eureka.client.register-with-eureka,eureka.client.fetch-registry

After that, add the annotation @ EnableEurekaServer to the startup class and run:

@ EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {public static void main (String [] args) {SpringApplication.run (EurekaServerApplication.class, args);}}

Enter: http://localhost:8761/ in the browser to display the following page, which means that the Eureka Server registry is set up:

Since we haven't registered any instances yet, we show that there are no available instances in Instances currently registered with Eureka.

Eureka Client

Next, write an Eureka Client and register the service with the registry.

The pom file is as follows:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.tsmyk eureka_client_2 0.0.1-SNAPSHOT eureka_client_2 Demo project for Spring Boot 1.8 UTF-8 UTF-8 Greenwich.SR1 org.springframework.cloud Spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin

The configuration of the configuration file application.properties is as follows:

# Port number server.port=8765# service name spring.application.name=eureka-client-2# is registered to the eureka center, and the configuration service eureka.client.service-url.defaultZone= http://localhost:8761/eureka/# sets the ID of the instance to ip:porteureka.instance.instance-id=localhost:$ {server.port} # heartbeat time, that is, the service renewal interval (default is 30s) eureka.instance.lease-renewal-interval-in-seconds=5# daze time Expiration time of service renewal (default is 90s) eureka.instance.lease-expiration-duration-in-seconds=10

After that, annotate the startup class with @ EnableDiscoveryClient, and run:

@ EnableDiscoveryClient@SpringBootApplicationpublic class EurekaClient2Application {public static void main (String [] args) {SpringApplication.run (EurekaClient2Application.class, args);}}

At this point, enter the above URL: http://localhost:8761/ in the browser, as follows:

At this point, you can see that our Eureka-client has been registered, the status is UP, the address is: locahost:8765.

The above is an example of a simple service registered in discovery.

Eureka Server cluster

In order to ensure the high availability of registries, Eureka Server registries generally need to be built in cluster mode. Now, let's build three Eureka Server registries to implement cluster mode. The eureka.client.service-url.defaultZone of each registry needs to point to the other two Eureka Server registry addresses.

Create three new Spring projects, each for three Eureka Server registries, with the same code as above, but with different configuration files

Configuration file for the first Eureka Server registry:

The spring.application.name=spring-cloud-eureka# port server.port=8761eureka.instance.hostname=eureka-server-peer1# indicates whether to register yourself with Eureka Server. The default is true. Eureka.client.register-with-eureka=true# indicates whether to obtain registration information from Eureka Server. The default is true. Eureka.client.fetch-registry=true# sets the address to interact with Eureka Server, query service and registration service all need to rely on this address, multiple addresses can be used, separated. Eureka.client.serviceUrl.defaultZone= http://eureka-server-peer2:8762/eureka/,http://eureka-server-peer3:8763/eureka/logging.level.com.netflix.eureka=OFFlogging.level.com.netflix.discovery=OFF

Where you need to set eureka.client.register-with-eureka,eureka.client.fetch-registry to true, and then eureka.client.serviceUrl.defaultZone points to the addresses of the other two registries.

Configuration file for the second Eureka Server registry:

The spring.application.name=spring-cloud-eureka# port server.port=8762eureka.instance.hostname=eureka-server-peer2# indicates whether to register yourself with Eureka Server. The default is true. Eureka.client.register-with-eureka=true# indicates whether to obtain registration information from Eureka Server. The default is true. Eureka.client.fetch-registry=true# sets the address to interact with Eureka Server, query service and registration service all need to rely on this address, multiple addresses can be used, separated. Eureka.client.serviceUrl.defaultZone= http://eureka-server-peer1:8761/eureka/,http://eureka-server-peer3:8763/eureka/# is false, and the self-protection eureka.server.enable-self-preservation=false# cleanup interval is turned off (in milliseconds). Default is 60*1000eureka.server.eviction-interval-timer-in-ms=4000logging.level.com.netflix.eureka=OFFlogging.level.com.netflix.discovery=OFF.

Configuration file for the third Eureka Server registry:

The spring.application.name=spring-cloud-eureka# port server.port=8763eureka.instance.hostname=eureka-server-peer3# indicates whether to register yourself with Eureka Server. The default is true. Eureka.client.register-with-eureka=true# indicates whether to obtain registration information from Eureka Server. The default is true. Eureka.client.fetch-registry=true# sets the address to interact with Eureka Server, query service and registration service all need to rely on this address, multiple addresses can be used, separated. Eureka.client.serviceUrl.defaultZone= http://eureka-server-peer1:8761/eureka/,http://eureka-server-peer2:8762/eureka/logging.level.com.netflix.eureka=OFFlogging.level.com.netflix.discovery=OFF

Now, write two more Eureka Clinet to register with eureka-server-peer1 and eureka-server-peer3

The first Eureka Clinet configuration file:

# Port number server.port=8764# service name spring.application.name=eureka-client-1# is registered to the eureka center, and the configuration service eureka.client.service-url.defaultZone= http://eureka-server-peer1:8761/eureka/# sets the ID of the instance to ip:porteureka.instance.instance-id=localhost:$ {server.port} # heartbeat time, that is, the service renewal interval (default is 30s) eureka.instance.lease-renewal-interval-in-seconds=5# daze time Expiration time of service renewal (default is 90s) eureka.instance.lease-expiration-duration-in-seconds=10

Eureka.client.service-url.defaultZone= http://eureka-server-peer1:8761/eureka/ means to register with the first registry.

Second Eureka Clinet profile:

# Port number server.port=8765# service name spring.application.name=eureka-client-2# is registered to the eureka center, and the configuration service # eureka.client.service-url.defaultZone= http://localhost:8761/eureka/eureka.client.service-url.defaultZone=http://eureka-server-peer3:8763/eureka/# sets the ID of the instance to ip:porteureka.instance.instance-id=localhost:$ {server.port} # heartbeat time That is, the service renewal interval (default is 30s) eureka.instance.lease-renewal-interval-in-seconds=5# in a daze, that is, the service renewal expiration time (default is 90s) eureka.instance.lease-expiration-duration-in-seconds=10

Eureka.client.service-url.defaultZone= http://eureka-server-peer3:8763/eureka/ means to register with the third registry.

Then add the following information to the hosts file:

127.0.0.1 eureka-server-peer1127.0.0.1 eureka-server-peer2127.0.0.1 eureka-server-peer3

Now run these five projects, and type: http://localhost:8761/ in the browser, as follows:

Two services have been registered:

There are two copies of the registry and two registries are available:

When we stop the second registry, it becomes unavailable:

When the second registry is shut down, it does not immediately become unavailable. This is because Eureka has a self-protection mechanism. When a service in its registry is shut down due to network or other reasons, Eureka will not delete the service registration immediately, but wait for it to be repaired. However, you can turn off self-protection through parameters:

# is false, the interval between turning off self-protection eureka.server.enable-self-preservation=false# cleaning invalid nodes (in milliseconds). Default is 60*1000eureka.server.eviction-interval-timer-in-ms=4000.

In addition to some common configurations mentioned above, Eureka Client also configures the

Eureka.instance.lease-renewal-interval-in-seconds=5eureka.instance.lease-expiration-duration-in-seconds=10

Eureka.instance.lease-renewal-interval-in-seconds represents heartbeat time, that is, the interval between service renewals (default is 30s)

Eureka.instance.lease-expiration-duration-in-seconds indicates the time spent in a daze, that is, the expiration time of service renewal (default is 90s)

Heartbeat time means how often the client needs to send a heartbeat to the registry to show that he is still alive. The default time is 30 seconds.

The time in a daze means how long it will take for the eureka server to delete the instance after receiving the last heartbeat from the instance. The default time is 90 seconds.

In addition, Eureka Client can also configure how often to go to the Eureka Server registry to pull registration information. The default time is 30 seconds, which is configured by the following parameters:

Eureka.client.registry-fetch-interval-seconds at this point, on the "Spring Eureka cluster building method" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report