zoukankan      html  css  js  c++  java
  • dubbo服务降级需要满足三个业务场景

    package com.test.mock.controller;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    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.PostMapping;
    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 java.util.Date;

    /**
    * driosdb-mock
    * <p>
    * dubbo服务降级需要满足三个业务场景:
    * 1、dubbo线程池满(设置dubbo线程数dubbo.protocol.threads)
    * 2、dubbo调用超时(设置超时时间dubbo.consumer.timeout)
    * 3、dubbo未注册(直接停掉服务)
    *
    * @author nanfengxiangbei
    */
    @RequestMapping("/content")
    @RestController
    public class OttContentMockController {
    private static final Logger logger = LoggerFactory.getLogger(OttContentMockController.class);
    private static final Logger mockLog = LoggerFactory.getLogger("OttContentMockController");


    @Reference
    private IServiceProduct productService;

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


    @PostMapping(value = "/mock")
    public String mock(HttpServletRequest httpRequest,
    @RequestBody OttProductContentReqVo reqVo) {

    OttContentRespVo respVo = new OttContentRespVo("cp同步内容到hd成功", "000");
    logger.info("cp同步内容请求开始" + reqVo);
    try {
    ServiceProduct serviceProduct = productService.getServiceProductByProductCode(reqVo.getProductID());
    logger.info("获取产品信息:" + JSON.toJSONString(serviceProduct));

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

    // 模拟线程池满,服务降级
    for (int i = 0; i < 400; i++) {
    new Thread(() -> {
    // 调用服务降级dubbo
    String message = unityAuthService.add(dorisDemo);
    if (!StringUtils.isEmpty(message)) {
    mockLog.error(message);
    }
    }).start();
    }
    } catch (Exception e) {
    respVo.setMessage("cp同步内容到hdc发生异常" + e.getMessage());
    respVo.setResult("1");
    logger.error("cp同步内容到hdc发生异常:", e);
    }
    return JSONObject.toJSONString(respVo);
    }
    }

    import com.google.common.collect.Lists;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.util.List;

    /**
    * dubbo服务降级mock接口实现类
    * IUnityAuthServiceMock 类路径:service接口同级

    * @author nanfengxiangfei
    */

    public class IUnityAuthServiceMock implements IUnityAuthService{

    private static final Logger logger = LoggerFactory.getLogger(IUnityAuthServiceMock.class);


    @Override
    public String add(UnityAuth unityAuth) {
    return "Dubbo服务降级调用:IUnityAuthService.add";
    }
    }
    知人者智,自知者明,胜人者有力,自胜者强。
  • 相关阅读:
    Hibernate整合Druid数据库连接池遇到的问题整合
    Spring 整合quartz 时 定时任务被调用两次以及quartz 的配置
    BigDecimal divide:Non-terminating decimal expansion; no exact representable decimal result.
    Hibernate: Encountered a duplicated sql alias [] during auto-discovery of a native-sql
    oracle与mysql的区别
    搜索评价指标——NDCG
    java 动态修改注解值
    eclipse 取消置顶
    Xiangqi
    All in All
  • 原文地址:https://www.cnblogs.com/nanfengxiangbei/p/15541082.html
Copyright © 2011-2022 走看看