zoukankan      html  css  js  c++  java
  • springboot与resilience4j集成

    原创转载请注明出处: https://www.cnblogs.com/agilestyle/p/15166657.html

    package com.test.resilience4j.controller;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
    import org.apache.commons.lang.StringUtils;
    import org.apache.dubbo.config.annotation.Reference;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Date;

    /**
    * springboot与resilience4j集成
    *
    * <dependency>
    * <groupId>io.github.resilience4j</groupId>
    * <artifactId>resilience4j-spring-boot2</artifactId>
    * <version>1.7.1</version>
    * </dependency>
    *
    *
    * 需注意的地方:
    * 1、name = "cp-auth-web" 和resilience4j.circuitbreaker.instances.cp-auth-web.base-confi须一致
    * 2、@RequestMapping请求方法和fallbackMethod方法的参数须一致
    * 3、不能try...catch...捕获异常
    * @author nanfengxiangbei
    * @date 2021-11-12
    */
    @RequestMapping("/content")
    @RestController
    public class OttContentFallBackController {
    private static final Logger logger = LoggerFactory.getLogger(OttContentFallBackController.class);
    private static final Logger mockLog = LoggerFactory.getLogger("OttContentMockController");

    @Reference
    private IServiceProduct productService;

    @Reference(check = false, mock = "true")
    private IUnityAuthService unityAuthService;


    @RequestMapping(value = "/fallback")
    @CrossOrigin(origins = "*")
    @CircuitBreaker(name = "cp-auth-web", fallbackMethod = "fallbackTest")
    public String fallBack(@RequestBody OttProductContentReqVo reqVo, HttpServletRequest request, HttpServletResponse response) {

    OttContentRespVo respVo = new OttContentRespVo("cp同步内容到hd成功", "000");
    logger.debug("fallbackTest请求开始" + JSON.toJSONString(reqVo));

    ServiceProduct serviceProduct = productService.getServiceProductByProductCode(reqVo.getProductID());
    logger.debug("获取产品信息:" + JSON.toJSONString(serviceProduct));

    UnityAuth dorisDemo = new UnityAuth();
    dorisDemo.setContentId(reqVo.getContentID());
    dorisDemo.setProductCode(serviceProduct.getProductCode());
    dorisDemo.setCreateTime(new Date());
    dorisDemo.setRouteId(System.currentTimeMillis());
    dorisDemo.setAccount("136870000000");
    dorisDemo.setAllPass(0);
    dorisDemo.setResult(0);
    dorisDemo.setRouteEnd(System.currentTimeMillis());
    dorisDemo.setRequestBody(JSON.toJSONString(dorisDemo));

    // 调用服务降级dubbo
    String message = unityAuthService.add(dorisDemo);
    if (!StringUtils.isEmpty(message)) {
    mockLog.error(message);
    }
    return JSONObject.toJSONString(respVo);
    }

    /**
    * 当请求发生异常时,fallbackMethod里指定调用此方法
    *
    * @param reqVo 请求参数
    * @param request 请求参数
    * @param response 响应参数
    * @param e 异常
    * @return 响应消息
    */
    private String fallbackTest(@RequestBody OttProductContentReqVo reqVo,
    HttpServletRequest request,
    HttpServletResponse response,
    RuntimeException e) {
    mockLog.error("resilience4j invoked with operation: " + reqVo.getAction() + ", exception:" + e);
    OttContentRespVo respVo = new OttContentRespVo("cp同步内容到hd成功", "000");
    return JSONObject.toJSONString(respVo);
    }
    }
    知人者智,自知者明,胜人者有力,自胜者强。
  • 相关阅读:
    我开发过程中用到的工具
    我最近写的DataGrid合并/删除相同列通用函数,跟大家分享
    开源项目- Archive Explorer
    SQLite准备出3.0了!
    软件缺陷管理指南 3
    介绍一些.net好站点
    几个著名java开源缓存系统的介绍
    SQL中的Where,Group By,Order By和Having的用法/区别
    Unix/Linux中Cron的用法
    java中判断字符串是否为纯数字
  • 原文地址:https://www.cnblogs.com/nanfengxiangbei/p/15541026.html
Copyright © 2011-2022 走看看