栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

处理限流,熔断事件触发时出现的自定义异常(自定义页面返回值)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

处理限流,熔断事件触发时出现的自定义异常(自定义页面返回值)

@Component
public class ServiceBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       BlockException e) throws Exception {
        //response.setStatus(601);
        //设置响应数据的编码
        response.setCharacterEncoding("utf-8");
        //告诉客户端要响应的数据类型以及客户端以什么编码呈现数据
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw=response.getWriter();
        Map map=new HashMap<>();
        if(e instanceof DegradeException){//降级、熔断
            map.put("status",601);
            map.put("message", "服务被熔断了!");
        }else if(e instanceof FlowException){
            map.put("status",602);
            map.put("message", "服务被限流了!");
        }else{
            map.put("status",603);
            map.put("message",
                    "Blocked by Sentinel (flow limiting)");
        }
        //将map对象转换为json格式字符串
        String jsonStr=new ObjectMapper().writevalueAsString(map);
        pw.println(jsonStr);
        pw.flush();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/333268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号