Introduction to the

In a distributed system, the dependencies between services are complex, and it is inevitable that some services will fail. Hystrix is a library that provides fault tolerance between services, mainly in the aspects of delay and fault tolerance, to control linkage faults in distributed systems. Hystrix improves the resiliency of this distributed system by isolating access points to services, preventing linkage failures, and providing solutions to failures.

Problems: An application will generally rely on multiple services, each service due to the network is not reliable, the machine room is not reliable and other unstable factors, will always lead to service failure, if we do not deal with these failures, will then lead to the whole system is unavailable.

Service avalanche:

A normal user enters and invokes microservice A, then invokes B, then invokes C, and then leaves. When microservice C fails, the user stays in B. More and more users enter, request, and stay in B, which eventually leads to resource exhaustion and unavailability of B, and then A becomes unavailable. This chain reaction is like an avalanche that gets bigger and bigger, and it's called a service avalanche.Copy the code

Service fusing

Reference: blog.csdn.net/zero__007/a…

A microservice link protection mechanism for avalanche effects

When a microservice on the invocation link is unavailable or the response time is too long, the service is fused. The microservice on this node is no longer invoked, and an incorrect response message is quickly returned. When detecting that the microservice invocation response of this node is normal, the call link is restored.

Implemented in Spring Cloud via Hystrix. Hystrix monitors the status of calls between microservices, and when the number of failed calls reaches a certain threshold, the default is 20 failed calls within five seconds, the circuit breaker is activated.

Service fusing solves the following problems:

  1. When the dependent object is unstable, it can achieve the purpose of rapid failure.
  2. After a quick failure, it can dynamically test whether the dependent object is recovered according to a certain algorithm

practice

Project structures,

Create a new module named SpringCloud-provider-dept-Hystrix based on the project copy in the setup of the experimental environment.

Import dependence

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-hystrix</artifactId>
     <version>1.4.6. RELEASE</version>
</dependency>
Copy the code

use

At the Controller layer, use @hystrixCommand to implement the circuit breaker


@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand {
    String groupKey(a) default "";
    String commandKey(a) default "";
    String threadPoolKey(a) default "";  
    String fallbackMethod(a) default "";
    HystrixProperty[] commandProperties() default {};
    HystrixProperty[] threadPoolProperties() default {};
    Class<? extends Throwable>[] ignoreExceptions() default {};
    ObservableExecutionMode observableExecutionMode(a) default ObservableExecutionMode.EAGER;
    HystrixException[] raiseHystrixExceptions() default {};
    String defaultFallback(a) default "";
}


Copy the code

In @hystrixCommand we have defaultFallback() specifying the default alternate method (default “”) and fallbackMethod() specifying the alternate method to proceed after failure (default “”).

When a normal method call fails (20 calls in 5 seconds), it is considered to be faulty and fuses are performed, quickly returning an error message (calling alternative methods).

@RestController
public class DeptController {
    @Autowired
    private DeptImpl deptimpl;
    @RequestMapping("/dev/{id}")
    @HystrixCommand(fallbackMethod = "HystrixGet")// Specify alternate methods
    public Dept DeptqueryByID(@PathVariable("id") Long id) {
        Dept dept = deptimpl.queryByID(id);
        System.out.println(dept);
        if (dept==null) {
            throw new RuntimeException("id--->" + id + "User does not exist");
        }
        return dept;
    }
    public Dept HystrixGet(@PathVariable("id") Long id) {
        Dept dept=new Dept();
        dept.setDeptnumber(id.intValue());
        dept.setDname("id"+id+"User does not exist");
        dept.setD_source("no~~~~~~");
        returndept; }}Copy the code

Add @enablecircuitbreaker to the startup class to enable fuse breaker support

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker// Enable fuse support
public class HApplication {
    public static void main(String[] args) { SpringApplication.run(HApplication.class,args); }}Copy the code

Service degradation

This is to shut down some services that are rarely invoked to free up some resources and keep running properly when the server pressure increases dramatically.

Such as Taobao double 11 closed the refund channel.

practice

Specify fallbackFactory in the original FeignClient

@FeignClient(value = "PROVIDER-NAME",fallbackFactory = SerciceFallbackFactory.class)
public interface DeptClientService {
    @RequestMapping(value = "/dev/add")
    boolean add(Dept dept);

    @RequestMapping(value = "/dev/{id}")
    Dept queryByID(@PathVariable("id") Long id );

    @PostMapping(value = "/dev/list")
    List<Dept> queryAll(a);
}

Copy the code

Define your own FallbackFactory

Error note import feign. Hystrix. FallbackFactory;

import feign.hystrix.FallbackFactory;
@Component
public class SerciceFallbackFactory implements FallbackFactory {

    public DeptClientService create(Throwable cause) {
        return new DeptClientService() {
            public boolean add(Dept dept) {
                return false;
            }
            // Define the error message returned
            public Dept queryByID(Long id) {
                Dept dept = new Dept();
                dept.setD_source("Service degradation");
                dept.setDname("fail");
                dept.setDeptnumber(-1);
                return dept;
            }

            public List<Dept> queryAll(a) {
                return null; }}; }}Copy the code

Add it to the client configuration file

# Enable downgrade
feign:
  hystrix:
    enabled: true
Copy the code

Result: When we access the service again after shutting down the server

The difference between service circuit breaker and service degradation

  • Service fuses occur on the server side, while service downgrades occur on the client side
  • Causes of service fusing: failure and service degradation: To ensure the operation of core services for the overall load

Service Monitoring Dashboard

Establishing the project

Import dependence

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.4.6. RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    <version>1.4.6. RELEASE</version>
</dependency>
Copy the code

The configuration file

server:
  port: 9001
hystrix:
  dashboard:
# Hystrix Dashboard will be parsed to the host section via the proxyUrl and then through the configured proxyStreamAllowList. Determines whether access is allowed
    proxy-stream-allow-list: "localhost" 
    
Copy the code

Enabling Monitoring Support

@SpringBootApplication
@EnableHystrixDashboard/ / open
@EnableDiscoveryClient
public class DashboardApplication {
    public static void main(String[] args) { SpringApplication.run(DashboardApplication.class,args); }}Copy the code

Run after the visit: http://localhost:9001/hystrix

Follow the instructions inspingcloud-provider-dept-hystrixThe server adds beans

@Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(a){
        ServletRegistrationBean servlet = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        servlet.addUrlMappings("/actuator/hystrix.stream");
        return servlet;
    }
Copy the code

After the operation to http://localhost:8081/actuator/hystrix.stream can get some information service

Note: you need to call the @hystrixCommand method once to display the data. Only methods with @hystrixCommand are monitored

We can also use thehttp://localhost:9001/hystrixThe inputPress the button to enable monitoring of the service

Quote:www.cnblogs.com/augus007/ar…