一、相关依赖
<!-- hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
二、案例核心代码
1)、方式一:通过@HystrixCommand方法的方式给部分接口配置单独的线程池实现服务降级
对外FeigClient案例代码
@FeignClient(name = "app-kingseok-framework")
public interface FrameworkServiceFeign extends IUserService {
}
本地服务案例代码
@RestController
public class PatientServiceImpl extends BaseApiService implements IPatientService {
/**
* framework服务
*/
@Autowired
private FrameworkServiceFeign frameworkServiceFeign;
/**
* 处理了雪崩 服务降级执行
* HystrixCommand默认开启线程池隔离方式,服务降级,服务熔断
* @return ResponseBase
*/
@HystrixCommand(fallbackMethod = "patientToUserInfoHystrixFallback")
@RequestMapping("/patientToUserInfoHystrix")
public ResponseBase patientToUserInfoHystrix() {
System.out.println("patientToUserInfoHystrix线程池名称:"+Thread.currentThread().getName());
return frameworkServiceFeign.getUserInfo();
}
public ResponseBase patientToUserInfoHystrixFallback(){
return setResultSuccess("返回一个友好的提示:服务降级!服务器忙,请稍后重试!");
}
}
2)、方式二:通过类方式给整个对外服务配置独立线程池
服务降级类
@Component
public class FrameworkServiceFallback extends BaseApiService implements FrameworkServiceFeign {
@Override
public ResponseBase getUserInfo() {
return setResultError("服务器忙,走服务降级");
}
}
对外FeigClient案例代码
@FeignClient(name = "app-kingseok-framework",fallback = FrameworkServiceFallback.class)
public interface FrameworkServiceFeign extends IUserService {
}
本地服务类
@RestController
public class PatientServiceImpl extends BaseApiService implements IPatientService {
/**
* framework服务
*/
@Autowired
private FrameworkServiceFeign frameworkServiceFeign;
@RequestMapping("/patientToUserInfoHystrixDemo2")
public ResponseBase patientToUserInfoHystrixDemo2() {
System.out.println("patientToUserInfoHystrix线程池名称:"+Thread.currentThread().getName());
return frameworkServiceFeign.getUserInfo();
}
}
三、JMeter测试结果
1)、压测前
2)、压测后
编写压测脚本
结果