Dubbo自定义ExceptionFilter实现业务异常透传

在前一篇文章中,讲到了dubbo自定义异常如何进行传递,在consumer中如何获取到provider的业务异常信息,那篇文章的主旨只是归纳总结,这篇文章来看一下,具体如何自定义filter来实现dubbo的异常透传。

一、实现dubbo提供的Filter接口

package com.zhiri.biz.center.application.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.service.GenericService;

import java.lang.reflect.Method;

/**
 * 自定义dubbo异常处理,不让dubbo包一层RuntimeException
 *
 * @author SongFei
 * @date 2020/5/7 17:03
 */
@Activate(group = CommonConstants.PROVIDER)
@Slf4j
public class DubboExceptionFilter implements Filter, Filter.Listener {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        return invoker.invoke(invocation);
    }

    @Override
    public void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation) {
        if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
            try {
                Throwable exception = appResponse.getException();

                // directly throw if it's checked exception
                if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {
                    return;
                }
                // directly throw if the exception appears in the signature
                try {
                    Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
                    Class<?>[] exceptionClassses = method.getExceptionTypes();
                    for (Class<?> exceptionClass : exceptionClassses) {
                        if (exception.getClass().equals(exceptionClass)) {
                            return;
                        }
                    }
                } catch (NoSuchMethodException e) {
                    return;
                }

                // for the exception not found in method's signature, print ERROR message in server's log.
                log.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);

                // directly throw if exception class and interface class are in the same jar file.
                String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
                String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
                if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) {
                    return;
                }
                // directly throw if it's JDK exception
                String className = exception.getClass().getName();
                if (className.startsWith("java.") || className.startsWith("javax.")) {
                    return;
                }
                // customer exception
                if (className.startsWith("com.zhiri.common.base")) {
                    return;
                }
                // directly throw if it's dubbo exception
                if (exception instanceof RpcException) {
                    return;
                }

                // otherwise, wrap with RuntimeException and throw back to the client
                appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
            } catch (Throwable e) {
                log.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
            }
        }
    }

    @Override
    public void onError(Throwable throwable, Invoker<?> invoker, Invocation invocation) {
        log.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + throwable.getClass().getName() + ": " + throwable.getMessage(), throwable);
    }

}

二、在resources目录下新建Filter文件

Dubbo自定义ExceptionFilter实现业务异常透传

文件内容:

  dubboExceptionFilter=com.zhiri.biz.center.application.filter.DubboExceptionFilter

文件目录层级:/META-INF/dubbo,文件全名就叫“org.apache.dubbo.rpc.Filter”,也可以叫“com.alibaba.dubbo.rpc.Filter”。

这一步是为了第三步做准备,因为这个filter不是spring的bean,不能直接在配置中引入,所以需要额外声明。

三、在dubbo配置中引入自定义的filter

  dubbo.provider.filter=-exception,dubboExceptionFilter

逗号分隔,两个参数,前面那个是将dubbo默认的ExceptionFilter去除,后面那个是第二步中配置的Filter。

dubbo分布式

看完文章,有任何疑问,请加入群聊一起交流!!!

扫码关注本站微信公众号,第一时间获取网站更新动态!

本文地址:https://www.jiweichengzhu.com/article/53d62a6e4f1e4e1c9fb64b774f0e6f00

**关于转载:**若站内作品和文章得到了你的青睐,欢迎各位朋友分享到个人站长或者朋友圈,但转载请注明文章出处和原文链接!

很赞哦! (5)

上一篇:如何在dubbo中捕获并处理自定义业务异常

下一篇:记一次dubbo自定义ExceptionFilter踩的坑

Dubbo中,可以通过自定义异常实现屏蔽IP端口的功能。 首先,可以定义一个自定义异常类,例如BlockedIPException,用于表示被屏蔽的IP地址和端口号。 ```java public class BlockedIPException extends RuntimeException { private String ip; private int port; public BlockedIPException(String ip, int port) { super("IP " + ip + ":" + port + " has been blocked!"); this.ip = ip; this.port = port; } public String getIp() { return ip; } public int getPort() { return port; } } ``` 然后,在服务提供者中,可以通过实现DubboExceptionFilter接口,在invoke()方法中判断请求的IP地址和端口号是否被屏蔽,如果是,则抛出BlockedIPException异常。 ```java public class IPBlockExceptionFilter implements ExceptionFilter { private Set<String> blockedIps = new HashSet<>(); public IPBlockExceptionFilter() { // 初始化屏蔽的IP地址和端口号 blockedIps.add("127.0.0.1:8080"); } @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { // 获取请求的IP地址和端口号 String remoteAddress = RpcContext.getContext().getRemoteAddressString(); if (blockedIps.contains(remoteAddress)) { throw new BlockedIPException(remoteAddress.split(":")[0], Integer.parseInt(remoteAddress.split(":")[1])); } return invoker.invoke(invocation); } } ``` 最后,在服务提供者的配置文件中,将IPBlockExceptionFilter配置为异常过滤器即可。 ```xml <dubbo:service interface="com.example.demo.DemoService" ref="demoService"> <dubbo:parameter key="exception-filter" value="com.example.demo.filter.IPBlockExceptionFilter"/> </dubbo:service> ``` 这样,当客户端请求的IP地址和端口号被屏蔽时,Dubbo框架会抛出BlockedIPException异常,可以通过该异常实现屏蔽IP端口的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值