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.                   
  • 相关阅读:
    hive学习
    spark Streaming
    spark sql
    参考
    数论基础
    2020.07.17模拟3
    2020.07.16模拟2
    关于Linux环境下的对拍
    2020.07.15模拟1
    三体
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/5815514.html
Copyright © 2011-2022 走看看