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() + ", \nexception:" + e);
OttContentRespVo respVo = new OttContentRespVo("cp同步内容到hd成功", "000");
return JSONObject.toJSONString(respVo);
}
}
posted @ 2021-11-11 17:59  南风向北  阅读(413)  评论(0编辑  收藏  举报