SpringCloud之异常报警通知(⼋)
在之前整合降级的基础上,整合redis,达到报警的效果(redis的启动还是之前boot⾥⾯整合的redis)
order-service
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
application:
name: order-service
redis:
port: 6379
host: 192.168.180.113
timeout: 2000
修改web层,添加redis的逻辑
1@RestController
2 @RequestMapping("/api/v1/order")
3public class OrderController {
4
5
6 @Autowired(required = false)
7private ProductOrderServiceImpl productOrderService;
8
9 @Autowired
10private StringRedisTemplate redisTemplate;
11
12
13 @RequestMapping("/save")
14 @HystrixCommand(fallbackMethod="saveOrderFail")
15public Object save(@RequestParam("user_id")int userId, @RequestParam("product_id") int productId, HttpServletRequest request){ 16
17 Map<String, Object> data = new HashMap<>();
18 data.put("code", 0);
19 data.put("data", productOrderService.save(userId, productId));
20return data;
21 }
22
23
24//注意,⽅法签名⼀定要要和api⽅法⼀致
25private Object saveOrderFail(int userId, int productId, HttpServletRequest request){
26
27
28//监控报警
29 String saveOrderKye = "save-order";
30
springcloud和springboot31 String sendValue = redisTemplate.opsForValue().get(saveOrderKye);
32final String ip = RemoteAddr();
33new Thread( ()->{
34if (StringUtils.isBlank(sendValue)) {
35 System.out.println("紧急短信,⽤户下单失败,请离开查原因,ip地址是="+ip);
36//发送⼀个http请求,调⽤短信服务 TODO
37 redisTemplate.opsForValue().set(saveOrderKye, "save-order-fail", 20, TimeUnit.SECONDS);
38 }else{
39 System.out.println("已经发送过短信,20秒内不重复发送");
40 }
41
42 }).start();
43
44
45 Map<String, Object> msg = new HashMap<>();
46 msg.put("code", -1);
47 msg.put("msg", "抢购⼈数太多,您被挤出来了,稍等重试");
48return msg;
49 }
50
51
52 }
测试:
2019-10-19 20:56:11.880 INFO 20020 --- [erListUpdater-0] fig.ChainedDynamicProperty : Flipping property: product-service.ribbon.Active ConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
feign 调⽤product-service findbyid 异常
2019-10-19 20:56:12.771 INFO 20020 --- [ HystrixTimer-1] EpollProvider : Starting without optional epoll library
2019-10-19 20:56:12.774 INFO 20020 --- [ HystrixTimer-1] KqueueProvider : Starting without optional kqueue library
已经发送过短信,20秒内不重复发送
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论