zoukankan      html  css  js  c++  java
  • Spring Boot 中如何使用 Dubbo Activate 扩展点

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!

    『 公司的核心竞争力在于创新 – 《启示录》 』

    继续上一篇:《 Springboot 整合 Dubbo/ZooKeeper 》,在 Spring Boot 使用 Dubbo Activate 扩展点。这是一个群友问的,我总结下,分享给更多人。

    本文提纲
    一、什么是 Dubbo Activate 注解
    二、使用 Dubbo Activate
    三、小结

    运行环境:JDK 7 或 8,Maven 3.0+
    技术栈:SpringBoot 1.5+、Dubbo 2.5+、ZooKeeper 3.3+

    一、什么是 Dubbo Activate 注解

    @Activate 是一个 Duboo 框架提供的注解。在 Dubbo 官方文档上有记载:
    对于集合类扩展点,比如:Filter, InvokerListener, ExportListener, TelnetHandler, StatusChecker等, 可以同时加载多个实现,此时,可以用自动激活来简化配置。

    用 @Activate 来实现一些 Filter ,可以具体如下:
    1. 无条件自动激活
    直接使用默认的注解即可

    1
    2
    3
    4
    5
    6
    7
    import com.alibaba.dubbo.common.extension.Activate;
    import com.alibaba.dubbo.rpc.Filter;
      
    @Activate // 无条件自动激活
    public class XxxFilter implements Filter {
        // ...
    }

    2. 配置 xxx 参数,并且参数为有效值时激活,比如配了cache=”lru”,自动激活 CacheFilter

    1
    2
    3
    4
    5
    6
    7
    import com.alibaba.dubbo.common.extension.Activate;
    import com.alibaba.dubbo.rpc.Filter;
      
    @Activate("xxx") // 当配置了xxx参数,并且参数为有效值时激活,比如配了cache="lru",自动激活CacheFilter。
    public class XxxFilter implements Filter {
        // ...
    }

    3. 只对提供方激活,group 可选 provider 或 consumer

    1
    2
    3
    4
    5
    6
    7
    8
    import com.alibaba.dubbo.common.extension.Activate;
    import com.alibaba.dubbo.rpc.Filter;
      
    @Activate(group = "provider", value = "xxx")
    // 只对提供方激活,group可选"provider"或"consumer"
    public class XxxFilter implements Filter {
        // ...
    }

    二、使用 Dubbo Activate 注解

    基于以前的 springboot-dubbo-server 和 springboot-dubbo-client 工程,GitHub 地址:https://github.com/JeffLi1993/springboot-learning-example 。

    这里我们在消费端,既 springboot-dubbo-client 工程上添加一个 Filter。代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package com.xxx;
      
    import com.alibaba.dubbo.rpc.Filter;
    import com.alibaba.dubbo.rpc.Invoker;
    import com.alibaba.dubbo.rpc.Invocation;
    import com.alibaba.dubbo.rpc.Result;
    import com.alibaba.dubbo.rpc.RpcException;
      
      
    public class XxxFilter implements Filter {
        public Result invoke(Invoker<?> invoker,
    Invocation invocation) throws RpcException {
            // before filter ...
            Result result = invoker.invoke(invocation);
            // after filter ...
            return result;
        }
    }

    启动 client 工程发现,Console 报错,出现:

    1
    Caused by: java.lang.IllegalStateException: No such extension dubboConsumerFilter for filter/com.alibaba.dubbo.rpc.Filter

    发现这个 Filter 初始化时,报错了。证明没有配置成功。

    原来根据官方文档中描述,我们需要配置扩展点配置文件。

    在 META-INF 中配置:

    1
    xxx=com.xxx.XxxFilter

    Maven 项目目录结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    src
     |-main
        |-java
            |-com
                |-xxx
                    |-XxxFilter.java (实现Filter接口)
        |-resources
            |-META-INF
                |-dubbo
                    |-com.alibaba.dubbo.rpc.Filter (纯文本文件,内容为:xxx=com.xxx.XxxFilter)

    三、小结

    调用拦截扩展的应用场景很多,比如黑白名单,比如 IP 等。

    欢迎扫一扫我的公众号关注 — 及时得到博客订阅哦!
    — http://www.bysocket.com/ —
    — https://github.com/JeffLi1993 —

  • 相关阅读:
    工作总结系列---【端口被占用,如何取消端口进程】
    封装工具系列---【封装H5页面管理机构和证件类型下拉框数据处理的方法】
    工作总结系列---【vue页面实现导出表格效果】
    vscode---【内网开发下,离线安装vscode插件vsix文件】
    工作总结系列---【IDE深色背景下,鼠标移动也变成黑色,看不清怎么解决???】
    node系列---【为什么要设置镜像源?怎么配置?】
    git系列---【如何在本地修改别人的代码图解】
    git系列---【git如何回退到上一个版本??】
    封装工具系列---【补零操作】
    封装的各类工具系列---【时间格式化为YYYY-MM-DD格式 】
  • 原文地址:https://www.cnblogs.com/Alandre/p/6727133.html
Copyright © 2011-2022 走看看