Introduction to hystrix, service fuse, service degradation, introduction to dashboard

table of Contents

Problems faced by distributed systems

Introduction to hystrix

What can Hystrix do

Service fuse

Service degradation

Introduction to dashboard


Problems faced by distributed systems

An application in a complex distributed architecture has dozens of dependencies, and each dependency will inevitably fail at some point.

Service avalanche

When calling between multiple microservices, suppose microservice A calls microservice B and microservice C, and microservice B and microservice C call other microservices. This is called "fan-out". If the response time of a microservice call on the fan-out link is too long or unavailable, the call to microservice A will consume more and more system resources, which will cause the system to crash, the so-called "avalanche effect".

For high-traffic applications, a single back-end dependency may cause all resources on all servers to saturate within a few seconds. Worse than failures, these applications may also cause increased delays between services, tight backup queues, threads and other system resources, causing more cascading failures throughout the system. These all indicate the need to isolate and manage failures and delays so that the failure of a single dependency cannot cancel the entire application or system.

Introduction to hystrix

Hystrix is ​​an open source library for handling the delay and fault tolerance of distributed systems. In a distributed system, many dependencies will inevitably fail to call, such as timeouts, exceptions, etc. Hystrix can ensure that in the case of a dependency problem, It will not cause overall service failure, avoid cascading failures, and improve the flexibility of the distributed system.

The "circuit breaker" itself is a switching device. After a service unit fails, the circuit breaker's fault monitoring (similar to a blown fuse) returns a callable response that can be handled (FallBack) to the caller. , Instead of waiting for a long time or throwing an exception that the caller cannot handle , this ensures that the service caller's thread will not be occupied unnecessarily for a long time, thereby avoiding the spread of failures in the distributed system, and even avalanche.

What can Hystrix do

Service degradation

Service fuse

Service limit

Near real-time monitoring

Service fuse

The fuse mechanism is a microservice link protection mechanism to deal with the avalanche effect.

When a microservice of the fan-out link is unavailable or the response time is too long, the service will be downgraded, and then the call of the microservice of the node will be fused, and the "error" response information will be quickly returned. When it is detected that the node microservice call response is normal, the call link is restored. In the Spring Cloud framework, the fuse mechanism is implemented through Hystrix. Hystrix will monitor the status of calls between microservices. When a failed call reaches a certain threshold, the default is that 20 calls fail within 5 seconds will start the fuse mechanism. The annotation of the fuse mechanism is @HystrixCommand.

Service degradation

The overall resources are almost insufficient, and some services will be shut down with painful pains, and the difficulties will be overcome before opening again.

Service downgrade processing is done on the client side, and has nothing to do with the server side

package com.atguigu.springcloud.service;
 
import java.util.List;
 
import org.springframework.stereotype.Component;
 
import com.atguigu.springcloud.entities.Dept;
 
import feign.hystrix.FallbackFactory;
 
@Component//不要忘记添加,不要忘记添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>
{
  @Override
  public DeptClientService create(Throwable throwable)
  {
   return new DeptClientService() {
     @Override
     public Dept get(long id)
     {
       return new Dept().setDeptno(id)
               .setDname("该ID:"+id+"没有没有对应的信息,Consumer客户端提供的降级信息,此刻服务Provider已经关闭")
               .setDb_source("no this database in MySQL");
     }
 
     @Override
     public List<Dept> list()
     {
       return null;
     }
 
     @Override
     public boolean add(Dept dept)
     {
       return false;
     }
   };
  }
}
 
 
 

package com.atguigu.springcloud.service;
 
import java.util.List;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.atguigu.springcloud.entities.Dept;
 
@FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class)
public interface DeptClientService
{
  @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
  public Dept get(@PathVariable("id") long id);
 
  @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
  public List<Dept> list();
 
  @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
  public boolean add(Dept dept);
}
 
 

Introduction to dashboard

In addition to isolating calls to dependent services, Hystrix also provides quasi-real-time call monitoring (Hystrix Dashboard). Hystrix will continuously record the execution information of all requests initiated through Hystrix and display it to users in the form of statistical reports and graphics, including How many requests are executed per second, how many successes, how many failures, etc. Netflix monitors the above metrics through the hystrix-metrics-event-stream project. Spring Cloud also provides the integration of Hystrix Dashboard to convert the monitoring content into a visual interface.

 

 

 

 

 

 

 

 

 

 

Published 524 original articles · Like 80 · Visits 150,000+

Guess you like

Origin blog.csdn.net/xushiyu1996818/article/details/104538928