在一个公共包里定义一个异常类,继承自RuntimeException。
public class BusinessException extends RuntimeException {
/**
* 异常code
*/
private String code;
public BusinessException() {
super();
}
public BusinessException(String message) {
super(message);
}
public BusinessException(String code, String message) {
super(message);
this.code = code;
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(String code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
public BusinessException(Throwable cause) {
super(cause);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
在服务提供者的Dubbo配置文件中添加下面的内容,主要就是自定义exceptionFilter,然后排出dubbo自带的exceptionFilter
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- http://dubbo.apache.org/schema/dubbo/dubbo.xsd 上面配置为这个一直报错,改为 http://code.alibabatech.com/schema/dubbo/dubbo.xsd -->
<!--用于配置当前应用信息,不管该应用是提供者还是消费者 -->
<dubbo:application name="dubbo-web-provide"/>
<!-- 用于配置连接注册中心相关信息 -->
<dubbo:registry address="zookeeper://localhost:2181" timeout="30000">
<!--配置redis连接参数 -->
<!--具体参数配置见com.alibaba.dubbo.registry.redis.RedisRegistry.class -->
<dubbo:parameter key="max