SpringCloud the fuse -Hystrix

Hystrix is ​​a delay and fault-tolerant database, designed to isolate the access point to remote systems, services and third-party libraries, stop cascading failure, and to achieve flexibility in complex distributed systems, in these systems, failure is inevitable .
Here Insert Picture Description

rely

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

SpringBootApplication entrance need to add @EnableCircuitBreakernotes, then Spring AOP factory will start on the way there are all methods @HystrixCommandto add fuse strategic business approach.

@SpringBootApplication
@EnableCircuitBreaker
public class HystrixSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixSpringBootApplication.class,args);
    }
}

Add on business method @HystrixCommandNotes realized blown.

@HystrixCommand
@Override
public User queryUserById(Integer id) {
    System.out.println(Thread.currentThread().getId());
    return new User("未熔断", new Date(), true, 11.11);
}

Thread isolation

The default method of execution will start a new thread of execution and the main program is not a thread, so if there is ThreadLocal variable context, in this method becomes ineffective. So generally annotation property by setting commandProperties, set the thread on it.

  • By default thread ID Service and the Controller
    Here Insert Picture Description
@HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")
    })
@Override
public User queryUserById(Integer id) {
	System.out.println("Service线程ID:" + Thread.currentThread().getId());
    return new User("未熔断", new Date(), true, 11.11);
}
  • After setting the thread ID Service and the Controller
    Here Insert Picture Description

execution.isolation.strategyAlternatively value of this attribute has two THREADand SEMAPHOREdefault values THREAD. ① general if there is a second instance of a 100 concurrent, this time because of frequent start threads overhead is too large at this time generally consider SEMAPHORE, ② non-network calls.

Fallback

Over the @HystrixCommand中声明fallbackMethodname of graceful degradation may be achieved as follows:

@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById")
@Override
public User queryUserById(Integer id) {
    System.out.println(Thread.currentThread().getId());
    int i=10/0;
    return new User("未熔断", new Date(), true, 11.11);
}
public User fallbackMethodQueryUserById(Integer id, Throwable e) {
    System.out.println(e.getMessage());
    return new User("熔断降级后", new Date(), true, 11.11);
}

Result of the call:
Here Insert Picture Description

Note that the target method and the method in claim fallbackMethod must be in the same class having the same parameters (abnormal parameter optional)

Error Propagation

According to this description, @ HystrixCommandbe able to specify the type of exception should be ignored. The following ArithmeticException: / by zerowill not trigger fallbackMethod method.

//	@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById")
//	@HystrixCommand(commandProperties = {
//			@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")
//	})
	@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById",ignoreExceptions = {ArithmeticException.class})
	@Override
	public User getUser(Integer id) {
		System.out.println("Service线程ID:" + Thread.currentThread().getId());
		int i = 10 / 0;
		return new User("未熔断", new Date(), true, 11.11);
	}

	public User fallbackMethodQueryUserById(Integer id, Throwable e) {
		System.out.println(e.getMessage());
		return new User("熔断降级后", new Date(), true, 11.11);
	}

Request timed out fuse

The user can set the execution.isolation.thread.timeoutInMillisecondsproperty of a requested maximum delay method, the system will throwHystrixTimeoutException

@HystrixCommand(fallbackMethod = "fallbackMethodQueryUserById",commandProperties = {
			@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="100")
	})
	@Override
	public User getUser(Integer id) {
		System.out.println("Service线程ID:" + Thread.currentThread().getId());
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//		int i = 10 / 0;
		return new User("未熔断", new Date(), true, 11.11);
	}

	public User fallbackMethodQueryUserById(Integer id, Throwable e) {
		System.out.println(e.getMessage());
		return new User("超时熔断", new Date(), true, 11.11);
	}

Results of the:
Here Insert Picture Description

https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica
https://github.com/Netflix/Hystrix/wiki/Configuration

Hystrix Dashboard

rely

 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
 </dependency>

SpringBoot entrance class to add @EnableHystrixDashboardannotations
to access the page http: // localhost: port / hystrix

Here Insert Picture Description

Published 19 original articles · won praise 8 · views 4541

Guess you like

Origin blog.csdn.net/M283592338/article/details/104297756