Spring Cloud Series: Service Registration and Discovery Components - Eureka (Part 2)

Author's other platforms:

| CSDN:blog.csdn.net/qq_41153943

| Nuggets: juejin.cn/user/651387…

| Zhihu: www.zhihu.com/people/1024…

| GitHub: github.com/JiangXia-10…

This article has a total of 3304 words and is expected to read for 9 minutes

foreword

As mentioned earlier, the eureka component mainly consists of two parts: eureka server and eureka client, eureka server The previous article SpringCloud series: Service Registration and Discovery Components - Eureka (Part 1) introduced how to develop eureka server as a service registration center, today in the previous article On the basis of continuing the study of eureka, develop eureka client. In fact, the eureka client here is a microservice based on business split.

Practical development

The project foundation of this article is based on the project foundation of the previous article. For details, please refer to the SpringCloud series: Service Registration and Discovery Component-Eureka (Part 1) . And the source code of the final project will be synchronized to github, and the address is placed at the end of the article, which can be downloaded and used.

First of all, like eureka server, you also need to create a springboot project as a client application. The project structure is as follows:

picture

Then also need to introduce springboot related dependencies:

<dependencies>
        <dependency>
            <!--引入springboot相关依赖-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入spring-cloud-netflix-eureka-client依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
复制代码

Then add the application.properties file and add related configurations:

#指定服务端口
server.port=8081
#指定服务名称,唯一标识,服务名不能出现下划线,如果不指定名称,显示的就是unknown
spring.application.name=eurekaclient
#指定服务注册中心的地址,暴露服务地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
复制代码

Then add an entry class and use the @enableeurekaclient annotation to indicate that the current application is a client registered with the eureka server service for service registration:

@SpringBootApplication
//当前应用作为一个客户端进行服务注册
@EnableEurekaClient
public class SpringCloudEurekaClient {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaClient.class,args);
    }
}
复制代码

The above steps are basically the same as the creation process of the eureka server project, except for some specific content differences.

Then start the server project first and then the client project in order:

picture

picture

Enter in the browser address bar:

http://localhost:8761/
复制代码

An instance of EUREKACLIENT registration can be found in the registration center management interface , indicating that EUREKACLIENT has been registered in the registration center:

picture

However, you can find that there is a red message on it. According to your intuition, you can judge what kind of security prompt information it should be. The details are as follows:

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
复制代码

The translated meaning of this sentence is: Eureka may declare instances that no longer exist. When the number of refreshes is less than the threshold, expired instances will not be removed for safety reasons.

Here we need to explain that Eureka's default threshold is 85%.

That is to say, for example, if there are currently 10 microservices and only 8 have heartbeat responses, (8/10=80%<85%) Eureka will activate the protection mechanism, and expired instances will not be removed immediately, and this emergency warning will appear . When building Eureka Server, for example, we built two Eureka Servers, and self-registration is prohibited. Eureka Server itself counts as a service, so any one of Eureka Servers can only get one heartbeat, 1/2=50%. Then this warning will also appear. This situation would not have occurred if self-registration had not been disabled.

Then when you don't want to have this red warning, you can configure it in the application.properties file on the server side:

eureka.server.enable-self-preservation=false
复制代码

Start the project, the red warning disappears:

picture

But the following warning appears:

THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
复制代码

Therefore, the official does not recommend turning off the self-protection mechanism in the production environment.

Let's talk about what is eureka's self-protection mechanism. By default, if the eureka server does not receive the heartbeat of a service instance within a certain period of time (the default time is 90s), the eureka server will remove the instance. However, when a network partition fails, the microservice and the eureka server cannot communicate normally, but the microservice itself can communicate normally. At this time, the instance should not be removed, so a self-protection mechanism is introduced. The eureka server will count the proportion of heartbeat failures during operation. If it is less than 85% within 15 minutes, these instances will be protected so that these instances will not expire and be cleaned up. Therefore, the self-protection mechanism is a protective measure against abnormal network fluctuations, which can make the eureka cluster more robust and stable. The figure below is the explanation of the self-protection mechanism on the github official website.

picture

The figure below shows the maximum time for the eureka server to accept heartbeats by default. The default is 90s and how often the client sends a heartbeat to the eureka server. The default is 30s. It can be modified through the application.properties file:

picture

You can also configure the timeout for clearing instances:

#超时3s自动清除
eureka.server.eviction-interval-timer-in-ms=3000
复制代码

At present, the official website states that the update and maintenance of version 2.0 of eureka has stopped, so version 2.0 is not recommended, and version 1.0 is more stable.

picture

Summarize

The above is how to use eureka as a learning for microservice registration and service discovery! If you have any questions or mistakes, please point out and discuss!

Project source address:

github.com/JiangXia-10…

eureka official website github address:

github.com/Netflix/eur…

related suggestion

Guess you like

Origin blog.csdn.net/qq_41153943/article/details/125837657