Spring Cloud (eight): Hystrix fault tolerance mechanism and data monitoring

I. Overview

The fault tolerance mechanism means that in a distributed system, each microservice calls each other and they depend on each other. However, in actual operation, a certain microservice may fail due to various reasons. If it is used, other microservices that depend on this microservice may have a long response time or a request failure. If this happens more frequently, the entire system may freeze or even crash. So how to solve this problem can be handled through Hystix's fault tolerance mechanism.

The fault tolerance mechanism refers to the pre-processing of error conditions without changing the calling relationship of each microservice. The main function of Hystrix is ​​to return a fallback downgrade process to the service consumer when the service provider fails and cannot be accessed, so as to ensure the smooth operation of the system.

The previous article has explained Hystrix’s fault-tolerant mechanism in combination with Feign. The fault-tolerant function means that when there is a problem with the service call, we do a downgrade process and use another part of the code to process it. It can effectively prevent the entire system from crashing due to the long response time of the program, and can also provide users with more friendly interaction. However, in addition to the fuse mechanism, Hystrix also provides data monitoring for requests. This article focuses on the data monitoring of Hystix. Hystrix data monitoring needs to be used in conjunction with Spring Boot Actuator. Actuator provides health monitoring and data statistics for services. Monitoring request data can be obtained through the hystrix.stream node, and a visual monitoring interface is provided.

2. Design principles

  1. Service isolation mechanism: to prevent the failure of a service provider from affecting the operation of the entire system.

  2. Service downgrade mechanism: When the service provider fails, it returns a fallback downgrade process to the service consumer.

  3. Fuse mechanism: When the service consumer request failure rate reaches a certain value, the fuse mechanism will be activated quickly and the error will be repaired.

  4. Provide real-time monitoring and alarm functions: ensure the operation of the fuse mechanism.

  5. Provide real-time configuration modification function: ensure the operation of the fuse mechanism.

Three, actual combat!

  1. Create Module, the pom.xml code is as follows:
 <!-- Eureka客户端依赖 -->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    	<version>2.0.2.RELEASE</version>
    </dependency>
    <!-- 加入feign -->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-openfeign</artifactId>
    	<version>2.0.2.RELEASE</version>
    </dependency>
    <!-- 加入actuator健康监控 -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
    	<version>2.0.7.RELEASE</version>
    </dependency>
    <!-- 添加hysteix熔断器 -->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    	<version>2.0.2.RELEASE</version>
    </dependency>
    <!-- 添加可视化监控组件 -->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    	<version>2.0.2.RELEASE</version>
    </dependency>
  1. Create a configuration file application.yml, the configuration code is as follows:
server:
  port: 8060
spring:
  application:
    name: hystrix
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
feign:
  hystrix:
    enabled: true
management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'
  1. Create a startup class with the following code:
package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class HystrixApplication {
    
    
	public static void main(String[] args) throws Exception {
    
    
		SpringApplication.run(HystrixApplication.class, args);
	}

}

Notes

  • @EnableCircuitBreaker: Declare to enable data monitoring

  • @EnableHystrixDashboard: Declare to enable visual data monitoring

  1. Create an entity class, which is convenient for receiving the data of findAll and transforming the entity. If you don't receive data and transfer the entity, you don't need an instance class, such as the index method. The specific code is as follows:
package com.zing.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data //生成Getter,Setter,equals,canEqual,hasCode,toString等方法
@AllArgsConstructor //添加一个构造函数,该构造函数含有所有已声明字段属性参数
@NoArgsConstructor //创建一个无参构造函数
public class Student {
    
    
	private long id;
	private String name;
	private int age;
}
  1. Create a Feign declarative interface, the code is as follows:
package com.zing.service;

import java.util.Collection;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import com.zing.entity.Student;

@FeignClient(value = "provider")
public interface IFeignService {
    
    
	
	@GetMapping("/student/findAll")
	public Collection<Student> findAll();
	
	@GetMapping("/student/index")
	public String index();
}
  1. Create the Controller code as follows;
package com.zing.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zing.entity.Student;
import com.zing.service.IFeignService;

@RestController
@RequestMapping("/hystrix")
public class HystrixHandler {
    
    
	
	@Autowired
	private IFeignService feignservice;
	
	@GetMapping("/findAll")
	public Collection<Student> findAll(){
    
    
		return feignservice.findAll();
	}
	
	@GetMapping("/index")
	public String index() {
    
    
		return feignservice.index();
	}
	
}
  1. Start the registration center in turn, the services of the two service providers provider and Hystrix, you can visit the registration center to see the following effect:
    insert image description here
  2. Visit the address http://localhost:8060/actuator/hystrix.stream to see that the data monitoring page has been refreshing the access data. The data is empty because we did not access the specific interface, the page is as follows:
    insert image description here
  3. When we visit http://localhost:8060/hystrix/index to call the interface, we see the following page:
    insert image description here
  4. We return to http://localhost:8060/actuator/hystrix.stream again to see that there is data, as shown below:
    insert image description here
  5. But the data on this page is not intuitive enough. At this time, we have to visit http://localhost:8060/hystrix and the following interface will appear:
    insert image description here
  6. After filling in the data monitoring address http://localhost:8060/actuator/hystrix.stream in the monitoring interface address, enter a name in the title to enter the data monitoring visualization interface, as shown in the figure below: So far, the entire data monitoring of
    insert image description here
    Hystrix The process demonstration has been completed. So in the next article we will learn about the configuration center of Spring Cloud, please look forward to the next article Spring Cloud (9): Config local configuration center .

One development engineer is also in the continuous learning stage, and the usual small experiences are shared from time to time. I hope that those who read the text I wrote can avoid detours and wish you success in work and study.
Bloggers have limited experience, if there are any shortcomings, welcome to communicate and improve together~ I hope to make progress together with you who are also in CSDN.

Author | Sweet Little Sweet Potato
Produced | Little Sweet Potato

Guess you like

Origin blog.csdn.net/qq_36836370/article/details/130901237