Kaynağa Gözat

gateway使用Hystrix

gaodm 6 yıl önce
ebeveyn
işleme
cd09264a00

+ 4 - 0
gateway-service/pom.xml

@@ -82,6 +82,10 @@
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-openfeign</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
+        </dependency>
     </dependencies>
 
     <dependencyManagement>

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

@@ -8,6 +8,7 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfigurat
 import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
 
 /**
  * @Description: 网关启动文件
@@ -17,6 +18,7 @@ import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
         JmxAutoConfiguration.class, ThymeleafAutoConfiguration.class})
 @EnableEurekaClient
+@EnableFeignClients
 @RefreshScope
 public class GatewayServiceApplication {
 

+ 3 - 2
gateway-service/src/main/java/com/diagbot/client/UserServiceClient.java

@@ -2,6 +2,7 @@ package com.diagbot.client;
 
 import com.diagbot.client.hystrix.UserServiceHystrix;
 import com.diagbot.dto.RespDTO;
+import com.diagbot.entity.User;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -15,8 +16,8 @@ import org.springframework.web.bind.annotation.RequestParam;
 @FeignClient(value = "user-service",fallback = UserServiceHystrix.class )
 public interface UserServiceClient {
 
-    @PostMapping("/login")
-    RespDTO login(@RequestParam String username , @RequestParam String password);
+    @PostMapping("/user/login")
+    RespDTO<User> login(@RequestParam("username") String username , @RequestParam("password") String password);
 }
 
 

+ 54 - 10
gateway-service/src/main/java/com/diagbot/entity/User.java

@@ -56,7 +56,32 @@ public class User implements Serializable {
     private String username;
 
 
-    public Long getId() {
+    /**
+     * 联系人
+     */
+    private String linkman;
+
+    /**
+     * 邮箱
+     */
+    private String email;
+    
+    /**
+     * 用户类型
+     */
+    private String type;
+
+
+
+    public String getType() {
+		return type;
+	}
+
+	public void setType(String type) {
+		this.type = type;
+	}
+
+	public Long getId() {
         return id;
     }
 
@@ -120,17 +145,36 @@ public class User implements Serializable {
         this.username = username;
     }
 
+    public String getLinkman() {
+        return linkman;
+    }
+
+    public void setLinkman(String linkman) {
+        this.linkman = linkman;
+    }
+
+    public String getEmail() {
+        return email;
+    }
+
+    public void setEmail(String email) {
+        this.email = email;
+    }
+
     @Override
     public String toString() {
         return "User{" +
-        "id=" + id +
-        ", isDeleted=" + isDeleted +
-        ", gmtCreate=" + gmtCreate +
-        ", gmtModified=" + gmtModified +
-        ", creator=" + creator +
-        ", modifier=" + modifier +
-        ", password=" + password +
-        ", username=" + username +
-        "}";
+                "id=" + id +
+                ", isDeleted='" + isDeleted + '\'' +
+                ", gmtCreate=" + gmtCreate +
+                ", gmtModified=" + gmtModified +
+                ", creator='" + creator + '\'' +
+                ", modifier='" + modifier + '\'' +
+                ", password='" + password + '\'' +
+                ", username='" + username + '\'' +
+                ", linkman='" + linkman + '\'' +
+                ", email='" + email + '\'' +
+                ", type=" + type +
+                '}';
     }
 }

+ 8 - 0
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -1,6 +1,10 @@
 package com.diagbot.filter;
 
+import com.diagbot.client.UserServiceClient;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.entity.User;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.gateway.filter.GatewayFilterChain;
 import org.springframework.cloud.gateway.filter.GlobalFilter;
 import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
@@ -23,6 +27,8 @@ import java.util.LinkedHashSet;
 public class GlobalGatewayFilter implements GlobalFilter {
     private static final String GATE_WAY_PREFIX = "/api";
 
+    @Autowired
+    private UserServiceClient userServiceClient;
     @Override
     public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
         log.info("check token and url permission....");
@@ -40,9 +46,11 @@ public class GlobalGatewayFilter implements GlobalFilter {
                 }
             }
         }
+        RespDTO<User> userRespDTO =  userServiceClient.login("admin","a123456");
         log.info("APIURL:{}", requestUri);
         log.info("SERVICENAME:{}", serviceName);
         //
+
         ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
         builder.header("Authorization","Authorization Bearer token");
         gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());