? ? 1.1?官网资料
https://github.com/Netflix/Hystrix/wiki/How-To-Use
? ? 1.2?停更进维
https://github.com/Netflix/Hystrix
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
? 超过3面提示
@HystrixCommand(fallbackMethod="paymentInfo_TimeOutHandler",
commandProperties= {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
//失败
public String paymentInfo_TimeOut(Integer id){
int timeNumber = 3;
try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();}
return "线程池:"+Thread.currentThread().getName()+" id: "+id+"\t"+"呜呜呜"+" 耗时(秒)"+timeNumber;
}
//兜底方法
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池:"+Thread.currentThread().getName()+" 系统繁忙, 请稍候再试 ,id: "+id+"\t"+"哭了哇呜";
}
@EnableHystrix
@SpringBootApplication
public class PaymentHystrixMain {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain.class, args);
}
}
此处用@EnableCircuitBreaker也可以,建议使用EnableHystrix注解
@RestController
@DefaultProperties(defaultFallback = "paymentInfo_TimeOutHandler")
@RequestMapping("payment")
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@HystrixCommand
@GetMapping("timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
String result = "";
synchronized (this) {
G.count++;
result = paymentService.paymentInfo_TimeOut(id) + " count:" + G.count;;
log.info("result:"+result);
}
return result;
}
//兜底方法
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池:"+Thread.currentThread().getName()+" 系统繁忙, 请稍候再试 ,id: "+id+"\t"+"哭了哇呜";
}
}
? ? 1) yml
feign:
hystrix:
enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。
? ? 2) 接口类
? ?重载方法中写异常数据
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬_┬)";
}
}
? ? 3) 接口
@Component
@FeignClient(value = "CLOUD-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
3.限流
4.熔断