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 .
rely
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
SpringBootApplication entrance need to add @EnableCircuitBreaker
notes, then Spring AOP factory will start on the way there are all methods @HystrixCommand
to 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 @HystrixCommand
Notes 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
@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
execution.isolation.strategy
Alternatively value of this attribute has twoTHREAD
andSEMAPHORE
default valuesTHREAD
. ① 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中声明fallbackMethod
name 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:
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, @ HystrixCommand
be able to specify the type of exception should be ignored. The following ArithmeticException: / by zero
will 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.timeoutInMilliseconds
property 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:
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
@EnableHystrixDashboard
annotations
to access the page http: // localhost: port / hystrix