zoukankan      html  css  js  c++  java
  • dubbo 自定义过滤器,打印接口调用信息

    dubbo提供了web filter类似的com.alibaba.dubbo.rpc.Filter,这样,我们可以在dubbo提供的服务提供方和消费方都可以自定义过滤 器,从而可以获得方法调用的时间或参数、返回结果及异常信息。我们可以利用log打印出来。而且,这个过滤器机制,也是分布式跟踪系统的一部分。

          下面代码实例是一个自定义过滤器例子,获得方法调用的参数、返回结果、执行时间及异常信息的log功能。

    1. public class ElapsedTimeFilter implements Filter {  
    2.   
    3.     private static Logger log = LoggerFactory  
    4.             .getLogger(ElapsedTimeFilter.class);  
    5.   
    6.     @Override  
    7.     public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {  
    8.         long start = System.currentTimeMillis();  
    9.         Result result = invoker.invoke(invocation);  
    10.         long elapsed = System.currentTimeMillis() - start;  
    11.         if (invoker.getUrl() != null) {  
    12.   
    13.             // log.info("[" +invoker.getInterface() +"] [" + invocation.getMethodName() +"] [" + elapsed +"]" );  
    14.             log.info("[{}], [{}], {}, [{}], [{}], [{}]   ", invoker.getInterface(), invocation.getMethodName(),   
    15.                          Arrays.toString(invocation.getArguments()), result.getValue(),  
    16.                        result.getException(), elapsed);  
    17.   
    18.         }  
    19.         return result;  
    20.     }  
    21.   
    22. }  


    其实,dubbo内部,也有很多已经实现好的不同功能的过滤器,如:


    我们自定义了过滤器,还的按照dubbo spi机制,还得需要配置:

    在服务消费方或提供方还需要配上这个过滤器,消费方例子:

      1. <dubbo:consumer id="xx"  
      2.                     
      3.                     filter="elapsedTimeFilter"  
      4.                      
      5.                     retries="0"/>  
      6.                   
  • 相关阅读:
    Java8 Stream Function
    PLINQ (C#/.Net 4.5.1) vs Stream (JDK/Java 8) Performance
    罗素 尊重 《事实》
    小品 《研发的一天》
    Java8 λ表达式 stream group by max then Option then PlainObject
    这人好像一条狗啊。什么是共识?
    TOGAF TheOpenGroup引领开发厂商中立的开放技术标准和认证
    OpenMP vs. MPI
    BPMN2 online draw tools 在线作图工具
    DecisionCamp 2019, Decision Manager, AI, and the Future
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/5815514.html
Copyright © 2011-2022 走看看