Eureka service registration and discovery of springcloud notes (2)

1. What is eureka's self-protection mechanism?

Eureka has a self-protection mechanism. What does it mean, that is, when the eureka registry detects that a service is unavailable, it will not immediately cancel the service. It will have a waiting process. The time is about 90 seconds, within 90 seconds. , Eureka can still protect the basic information of the service from being lost. The purpose of this is that it is possible that the real reason for the service crash may be a network failure, such as a power outage. The microservice itself is actually healthy and should not be logged out. The service.

The self-protection mechanism is a security measure to deal with network abnormalities, which makes the eureka cluster more robust and stable.

In addition, in springcloud, eureka.server.enable-self-preservation=fasethe self-protection mechanism of turning off eureka can be used , but it is not recommended.

2. Demonstration steps

1. Introduce the library

<!-- eureka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>
<!-- 完善eureka监控信息 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.yaml configuration

server:
  port: 8001

mybatis:
  type-aliases-package: com.lhh.springcloud.pojo
  mapper-locations: classpath:mappers/*.xml
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

spring:
  application:
    name: springcloud-provider-emp
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    #    url: jdbc:mysql://localhost:3306/db01
    url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    #    password: root
    password: 123456
    #datasource category
    type: com.alibaba.druid.pool.DruidDataSource

#eureka的配置
#Eureka配置:服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
  instance:
    instance-id: springcloud-provider-emp-8001 #修改eureka上面的默认描述信息
    prefer-ip-address: true #显示服务来自哪个ip地址

#info监控信息配置
info:
  app.name: lhh-springcloud
  company.name: lhh.com

debug: true

2. Start

Start springcloud-eureka-7001,springcloud-provider-emp-8001

package com.lhh.springcloudprovideremp8001;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/*@SpringBootApplication*/
@SpringBootApplication(exclude= {
    
    DataSourceAutoConfiguration.class})
@MapperScan("com.lhh.springcloudprovideremp8001.dao")
@EnableEurekaClient
public class SpringcloudProviderEmp8001Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(SpringcloudProviderEmp8001Application.class, args);
    }
}

localhost:7001Visit through .

It is expected to see that the eureka registry has an 8001 service.

3. Demonstrate self-protection mechanism

If you close the 8001 service, you will find that the eureka registry can still see the service information and maintain it for a certain period of time (the default is 90s). When this period of time is exceeded, an exception will be thrown. This is the correct result.

to sum up

Because of the dependence on that piece, it has not been resolved, and no way to deal with it has been found for the time being.

Guess you like

Origin blog.csdn.net/qq_41486775/article/details/114395224