Browse Source

修复feign调用bug

gaodm 5 years atrás
parent
commit
4410d970e5

+ 23 - 3
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -4,7 +4,6 @@ package com.diagbot.filter;
 
 import com.diagbot.biz.log.entity.SysLog;
 import com.diagbot.dto.RespDTO;
-//import com.diagbot.entity.HasPermissionDTO;
 import com.diagbot.entity.ServiceFilter;
 import com.diagbot.entity.ServiceToken;
 import com.diagbot.enums.SysTypeEnum;
@@ -12,6 +11,7 @@ import com.diagbot.exception.CommonErrorCode;
 import com.diagbot.rabbit.MySender;
 import com.diagbot.util.EnDecodeUtil;
 import com.diagbot.util.GsonUtil;
+import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -23,6 +23,7 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.core.io.buffer.DataBuffer;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Flux;
@@ -37,6 +38,8 @@ import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
 
+//import com.diagbot.entity.HasPermissionDTO;
+
 /**
  * @Description: 全局过滤器
  * @author: gaodm
@@ -127,8 +130,7 @@ public class GlobalGatewayFilter implements GlobalFilter {
             st.setSecret(secret);
             st.setProductId(productId);
             //            RespDTO<HasPermissionDTO> res = diagbotmanServiceClient.hasPermission(st);
-            RespDTO<LinkedHashMap> res
-                    = restTemplate.postForObject("http://diagbotman-service/serviceToken/hasPermission", st, RespDTO.class);
+            RespDTO<LinkedHashMap> res = hasPermission(st);
             if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
                 return getVoidMono(serverWebExchange, res);
             }
@@ -184,4 +186,22 @@ public class GlobalGatewayFilter implements GlobalFilter {
         }
         return serviceFilters;
     }
+
+    public RespDTO<LinkedHashMap> hasPermission(ServiceToken st) {
+        try {
+            RespDTO<LinkedHashMap> res
+                    = restTemplate.postForObject("http://diagbotman-service/serviceToken/hasPermission", st, RespDTO.class);
+            return res;
+        } catch (Exception ex) {
+            return hasPermissionFallback(st);
+        }
+    }
+
+    public RespDTO hasPermissionFallback(ServiceToken st) {
+        log.error("【hystrix】调用{}异常", "hasPermisson");
+        RespDTO respDTO = new RespDTO();
+        respDTO.msg = CommonErrorCode.RPC_ERROR.getMsg();
+        respDTO.code = CommonErrorCode.RPC_ERROR.getCode();
+        return respDTO;
+    }
 }