zoukankan      html  css  js  c++  java
  • Dubbo的全局Filter配置

    转载:https://www.cnblogs.com/mumuxinfei/p/9305710.html

    前言:
      之前也写过dubbo的filter的文章, 后来和同事也有过交流, 才发生自己对dubbo的filter的机制, 还是存在一些误解, 尤其是自定义filter的定位, 不是那么清晰. 本文主要是补充一下, 自定义的filter如何成为全局filter, 或者说, 它不需要在bean的定义申明中指定filter标签.

    前文回顾:
      1. Dubbo透传traceId/logid的一种思路 
      2. Dubbo的Filter链梳理---分组可见和顺序调整 
      3. Dubbo的Filter实战--整合Oval校验框架 

    案列:
      比如自定义filter, 如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package com.test
     
    public class StatFilter implements Filter {
     
        @Override
        public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
           return invoker.invoke(invocation);
        }
     
    }

      其需要做的额外工作如下:
      在META-INF/dubbo目录下, 添加com.alibaba.dubbo.rpc.Filter文件, 其内容为

    1
    statFilter=com.test.StatFilter

      而对于每个需要用到该filter的dubbo provider/consumer, 都需要在xml申明中添加filter标签, 比如:

    1
    <dubbo:reference id="echoService" check="false" interface="com.test.EchoService" filter="statFilter" />

      对于具体的一个dubbo provider/consumer实例这种的配置, 绝对没问题, 问题是如果要作用所有的dubbo provider/consumer实例, 这样的copy/paste有点low.

    全局配置:
      其实实现全局配置, 非常的简单, 一种方式是通过额外的配置, 一种通过指定@Activate的group实现.
      1. 额外的配置方式
      以上文的案例为例, 在resource目录下, 添加dubbo.properties文件, 然后配置如下:

    1
    2
    3
    4
    # 如果该filter要作用于为provider
    dubbo.provider.filter=com.test.StatFilter
    # 如果该filter要作用于为consumer
    dubbo.consumer.filter=com.test.StatFilter

      具体的目录结果如下:
      
      2. 指定@Activate的group
      这个方法, 就比较简单了, 而且也不需要额外的配置文件了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package com.test
     
    import com.alibaba.dubbo.common.Constants;
    import com.alibaba.dubbo.common.extension.Activate;
     
    @Activate(
            group = {Constants.PROVIDER, Constants.CONSUMER},
            order = -2000
    )
    public class StatFilter implements Filter {
     
        @Override
        public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
           return invoker.invoke(invocation);
        }
     
    }

      

    总结:
      权当做笔记吧, 确实dubbo filter给了开发者很大自由度和空间.

  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/12016349.html
Copyright © 2011-2022 走看看