dubbo 自定义异常

前言

在很多公司,使用dubbo做微服务治理也是很常见的一种方式,简单来说,就是服务提供者一方将服务注册并发布到注册中心,消费者订阅服务,然后像调用本地接口一样;

但是在实际实践中,经常有这么一种场景,就是对于服务消费者来说,当调用服务生产者的服务接口时,一旦服务提供者的接口抛出异常,如果消费端不使用 try-catch 捕捉的话,在进行问题排查、故障分析时,将会是个头疼的问题;

对于消费端来说,不可能在所有的调用dubbo接口的地方都用 try-catch进行包裹吧?有没有一种办法,用来统一处理这样的服务接口调用异常方式呢?答案是肯定的,可以使用dubbo自定义过滤器,通过过滤器统一拦截调用异常问题;

操作步骤

一、创建一个公共的用于处理异常的工程

ComnonFilter 类,只需要实现dubbo提供的Filter 接口即可

package com.congge.filter;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.service.GenericService;

import java.util.Date;

@Activate(group = {Constants.PROVIDER,Constants.CONSUMER})
public class CommonFilter implements Filter {

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

        Result result = null;
        try {
            result = invoker.invoke(invocation);
            if (result.hasException() && GenericService.class != invoker.getInterface()) {
                Throwable exception = result.getException();
                String data = String.format("
[level]:Error,[createTime]:%s,[serviceName]:%s,[methodName]:%s,[inputParam]:%s",
                        DateUtil.formatDateTime(new Date()),
                        invoker.getInterface().getName(),
                        invocation.getMethodName(),
                        JSON.toJSONString(invocation.getArguments()));
                System.out.println(data);
                System.out.println(exception);
            }
        }catch (RuntimeException e){
            String data = String.format("
[level]:Error," +
                    "[createTime]:%s," +
                    "[serviceName]:%s," +
                    "[methodName]:%s," +
                    "[inputParam]:%s",
                    DateUtil.formatDateTime(new Date()),
                    invoker.getInterface().getName(),
                    invocation.getMethodName(),
                    JSON.toJSONString(invocation.getArguments()));
            System.out.println(data);
            System.out.println(e);
        }
        return result;
    }
}

在resources目录下创建相关的目录,注意文件路径和文件名称是固定的,文件内容如下

二、生产端配置文件改造

1、pom中导入上面这个公共依赖的maven工程坐标,然后在配置文件中,将过滤器的名称配置进去

2、生产端提供的服务中手动添加一个异常

三、消费端配置

消费端暂时无需做其他配置

import com.congge.service.HelloService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ConsumerMain {
    public static void main(String[] args) throws Exception {
   
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-consumer.xml");
        HelloService service = (HelloService) ac.getBean("helloService");
        String hello = service.hello("Hello Provider");
        System.out.println(hello);

    }
}

测试

1、启动本地的zk服务

2、启动生产端服务

3、启动消费端服务模拟服务调用

消费端报出的异常信息

由于我们将过滤器配置在生产端了,这时再去观察生产端的控制台,可以看到,调用异常的信息也输出了

使用场景说明

通常来说,在微服务的调用链路比较长的时候,在消费端采用上面的方式进行配置,是有一定意义的,可以较快的定位到调用的服务接口,以及抛出的具体的问题原因,便于服务提供者快速进行问题定位和修复

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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; } } ``` 然后,在服务提供者,可以通过实现Dubbo的ExceptionFilter接口,在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、付费专栏及课程。

余额充值