Explorar o código

修改Feign调用不进入熔断,直接抛出异常

yangzhijie %!s(int64=5) %!d(string=hai) anos
pai
achega
d076fcc104

+ 40 - 0
src/main/java/com/leanwo/gateway/exception/KeepErrMsgConfiguration.java

@@ -0,0 +1,40 @@
+package com.leanwo.gateway.exception;
+
+import java.io.Reader;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import com.netflix.hystrix.exception.HystrixBadRequestException;
+
+import feign.Response;
+import feign.codec.ErrorDecoder;
+
+/**
+ * 不进入熔断,直接抛出异常
+ * @author YangZhiJie
+ * @date 2020/08/03
+ */
+@Configuration
+public class KeepErrMsgConfiguration {
+    
+    @Bean
+    public ErrorDecoder errorDecoder() {
+        return new UserErrorDecoder();
+    }
+
+    /**
+     * 自定义错误
+     */
+    public class UserErrorDecoder implements ErrorDecoder {
+        private Logger logger = LoggerFactory.getLogger(getClass());
+
+        @Override
+        public Exception decode(String methodKey, Response response) {
+            String errorMessage = response.body().toString();
+            return new HystrixBadRequestException(errorMessage);
+        }
+    }
+}

+ 2 - 1
src/main/java/com/leanwo/gateway/service/ProdogServer.java

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
 import com.leanwo.gateway.dto.AccountManagementDto;
+import com.leanwo.gateway.exception.KeepErrMsgConfiguration;
 
 
 /**
@@ -19,7 +20,7 @@ import com.leanwo.gateway.dto.AccountManagementDto;
  */
 
 @Service
-@FeignClient(value = "prodog-server", fallback = ProdogServerImpl.class)
+@FeignClient(value = "prodog-server", fallback = ProdogServerImpl.class, configuration = { KeepErrMsgConfiguration.class })
 public interface ProdogServer {
 	
 	/**