[Microservices] First understanding of Spring Cloud, Eureka registration center, Eureka cluster, Eureka self-protection mechanism

一、Spring Cloud

1. What is Spring Cloud?
Spring Cloud official website
Spring Cloud is built on the basis of Spring Boot, a tool set for simplifying the construction of distributed systems.
The main sub-projects of Spring Cloud :
① Spring Cloud Netflix: integrates various OOS components, including Eureka, Ribbon, Hystrix, Zuul, Feign, and Archaius, etc.
② Spring Cloud Config: a configuration management tool that supports the use of Git to store configuration content. Use it to realize the external storage of infant deployment, and support configuration content such as client configuration information refresh, encryption and decryption
③ Spring Cloud Starter: The basic component of Spring Cloud is a basic dependency module based on Spring Boot style
2. Spring Cloud Features
(1) Easy to use
(2) Full-featured
(3) Easy to expand and maintain
(4) Used in various environments
3. Compatibility between Spring Boot and Spring Cloud versions
insert image description here
4. Devtools hot deployment
insert image description here
(1) Modules add Devtools dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

(2) Add plugin to the parent project

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

(3) Shortcut key to open: ctrl+shift+alt+/, select Registry, and then check the following two columns. Then close and restart idea, the configuration has taken effect.
insert image description here
5. Project refactoring
(1) Create a new module and introduce pom.xml dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

(2) Copy entity (including full class name), maven command click clean, install
(3) Delete the original entity, add the following dependencies respectively, and the reconstruction is completed.

<dependency>
	<groupId>org.example</groupId>
	<artifactId>cloud-api-commons</artifactId>
	<version>${project.version}</version>
</dependency>

insert image description here

2. Eureka Registration Center

insert image description here
Eureka is a service discovery framework developed by Netflix, which itself is a REST-based service. Contains two major components:
(1) Server discovery component , also known as service registration center (Eureka Server): mainly provides service registration function
(2) Client discovery component (Eureka Client): mainly used to process service registration and discovery
1. How to build a service registration center:
① The server project pom.xml depends on

<!--除了SpringBoot和SpringCloud之外必须的依赖外,还要此依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

② configuration file application.yml

server:
  port: 7001  #端口号
eureka:
  instance:
    hostname: localhost #实例名
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://${
    
    eureka.instance.hostname}:${
    
    server.port}/eureka/  #注册中心的地址

③ Annotations on the core classes of Spring Boot: @EnableEurekaServer
2. How to build client components
① Server-side engineering pom.xml dependencies

<!--除了SpringBoot和SpringCloud之外的依赖,还需要引入该依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

② configuration file application.yml

server:
  port: 8001  #Eureka实例的端口号
spring:
  application:
    name: cloud-payment-service #指定实例的名称  
eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址

③ Annotations on the core classes of Spring Boot: @EnableEurekaClient
3. Eureka cluster
Function: high availability, mutual registration, mutual watch
insert image description here
(1) Eureka cluster registration center configuration file application.yml

server:
  port: 7001  #端口号
eureka:
  instance:
    hostname: eureka7001.com #实例名
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/  #端口号相互注册,注册中心的地址

(2) Service provider cluster configuration file application.yml

server:
  port: 8001  #Eureka实例的端口号
spring:
  application:
    name: cloud-payment-service #指定实例的名称
eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      #defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群

(3) Service consumption is the only one, configuration file application.yml

server:
  port: 80
spring:
  application:
    name: cloud-order-service
eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      #指定eureka的服务端地址
      #defaultZone: http://localhost:7001/eureka/
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群

(4) Add load balancing annotations to the configuration class of the service consumer@LoadBalanced

@Configuration
public class ApplicationContextConfig {
    
    
    @Bean
    @LoadBalanced   //实现调用订单服务时服务的负载均衡
    public RestTemplate getRestTemplate(){
    
    
    //RestTemplate是Spring提供的用于访问Rest服务的客户端实例,它提供了多种便捷访问远程Http服务的方法,只需要传入url及返回值类型即可。
        return new RestTemplate();
    }
}

(5) To specify the service name of the client component, add the configuration in the configuration file application.yml corresponding to the microservice:

server:
  port: 8001  #Eureka实例的端口号
spring:
  application:
    name: cloud-payment-service #指定实例的名称
eureka:
  instance:
    prefer-ip-address: true	#选中时是否显示主机的Ip
    instance-id: payment8001  #指定id
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      #defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群

4. Service Discovery Discovery
function: For microservices registered in Eureka, service information can be obtained through service discovery
Steps:
① In the controller corresponding to the service provider, add the following content

@RestController
@Slf4j	//日志记录
public class PaymentController {
    
    
	@Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/payment/discovery")
    public Object discovery() {
    
    
        //获取服务
        List<String> services = discoveryClient.getServices();
        for(String element: services) {
    
    
            log.info("*******element**********"+element);
        }
        //获取服务的实例
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance instance: instances) {
    
    
            log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;	//返回当前的实例
    }
}

② Add annotations to the main startup class of the service provider:@EnableDiscoveryClient

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient  //开启服务发现
public class PaymentMain8001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

5. Eureka protection mechanism
When a service becomes unavailable at a certain moment, Eureka will not clean it up immediately, and will still retain the information of the current service. It belongs to the AP branch in the CAP.
In self-protection mode (enabled by default), Eureka Server will protect the information in the service registry and no longer log out any service instances.
How to ban self-protection?
Here we take the stand-alone registration center as an example
① Registration center application.yml configuration

server:
  port: 7001  #端口号
eureka:
  instance:
    hostname: eureka7001.com #实例名
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/  #注册中心的地址
  server:
    enable-self-preservation: false #false关闭自我保护机制,保证不可用的服务被及时移除
    eviction-interval-timer-in-ms: 2000 #单位:毫秒

② service provider application.yml configuration

server:
  port: 8001  #Eureka实例的端口
spring:
  application:
    name: cloud-payment-service #指定实例的名称
eureka:
  client:
    register-with-eureka: true  #向注册中心注册自己
    fetch-registry: true  #false表示自己就是注册中心,职责就是维护注册实例
    service-url:
      defaultZone: http://localhost:7001/eureka/  #指定eureka的服务端地址
      #defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #eureka集群
  instance:
    instance-id: payment8001  #指定id
    prefer-ip-address: true #是否选中的时候显示主机的ip
    lease-expiration-duration-in-seconds: 2 #Eureka服务端在接收到客户端发送的最后一次心跳后的等待时间,超时将清除服务(默认:90秒
    lease-renewal-interval-in-seconds: 1  #Eureka客户端向服务端发送心跳的时间间隔(默认:30秒)

Guess you like

Origin blog.csdn.net/weixin_46081857/article/details/123477202