Browse Source

修复feign调用bug

gaodm 5 years ago
parent
commit
44b82aabcf

+ 11 - 5
gateway-service/pom.xml

@@ -37,10 +37,10 @@
             <artifactId>spring-cloud-starter-config</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.springframework.cloud</groupId>
-            <artifactId>spring-cloud-starter</artifactId>
-        </dependency>
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.cloud</groupId>-->
+            <!--<artifactId>spring-cloud-starter</artifactId>-->
+        <!--</dependency>-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
@@ -71,10 +71,16 @@
             <artifactId>springfox-swagger-ui</artifactId>
         </dependency>
         <!-- 开启feign-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.cloud</groupId>-->
+            <!--<artifactId>spring-cloud-starter-openfeign</artifactId>-->
+        <!--</dependency>-->
+
         <dependency>
             <groupId>org.springframework.cloud</groupId>
-            <artifactId>spring-cloud-starter-openfeign</artifactId>
+            <artifactId>spring-cloud-loadbalancer</artifactId>
         </dependency>
+
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

+ 2 - 2
gateway-service/src/main/java/com/diagbot/GatewayServiceApplication.java

@@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfigurati
 import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-import org.springframework.cloud.openfeign.EnableFeignClients;
+//import org.springframework.cloud.openfeign.EnableFeignClients;
 
 /**
  * @Description: 网关启动文件
@@ -19,7 +19,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
         JmxAutoConfiguration.class, ThymeleafAutoConfiguration.class })
 @EnableEurekaClient
-@EnableFeignClients({ "com.diagbot.client" })
+//@EnableFeignClients({ "com.diagbot.client" })
 @RefreshScope
 @ConfigurationPropertiesScan
 public class GatewayServiceApplication {

+ 32 - 32
gateway-service/src/main/java/com/diagbot/client/DiagbotmanServiceClient.java

@@ -1,32 +1,32 @@
-package com.diagbot.client;
-
-import com.diagbot.client.hystrix.DiagbotmanServiceHystrix;
-import com.diagbot.dto.RespDTO;
-import com.diagbot.entity.HasPermissionDTO;
-import com.diagbot.entity.ServiceFilter;
-import com.diagbot.entity.ServiceToken;
-import org.springframework.cloud.openfeign.FeignClient;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-
-import java.util.List;
-
-
-/**
- * @Description: 产品服务客户端
- * @Author: ztg
- * @Date: 2018/9/18 15:19
- */
-@FeignClient(value = "diagbotman-service", fallback = DiagbotmanServiceHystrix.class)
-public interface DiagbotmanServiceClient {
-
-    @PostMapping("/service_filter/getAll")
-    RespDTO<List<ServiceFilter>> getAll();
-
-    @PostMapping("/serviceToken/hasPermission")
-    RespDTO<HasPermissionDTO> hasPermission(@RequestBody ServiceToken st);
-
-}
-
-
-
+//package com.diagbot.client;
+//
+//import com.diagbot.client.hystrix.DiagbotmanServiceHystrix;
+//import com.diagbot.dto.RespDTO;
+//import com.diagbot.entity.HasPermissionDTO;
+//import com.diagbot.entity.ServiceFilter;
+//import com.diagbot.entity.ServiceToken;
+//import org.springframework.cloud.openfeign.FeignClient;
+//import org.springframework.web.bind.annotation.PostMapping;
+//import org.springframework.web.bind.annotation.RequestBody;
+//
+//import java.util.List;
+//
+//
+///**
+// * @Description: 产品服务客户端
+// * @Author: ztg
+// * @Date: 2018/9/18 15:19
+// */
+//@FeignClient(value = "diagbotman-service", fallback = DiagbotmanServiceHystrix.class)
+//public interface DiagbotmanServiceClient {
+//
+//    @PostMapping("/service_filter/getAll")
+//    RespDTO<List<ServiceFilter>> getAll();
+//
+//    @PostMapping("/serviceToken/hasPermission")
+//    RespDTO<HasPermissionDTO> hasPermission(@RequestBody ServiceToken st);
+//
+//}
+//
+//
+//

+ 35 - 35
gateway-service/src/main/java/com/diagbot/client/hystrix/DiagbotmanServiceHystrix.java

@@ -1,35 +1,35 @@
-package com.diagbot.client.hystrix;
-
-
-import com.diagbot.client.DiagbotmanServiceClient;
-import com.diagbot.dto.RespDTO;
-import com.diagbot.entity.HasPermissionDTO;
-import com.diagbot.entity.ServiceFilter;
-import com.diagbot.entity.ServiceToken;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Component;
-import org.springframework.web.bind.annotation.RequestBody;
-
-import java.util.List;
-
-/**
- * @Description: 产品服务客户端(请求失败熔断)
- * @Author: ztg
- * @Date: 2018/9/18 15:19
- */
-@Component
-@Slf4j
-public class DiagbotmanServiceHystrix implements DiagbotmanServiceClient {
-
-    @Override
-    public RespDTO<List<ServiceFilter>> getAll() {
-        log.error("【hystrix】调用{}异常", "getAll");
-        return null;
-    }
-
-    @Override
-    public RespDTO<HasPermissionDTO> hasPermission(@RequestBody ServiceToken st) {
-        log.error("【hystrix】调用{}异常", "hasPermisson");
-        return null;
-    }
-}
+//package com.diagbot.client.hystrix;
+//
+//
+//import com.diagbot.client.DiagbotmanServiceClient;
+//import com.diagbot.dto.RespDTO;
+//import com.diagbot.entity.HasPermissionDTO;
+//import com.diagbot.entity.ServiceFilter;
+//import com.diagbot.entity.ServiceToken;
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.stereotype.Component;
+//import org.springframework.web.bind.annotation.RequestBody;
+//
+//import java.util.List;
+//
+///**
+// * @Description: 产品服务客户端(请求失败熔断)
+// * @Author: ztg
+// * @Date: 2018/9/18 15:19
+// */
+//@Component
+//@Slf4j
+//public class DiagbotmanServiceHystrix implements DiagbotmanServiceClient {
+//
+//    @Override
+//    public RespDTO<List<ServiceFilter>> getAll() {
+//        log.error("【hystrix】调用{}异常", "getAll");
+//        return null;
+//    }
+//
+//    @Override
+//    public RespDTO<HasPermissionDTO> hasPermission(@RequestBody ServiceToken st) {
+//        log.error("【hystrix】调用{}异常", "hasPermisson");
+//        return null;
+//    }
+//}

