运行环境:JDK 7 或 8,Maven 3.0+ 技术栈:SpringBoot 1.5+、Dubbo 2.5+、ZooKeeper 3.3+
一、什么是 Dubbo Activate 注解 icode9
@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 { // … }
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 { // … }
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 参数,并且参数为有效值时激活,比如配了 icode9 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 { // … }
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 { // … }
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 可选 icode9 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 { // … }
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 { // … }
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 工程,icode9 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; } }
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; } }
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
1 Caused by: java.lang.IllegalStateException: No such extension dubboConsumerFilter for filter /com .alibaba.dubbo.rpc.Filter
1 Caused by: java.lang.IllegalStateException: No such extension dubboConsumerFilter for filter /com .alibaba.dubbo.rpc.Filter
发现这个 Filter 初始化时,报错了。证明没有配置成功。
原来根据官方文档中描述,我们需要配置扩展点配置文件。
在 META-INF icode9 中配置:
1 xxx=com.xxx.XxxFilter
1 xxx=com.xxx.XxxFilter
1 xxx=com.xxx.XxxFilter
Maven icode9 项目目录结构
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)
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)
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/292339.html