SpringCloud整合Hystrix断路器
- 导入依赖,最好去官网查找对应的版本依赖地址:http://spring.io/projects/spring-cloud-netflix#learn
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 在启动类上面填加注解 @EnableCircuitBreaker
- 在最外层的Api使用,也就是Controller层中需要添加断路器的方法上,添加注解@HystrixCommand(fallbackMethod = “abc(方法名)”), 编写abc方法的具体实现,方法的参数必须和加@HystrixCommand注解的方法的参数一样。其实就好比异常处理,如果加@HystrixCommand注解的方法中出现异常,会走注解中命名的方法。
@RestController
@RequestMapping("api/v1/order")
public class OrderController {
//这个是service层
@Autowired
private ProductOrderService productOrderService;
@RequestMapping("save")
@HystrixCommand(fallbackMethod = "abc")
public Object save(@RequestParam("user_id")int userId,
@RequestParam("product_id") int productId){
Map<String, Object> data = new HashMap<>();
data.put("code", 0);
data.put("data", productOrderService.save(userId, productId));
return data;
}
//注意,方法中的参数一定要和api方法的一致
private Object abc(int userId, int productId){
Map<String, Object> msg = new HashMap<>();
msg.put("code", -1);
msg.put("msg", "抢购人数太多,您被挤出来了,稍等重试");
return msg;
}
}
feign结合Hystrix的使用
- 开启feign支持hystrix (旧版本默认支持,新版本默认关闭),设置Hystrix的超时时间默认是1秒
#开启feign支持hystrix
feign:
hystrix:
enabled: true
#设置hystrix的超时时间 默认是1000毫秒
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 2000
- 自定义一个类,去实现添加了@FeignClient(name = “product-service(服务名)”, fallback = xxx.class)注解的接口。xxx是实现了该接口的类名。
自定义的类必须要添加到spring容器中所以一定要添加@Component注解
/**
* 商品服务客户端
*/
@FeignClient(name = "product-service", fallback = ProductClientFallback.class)
public interface ProductClient {
@GetMapping("/api/v1/product/find")
String findById(@RequestParam(value = "id") int id);
}
=====================================================================
/**
* 针对商品服务,错降级处理
*/
@Component
public class ProductClientFallback implements ProductClient {
@Override
public String findById(int id) {
System.out.println("feign 调用product-service findbyid 异常");
return null;
}
}
熔断降级服务异常报警通知
- 加入redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 在application.yml的配置文件中配置redis
spring:
redis:
database: 0 # Redis数据库索引(默认为0)
host: 127.0.0.1 #Redis服务器地址
port: 6379 # Redis服务器连接端口
timeout: 2000 # 连接超时时间(毫秒)
//注意,方法签名一定要要和api方法一致
private Object saveOrderFail(int userId, int productId){
//监控报警
String saveOrderKye = "save-order";
//从redis数据库取数据来判断是否发送发送过短信
String sendValue = redisTemplate.opsForValue().get(saveOrderKye);
//新建一个线程去运行
new Thread( ()->{
//判断是否发送过短信,sendValue如果取出来的是null,就说明没有发送过短信
if (StringUtils.isBlank(sendValue)) {
System.out.println("紧急短信,用户下单失败,请离开查找原因);
//发送一个http请求,调用短信服务 TODO
//发送过短信后就随便储存一个值,设置过期时长
redisTemplate.opsForValue().set(saveOrderKye, "save-order-fail", 20, TimeUnit.SECONDS);
}else{
System.out.println("已经发送过短信,20秒内不重复发送");
}
}).start();
Map<String, Object> msg = new HashMap<>();
msg.put("code", -1);
msg.put("msg", "抢购人数太多,您被挤出来了,稍等重试");
return msg;
}
熔断和降级互相交集
相同点:
1. 从可用性和可靠性触发,为了防止系统崩溃
2. 最终让用户体验到的是某些功能暂时不能用
不同点:
1. 熔断一般是下游服务故障导致的,而服务降级一般是从整体系统负荷考虑,由调用方控制