DUBBO 日志过滤器,输出dubbo 接口调用入参、出参等信息(最新推荐)
作者:小目标青年 时间:2023-10-19 22:53:54
Dubbo过滤器概述
Dubbo中的过滤器和Web应用中的过滤器的概念是一样的,提供了在服务调用前后插入自定义逻辑的途径。过滤器是整个Dubbo框架中非常重要的组成部分,Dubbo中很多功能都是基于过滤器扩展而来的。过滤器提供了服务提供者和消费者调用过程的拦截,即每次都执行RPC调用的时候,对应的过滤器都会生效。虽然过滤器的功能强大,但由于每次调用时都会执行,因此在使用的时候需要注意它对性能的影响。
过滤器的使用
一种方式是使用@Active注解默认启用;
一种方式是在配置文件中配置;
<!-- 消费方调用过程拦截 -->
<dubbo:reference filter="xxx, yyy" />
<!-- 消费方调用过程默认 * ,将拦截所有reference -->
<dubbo:consumer filter="xxx, yyy" />
<!-- 服务提供方调用过程拦截 -->
<dubbo:service filter="xxx, yyy" />
<!-- 服务提供方调用过程默认 * ,将拦截所有service -->
<dubbo:provider filter="xxx, yyy" />
下面看下DUBBO 日志过滤器,输出dubbo 接口调用入参、出参等信息
①自定义一个过滤器 DubboLoggerFilter.java
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
/**
* @Author: JCccc
* @Description: dubbo请求和响应拦截(记录请求参数和响应结果和时长)
* @Date: Create in 19:03 2021/1/26
*/
public class DubboLoggerFilter implements Filter {
protected Logger logger = LoggerFactory.getLogger(getClass());
/**
* rpc日志最长字符串
*/
private final static Integer MAX_LOG_LENGTH=5000;
/**
* rpc日志超过长度截取长度
*/
private final static Integer REMAINING_LOG_LENGTH=1000;
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String methodName = invocation.getMethodName();
Object[] arguments = invocation.getArguments();
String className = invoker.getInterface().getName();
String callMethod = className + "." + methodName;
String argsJson = JSON.toJSONString(arguments);
logger.info("rpc接口callMethod:{}>>入参:{}", callMethod, argsJson);
long start = System.currentTimeMillis();
AsyncRpcResult result = (AsyncRpcResult)invoker.invoke(invocation);
if(result.hasException()){
logger.info("rpc接口callMethod:{},接口耗时:{},异常:{},", callMethod, System.currentTimeMillis() - start,result.getException().getMessage());
} else {
Object resultString = JSON.toJSON(result.getAppResponse().getValue());
if(resultString!=null&&resultString.toString().length()>MAX_LOG_LENGTH){
resultString=resultString.toString().substring(0,REMAINING_LOG_LENGTH)+"...";
}
logger.info("rpc接口callMethod:{},出参:{},接口耗时:{}", callMethod,resultString , System.currentTimeMillis() - start);
}
return result;
}
}
② 加上配置
来源:https://blog.csdn.net/qq_35387940/article/details/128338614
标签:DUBBO,过滤器,接口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#构造函数在基类和父类中的执行顺序
2021-10-24 06:58:03
Android中View的炸裂特效实现方法详解
2022-07-25 17:59:28
SQL语句删除和添加外键、主键的方法
2023-04-16 22:18:35
Spring Cloud Eureka(全面解析) 大白话
2022-11-12 22:43:02
![](https://img.aspxhome.com/file/2023/5/62735_0s.png)
Java @Async注解导致spring启动失败解决方案详解
2022-03-06 00:15:17
![](https://img.aspxhome.com/file/2023/9/69669_0s.jpg)
使用代理模式来进行C#设计模式开发的基础教程
2023-06-08 17:01:56
![](https://img.aspxhome.com/file/2023/2/104272_0s.gif)
SpringMVC的执行流程及组件详解
2021-06-17 23:29:21
Spring中统一异常处理示例详解
2022-08-01 07:09:24
C#浏览器提示跨域问题解决方案
2023-08-30 17:37:28
![](https://img.aspxhome.com/file/2023/0/108930_0s.png)
C++编程中用put输出单个字符和cin输入流的用法
2023-05-28 06:14:49
Java生成动态版验证码的方法实例
2023-08-26 03:53:08
Java设计模式之监听器模式实例详解
2022-10-04 02:51:44
![](https://img.aspxhome.com/file/2023/8/72568_0s.png)
详解flutter中常用的container layout实例
2022-07-25 07:49:53
![](https://img.aspxhome.com/file/2023/6/113306_0s.png)
简单捋捋@RequestParam 和 @RequestBody的使用
2022-11-02 16:19:20
![](https://img.aspxhome.com/file/2023/4/67694_0s.png)
C#中感叹号(!) 的作用总结
2021-10-14 15:08:12
![](https://img.aspxhome.com/file/2023/1/116551_0s.png)
Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑
2023-03-07 08:08:24
![](https://img.aspxhome.com/file/2023/3/137193_0s.png)
Android开发中WebView的简单使用小结
2022-09-11 00:22:43
![](https://img.aspxhome.com/file/2023/2/116012_0s.jpg)
java8 如何实现分组计算数量和计算总数
2022-05-05 01:17:32
浅谈java安全编码指南之死锁dead lock
2023-06-22 04:49:19
C++形参与实参的区别实例解析
2023-11-02 23:39:05