Springboot + Dubbo + Sentinel集成

Springboot + Dubbo + Sentinel集成,第1张

Springboot + Dubbo + Sentinel集成

1.添加依赖    



    com.alibaba.csp
    sentinel-dubbo-adapter
    1.8.2


    com.alibaba.csp
    sentinel-transport-simple-http
    1.8.2




    org.springframework.cloud
    spring-cloud-starter-alibaba-sentinel
    0.2.2.RELEASE

        
2.添加启动参数

-Djava.net.preferIPv4Stack=true
-Dcsp.sentinel.api.port=8720  //指定客户端监控 API 的端口
-Dcsp.sentinel.dashboard.server=localhost:8080

3.全局限流返回

// 作为消费者,设置消费限流的返回
DubboAdapterGlobalConfig.setConsumerFallback(new DubboFallback() {

    @Override
    public Result handle(Invoker invoker, Invocation invocation, BlockException ex) {
        return new RpcResult("服务器处理不过来了,停下来休息休息");
    }
});

//controller 限流返回
WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {

    @Override
    public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex)
            throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        Map map = new HashMap<>(16);
        map.put("rspCode", "0001");
        map.put("rspMsg", "限流了");
        response.getWriter().write(JSON.toJSonString(map));
    }
});

4.注解支持的配置Bean

// 注解支持的配置Bean
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
    return new SentinelResourceAspect();
}

自己看得懂系列,有问题可以私信我

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5635735.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存