Browse Source

update 优化 增加 rpc 异常拦截器

疯狂的狮子Li 1 năm trước cách đây
mục cha
commit
72d3e3caa8

+ 10 - 0
ruoyi-common/ruoyi-common-dubbo/src/main/java/org/dromara/common/dubbo/config/DubboConfiguration.java

@@ -1,6 +1,7 @@
 package org.dromara.common.dubbo.config;
 
 import org.dromara.common.core.factory.YmlPropertySourceFactory;
+import org.dromara.common.dubbo.handler.DubboExceptionHandler;
 import org.dromara.common.dubbo.properties.DubboCustomProperties;
 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -23,4 +24,13 @@ public class DubboConfiguration {
     public BeanFactoryPostProcessor customBeanFactoryPostProcessor() {
         return new CustomBeanFactoryPostProcessor();
     }
+
+    /**
+     * 异常处理器
+     */
+    @Bean
+    public DubboExceptionHandler dubboExceptionHandler() {
+        return new DubboExceptionHandler();
+    }
+
 }

+ 27 - 0
ruoyi-common/ruoyi-common-dubbo/src/main/java/org/dromara/common/dubbo/handler/DubboExceptionHandler.java

@@ -0,0 +1,27 @@
+package org.dromara.common.dubbo.handler;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.dubbo.rpc.RpcException;
+import org.dromara.common.core.domain.R;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * Dubbo异常处理器
+ *
+ * @author Lion Li
+ */
+@Slf4j
+@RestControllerAdvice
+public class DubboExceptionHandler {
+
+    /**
+     * 主键或UNIQUE索引,数据重复异常
+     */
+    @ExceptionHandler(RpcException.class)
+    public R<Void> handleDubboException(RpcException e) {
+        log.error("RPC异常: {}", e.getMessage());
+        return R.fail("RPC异常,请联系管理员确认");
+    }
+
+}