1、引入Maven依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
2、启动类使用@EnableCircuitBreaker注解
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; @SpringBootApplication @EnableCircuitBreaker public class HystrixApplication { public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); } }
3、使用@HystrixCommand注解,并配置fallbackMethod
@HystrixCommand由名为“javanica”的Netflix contrib库提供。Spring Cloud在连接到Hystrix断路器的代理中使用该注解自动包装Spring bean。断路器计算何时打开和关闭电路,以及在发生故障时应该做什么。
package com.mimaxueyuan.consumer.turbine.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.mimaxueyuan.consumer.turbine.service.MyService; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @RestController public class HelloController { @Autowired private MyService myService; /** * 模拟id为"ok"时请求成功,其它值让请求休眠5秒超时 * * @param id * @return * @author lynch */ @GetMapping("/hello/{id}") @HystrixCommand(fallbackMethod = "getError") public String hello(@PathVariable String id) { if(!"ok".equals(id)) { try { System.out.println(Thread.currentThread().getName()+":hello before sleep 5s...."); Thread.sleep(1000 * 5); System.out.println(Thread.currentThread().getName()+":hello after sleep 5s...."); } catch (InterruptedException e) { e.printStackTrace(); } } return "请求成功,id=" + id; } public String getError(String id) { System.out.println(Thread.currentThread().getName()+":getError before ...."); //myService.execute(); System.out.println("断路器已触发,并作相应的业务处理..."); System.out.println(Thread.currentThread().getName()+":getError after ...."); return "请求超时,使用断路器返回,id=" + id; } }
4、模拟url请求
4.1、正常请求
4.2、超时请求,使用断路器返回
4.3控制台输出信息
hystrix-HelloController-2:hello before sleep 5s.... HystrixTimer-1:getError before .... 断路器已触发,并作相应的业务处理... HystrixTimer-1:getError after .... java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at com.mimaxueyuan.consumer.turbine.controller.HelloController.hello(HelloController.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:116) at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.executeWithArgs(MethodExecutionAction.java:93) at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:78) at com.netflix.hystrix.contrib.javanica.command.GenericCommand$1.execute(GenericCommand.java:48) at com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommand.process(AbstractHystrixCommand.java:145) at com.netflix.hystrix.contrib.javanica.command.GenericCommand.run(GenericCommand.java:45) at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302) at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298) at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.Observable.unsafeSubscribe(Observable.java:10151) at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51) at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) at rx.Observable.unsafeSubscribe(Observable.java:10151) at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) at rx.Observable.unsafeSubscribe(Observable.java:10151) at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56) at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47) at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69) at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)