SpringBoot | Dubbo之Filter使用示例

欢迎参观我的博客,一个Vue 与 SpringBoot结合的产物:https://poetize.cn

原文链接:https://poetize.cn/article?id=39

@Activate注解可以设置过滤器的条件和顺序

String[] group():URL中的分组如果匹配则激活
String[] value():URL中如果包含该key值,则会激活
String[] before():填写扩展点列表,表示哪些扩展点要在本扩展点之前激活
String[] after():表示哪些扩展点需要在本扩展点之后激活
int order():排序信息

@Activate(group = Constants.PROVIDER, order = -1):表示在服务提供者有效

@Activate(group = Constants.CONSUMER, value = Constants.ACTIVES_KEY):表示在消费者有效

传递用户信息

@Activate(group = {CommonConstants.CONSUMER})
public class DubboConsumerContextFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        LoginUserVo user = UserContext.getLoginUser();
        if (user != null) {
            RpcContext.getServiceContext().setObjectAttachment(UserConstants.DUBBO_USER_KEY, user);
        }
        return invoker.invoke(invocation);
    }
}

@Activate(group = {CommonConstants.PROVIDER})
public class DubboProviderContextFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Object obj = RpcContext.getServiceContext().getObjectAttachment(UserConstants.DUBBO_USER_KEY);
        if (obj == null) {
            return invoker.invoke(invocation);
        }
        try {
            UserContext.setLoginUser((LoginUserVo) obj);
            return invoker.invoke(invocation);
        } finally {
            UserContext.clear();
        }
    }
}

异常传递

@Activate(group = {CommonConstants.PROVIDER})
@Slf4j
public class ServiceExceptionFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Result result = invoker.invoke(invocation);
        if (result.hasException() && GenericService.class != invoker.getInterface()) {
            try {
                Throwable exception = result.getException();
                if (exception.getCause() != null && exception.getCause() instanceof IOException) {
                    log.error("IOException message: {} ", exception.getCause().getMessage());
                    result.setException(new ServiceIoException((IOException) exception.getCause()));
                } else {
                    //其他异常
                    if (exception.getCause() != null) {
                        log.error("ServiceRpcException message: {} ", exception.getCause().getMessage());
                        result.setException(new ServiceRpcException((Exception) exception.getCause()));
                    }
                }
            } catch (Throwable e) {
                log.warn("Fail to ServiceExceptionFilter when called by " + RpcContext.getServiceContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
            }
        }
        return result;
    }
}
posted @ 2023-09-27 09:34  LittleDonkey  阅读(140)  评论(0编辑  收藏  举报