文章导读目录
1、引入Hystrix依赖 2、案例演示 3、服务端如何降级 1、引入Hystrix依赖 2、启动类添加激活注解@EnableHystrix 3、改yml 4、业务类方法上添加启用:@HystrixCommand(fallbackMethod = "fallback") 1、类上加注解:DefaultProperties(defaultFallback=" ") 2、方法加注解:@HystrixCommand 3、编写全局降级方法 1、新建一个解耦降级处理类 2、Feign客户端定义的接口添加fallback |
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.18</version>
</dependency>
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@GetMapping(value = "/hystrix/ok/{id}")
public String paymentInfoOk(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoOk(id);
log.info("========result:{}========", result);
return result;
}
@GetMapping(value = "/hystrix/timeout/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoTimeOut(id);
log.info("========result:{}========", result);
return result;
}
}
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {
@Override
public String paymentInfoOk(Integer id) {
return "线程池:" + Thread.currentThread().getName() + "成功访问paymentInfoOK,id=" + id;
}
@Override
public String paymentInfoTimeOut(Integer id) {
int time = 5;
try {
TimeUnit.SECONDS.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "线程池:" + Thread.currentThread().getName() + "耗时" + time + "秒成功访问paymentInfoTimeOut,id=" + id;
}
}
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@GetMapping(value = "/hystrix/ok/{id}")
public String paymentInfoOk(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoOk(id);
log.info("========result:{}========", result);
return result;
}
//设置超时时间3s
@HystrixCommand(fallbackMethod = "fallback",commandProperties = {
@HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
@GetMapping(value = "/hystrix/timeout/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoTimeOut(id);
log.info("========result:{}========", result);
return result;
}
public String fallback(Integer id){
return "系统繁忙,请稍后再试!线程池:" + Thread.currentThread().getName() +"访问paymentInfoTimeOut,id=" + id;
}
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.18</version>
</dependency>
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@EnableHystrix
public class CloudOrder {
public static void main(String[] args) {
SpringApplication.run(CloudOrder.class, args);
}
}
feign:
hystrix:
enabled: true
@RestController
@RequestMapping("/order")
@Slf4j
@DefaultProperties(defaultFallback="globalFallback")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping(value = "/hystrix/ok/{id}")
public String paymentInfoOk(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoOk(id);
log.info("========result:{}========", result);
return result;
}
@HystrixCommand(fallbackMethod = "fallback",commandProperties = {
@HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds",value = "1500")
})
@GetMapping(value = "/hystrix/timeout/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoTimeOut(id);
log.info("========result:{}========", result);
return result;
}
public String fallback(Integer id){
return "太久了我不想等待了,我已经等待了1.5s了!线程池:" + Thread.currentThread().getName() +"访问paymentInfoTimeOut,id=" + id;
}
}
@Component
@FeignClient(value = "CLOUD-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping(value = "/payment/hystrix/ok/{id}")
String paymentInfoOk(@PathVariable("id") Integer id);
@GetMapping(value = "/payment/hystrix/timeout/{id}")
String paymentInfoTimeOut(@PathVariable("id") Integer id);
}
@RestController
@RequestMapping("/order")
@Slf4j
@DefaultProperties(defaultFallback="globalFallback")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping(value = "/hystrix/ok/{id}")
public String paymentInfoOk(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoOk(id);
log.info("========result:{}========", result);
return result;
}
@HystrixCommand
@GetMapping(value = "/hystrix/timeout/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoTimeOut(id);
log.info("========result:{}========", result);
return result;
}
//全局方法降级
public String globalFallback(){
return "全局降级!线程池:" + Thread.currentThread().getName() +"访问paymentInfoTimeOut";
}
}
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfoOk(Integer id) {
return "全局解耦降级处理PaymentFallback->paymentInfoOk!";
}
@Override
public String paymentInfoTimeOut(Integer id) {
return "全局解耦降级处理PaymentFallback->paymentInfoTimeOut!";
}
}
@Component
@FeignClient(value = "CLOUD-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping(value = "/payment/hystrix/ok/{id}")
String paymentInfoOk(@PathVariable("id") Integer id);
@GetMapping(value = "/payment/hystrix/timeout/{id}")
String paymentInfoTimeOut(@PathVariable("id") Integer id);
}