博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dubbo服务配置sentinel
阅读量:7045 次
发布时间:2019-06-28

本文共 3127 字,大约阅读时间需要 10 分钟。

hot3.png

前言

    前几天ali开源了Sentinel项目,Sentinel中文意思是哨兵,作为dubbo的配套项目,看字面意思是dubbo的一个监控系统,sentinel自己的官方文档上是这样说的:Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。本文就是想简单的配置一下sentinel。

准备

Sentinel的官方文档是放在 ,配合上一篇文章配置的dubbo项目,无需依赖其他的外部资源。

开始

    在provider端的 pom.xml 文件中加入以下依赖:

com.alibaba.csp
sentinel-core
0.1.1

  在sayHello服务的中嵌入Sentinel的代码:

public class SayHelloImpl implements SayHello {    @Override    public String sayHello(String name) {        initFlowRules();        Entry entry = null;        try {            entry = SphU.entry("HelloWorld");            System.out.println("hello world");            return "Hello " + name;        } catch (BlockException e1) {            System.out.println("exception is " + BlockException.isBlockException(e1));            System.out.println("block!");        } finally {            if (entry != null) {                entry.exit();            }        }        return "";    }    private void initFlowRules() {        List
rules = new ArrayList
(); FlowRule rule = new FlowRule(); rule.setResource("HelloWorld"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // Set limit QPS to 20. rule.setCount(5); rules.add(rule); FlowRuleManager.loadRules(rules); }}

      在上述代码中,把限流的方式设置为QPS = 5,然后改造下客户端的代码,进行20 次的调用:

public class ConsumerApplication {    public static void main(String[] args) {        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");        SayHello sayHello = (SayHello) ctx.getBean("sayHello");        for (int i = 0; i < 20; i++) {            String s = sayHello.sayHello("张三");            System.out.println(i + ":" + s);        }    }}

   启动Provider端和Consumer端后,看到provider的后台打印的是:

hello worldhello worldhello worldhello worldhello worldexception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!exception is trueblock!

     只有前五次的调用成功了,其他的都在报了BlockException。所以在限流的时候,可以通过在BlockException 中进行限流那些请求的访问逻辑处理。

    Sentinel其实有提供一套注解,实现代码零侵入,接下来把代码改成注解形式实现,增加Sentinel的注解依赖包:

com.alibaba.csp
sentinel-annotation-aspectj
0.1.1

    启动类改成springboot启动,增加注解的启动配置:

@SpringBootApplicationpublic class ProviderApplication {    public static void main(String[] args) throws IOException {        SpringApplication.run(ProviderApplication.class,args);        System.in.read();    }    @Bean    public SentinelResourceAspect sentinelResourceAspect() {        return new SentinelResourceAspect();    }    @Bean    public RegistryConfig registryConfig() {        RegistryConfig registryConfig = new RegistryConfig();        registryConfig.setAddress("zookeeper://127.0.0.1:2181");        registryConfig.setClient("provider");        return registryConfig;    }}

 

转载于:https://my.oschina.net/u/3470849/blog/1934056

你可能感兴趣的文章
CSS 怀疑 Verify
查看>>
linux下jdk、tomcat的安装及项目的部署和去掉项目名访问详细总结
查看>>
用户界面设计
查看>>
linux-raid (四) raid0
查看>>
怎么在docker容器的mysql的编码格式变为utf8
查看>>
12306常用技能
查看>>
Struts2访问Servlet API的三种方式
查看>>
正确理解使用Vue里的nextTick方法
查看>>
VUE组件如何与iframe通信问题
查看>>
csv表格处理(上)-- JS 与 PHP 协作导入导出
查看>>
web前端开发之div+css教程精华收集一
查看>>
select * from (select user())a 为什么是查询user()的意思?
查看>>
Android SDK无法更新问题解决
查看>>
LeetCode – Refresh – N-Queens II
查看>>
LeetCode 639: DecodeWaysII
查看>>
Linux系统简介--LInix系统随笔(一)
查看>>
TCP接入层的负载均衡、高可用、扩展性架构
查看>>
使用Kieker(AspectJ)监控控制台程序
查看>>
C#多线程之旅(1)——介绍和基本概念
查看>>
Spring常用注解汇总
查看>>