+ 20 - 0
gateway-service/src/main/java/com/diagbot/config/LbConfigurer.java

@@ -0,0 +1,20 @@
+package com.diagbot.config;
+
+import org.springframework.cloud.client.loadbalancer.LoadBalanced;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2020/3/10 20:04
+ */
+@Configuration
+public class LbConfigurer {
+    @Bean
+    @LoadBalanced
+    RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
+}

+ 22 - 22
gateway-service/src/main/java/com/diagbot/entity/HasPermissionDTO.java

@@ -1,22 +1,22 @@
-package com.diagbot.entity;
-
-import lombok.Getter;
-import lombok.Setter;
-
-import java.io.Serializable;
-
-/**
- * @Description: 是否有权限返回类
- * @Author: ztg
- * @Date: 2018/9/20 10:32
- */
-
-@Getter
-@Setter
-public class HasPermissionDTO implements Serializable {
-
-    private static final long serialVersionUID = 1L;
-
-    private Boolean hasPermission ;//标志
-    private String userId;         //用户ID
-}
+//package com.diagbot.entity;
+//
+//import lombok.Getter;
+//import lombok.Setter;
+//
+//import java.io.Serializable;
+//
+///**
+// * @Description: 是否有权限返回类
+// * @Author: ztg
+// * @Date: 2018/9/20 10:32
+// */
+//
+//@Getter
+//@Setter
+//public class HasPermissionDTO implements Serializable {
+//
+//    private static final long serialVersionUID = 1L;
+//
+//    private Boolean hasPermission ;//标志
+//    private String userId;         //用户ID
+//}

+ 17 - 9
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -1,13 +1,14 @@
 package com.diagbot.filter;
 
-import com.diagbot.client.DiagbotmanServiceClient;
+//import com.diagbot.client.DiagbotmanServiceClient;
+
+import com.diagbot.biz.log.entity.SysLog;
 import com.diagbot.dto.RespDTO;
-import com.diagbot.entity.HasPermissionDTO;
+//import com.diagbot.entity.HasPermissionDTO;
 import com.diagbot.entity.ServiceFilter;
 import com.diagbot.entity.ServiceToken;
 import com.diagbot.enums.SysTypeEnum;
 import com.diagbot.exception.CommonErrorCode;
-import com.diagbot.biz.log.entity.SysLog;
 import com.diagbot.rabbit.MySender;
 import com.diagbot.util.EnDecodeUtil;
 import com.diagbot.util.GsonUtil;
@@ -22,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.RestTemplate;
 import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
@@ -31,6 +33,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
 
@@ -51,10 +54,12 @@ public class GlobalGatewayFilter implements GlobalFilter {
     @Value("${lantone.product}")
     private String lantonePrduct;
 
-    @Autowired
-    DiagbotmanServiceClient diagbotmanServiceClient;
+    //    @Autowired
+    //    DiagbotmanServiceClient diagbotmanServiceClient;
     @Autowired
     private MySender mySender;
+    @Autowired
+    private RestTemplate restTemplate;
 
 
     @Override
@@ -121,7 +126,9 @@ public class GlobalGatewayFilter implements GlobalFilter {
             st.setAppkey(appkey);
             st.setSecret(secret);
             st.setProductId(productId);
-            RespDTO<HasPermissionDTO> res = diagbotmanServiceClient.hasPermission(st);
+            //            RespDTO<HasPermissionDTO> res = diagbotmanServiceClient.hasPermission(st);
+            RespDTO<LinkedHashMap> res
+                    = restTemplate.postForObject("http://diagbotman-service/serviceToken/hasPermission", st, RespDTO.class);
             if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
                 return getVoidMono(serverWebExchange, res);
             }
@@ -137,14 +144,15 @@ public class GlobalGatewayFilter implements GlobalFilter {
             sysLog.setOperation(GsonUtil.toJson(serviceFilter));
             sysLog.setParams(requestUri);
             sysLog.setSysType(SysTypeEnum.APPKEY.getKey());
-            sysLog.setUsername(res.data.getUserId());
+            LinkedHashMap<String, String> map = res.data;
+            sysLog.setUsername(map.get("userId"));
             mySender.outputLogSend(sysLog);
 
         }
 
         ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
-//        builder.header("Authorization", "Authorization Bearer token");
-//        gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
+        //        builder.header("Authorization", "Authorization Bearer token");
+        //        gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
         return gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
     }