diff --git a/book-service/src/main/java/com/test/BookApplication.java b/book-service/src/main/java/com/test/BookApplication.java
index 446bef3..ee58ebd 100644
--- a/book-service/src/main/java/com/test/BookApplication.java
+++ b/book-service/src/main/java/com/test/BookApplication.java
@@ -1,9 +1,7 @@
package com.test;
-import com.apple.eawt.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
diff --git a/book-service/src/main/java/com/test/controller/BookController.java b/book-service/src/main/java/com/test/controller/BookController.java
index 38fa384..f5da79e 100644
--- a/book-service/src/main/java/com/test/controller/BookController.java
+++ b/book-service/src/main/java/com/test/controller/BookController.java
@@ -1,12 +1,13 @@
package com.test.controller;
import com.test.service.BookService;
-import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
import com.test.entity.Book;
/**
@@ -17,14 +18,15 @@ import com.test.entity.Book;
* @Create 2023/8/14 17:11
*/
@RestController
-@EnableEurekaClient
public class BookController {
@Resource
BookService service;
@RequestMapping("/book/{bid}")
- Book findBookById(@PathVariable("bid") int bid){
+ Book findBookById(@PathVariable("bid") int bid,
+ HttpServletRequest request){
+ System.out.println(request.getHeader("Test"));
return service.getBookById(bid);
}
}
diff --git a/borrow-service/src/main/java/com/test/BorrowApplication.java b/borrow-service/src/main/java/com/test/BorrowApplication.java
index 70956cb..67cdf77 100644
--- a/borrow-service/src/main/java/com/test/BorrowApplication.java
+++ b/borrow-service/src/main/java/com/test/BorrowApplication.java
@@ -2,8 +2,6 @@ package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
-import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
diff --git a/borrow-service/src/main/java/com/test/controller/BorrowController.java b/borrow-service/src/main/java/com/test/controller/BorrowController.java
index 7ee9ae1..2f4f300 100644
--- a/borrow-service/src/main/java/com/test/controller/BorrowController.java
+++ b/borrow-service/src/main/java/com/test/controller/BorrowController.java
@@ -1,15 +1,12 @@
package com.test.controller;
-import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.test.entity.UserBorrowDetail;
import com.test.service.BorrowService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
-import java.util.Collections;
/**
diff --git a/borrow-service/src/main/java/com/test/entity/UserBorrowDetail.java b/borrow-service/src/main/java/com/test/entity/UserBorrowDetail.java
index 7454d30..9cc2a03 100644
--- a/borrow-service/src/main/java/com/test/entity/UserBorrowDetail.java
+++ b/borrow-service/src/main/java/com/test/entity/UserBorrowDetail.java
@@ -6,7 +6,7 @@ import lombok.Data;
import java.util.List;
/**
- * ClassName: com.test.entity.UserBorrowDetail
+ * ClassName: entity.com.test.filter.UserBorrowDetail
* Package: PACKAGE_NAME
*
* @author yovinchen
diff --git a/borrow-service/src/main/java/com/test/service/BorrowService.java b/borrow-service/src/main/java/com/test/service/BorrowService.java
index 4561123..162fa36 100644
--- a/borrow-service/src/main/java/com/test/service/BorrowService.java
+++ b/borrow-service/src/main/java/com/test/service/BorrowService.java
@@ -1,6 +1,5 @@
package com.test.service;
-import com.test.entity.User;
import com.test.entity.UserBorrowDetail;
/**
diff --git a/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java b/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java
index 4e5a965..dc590d3 100644
--- a/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java
+++ b/borrow-service/src/main/java/com/test/service/impl/BorrowServiceImpl.java
@@ -10,7 +10,6 @@ import com.test.service.BorrowService;
import com.test.service.client.BookClient;
import com.test.service.client.UserClient;
import org.springframework.stereotype.Service;
-import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
diff --git a/gateway-service/pom.xml b/gateway-service/pom.xml
new file mode 100644
index 0000000..90cce10
--- /dev/null
+++ b/gateway-service/pom.xml
@@ -0,0 +1,30 @@
+
+
+ 4.0.0
+
+ com.example
+ SpringCloudStudy
+ 0.0.1-SNAPSHOT
+
+
+ org.example
+ gateway-service
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-gateway
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-eureka-client
+
+
+
diff --git a/gateway-service/src/main/java/com/test/GatewayApplication.java b/gateway-service/src/main/java/com/test/GatewayApplication.java
new file mode 100644
index 0000000..5361b1b
--- /dev/null
+++ b/gateway-service/src/main/java/com/test/GatewayApplication.java
@@ -0,0 +1,18 @@
+package com.test;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * ClassName: GatewayApplication
+ * Package: com.test
+ *
+ * @author yovinchen
+ * @Create 2023/8/16 08:52
+*/
+@SpringBootApplication
+public class GatewayApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(GatewayApplication.class,args);
+ }
+}
diff --git a/gateway-service/src/main/java/com/test/filter/TestFilter.java b/gateway-service/src/main/java/com/test/filter/TestFilter.java
new file mode 100644
index 0000000..93d3a65
--- /dev/null
+++ b/gateway-service/src/main/java/com/test/filter/TestFilter.java
@@ -0,0 +1,44 @@
+package com.test.filter;
+
+import org.springframework.cloud.gateway.filter.GatewayFilterChain;
+import org.springframework.cloud.gateway.filter.GlobalFilter;
+import org.springframework.core.Ordered;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.stereotype.Component;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+import java.util.List;
+
+/**
+ * ClassName: filterTestFilter
+ * Package: com.test
+ *
+ * @author yovinchen
+ * @Create 2023/8/16 08:51
+ */
+@Component //需要注册为Bean
+public class TestFilter implements GlobalFilter , Ordered {
+ @Override
+ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
+ //先获取ServerHttpRequest对象,注意不是HttpServletRequest
+ ServerHttpRequest request = exchange.getRequest();
+ //打印一下所有的请求参数
+ System.out.println(request.getQueryParams());
+ //判断是否包含test参数,且参数值为1
+ List value = request.getQueryParams().get("test");
+ if(value != null && value.contains("1")) {
+ //将ServerWebExchange向过滤链的下一级传递(跟JavaWeb中介绍的过滤器其实是差不多的)
+ return chain.filter(exchange);
+ }else {
+ //直接在这里不再向下传递,然后返回响应
+ return exchange.getResponse().setComplete();
+ }
+ }
+
+ //设置过滤等级(越小优先级越高)
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+}
diff --git a/gateway-service/src/main/resources/application.yml b/gateway-service/src/main/resources/application.yml
new file mode 100644
index 0000000..4e5c2dd
--- /dev/null
+++ b/gateway-service/src/main/resources/application.yml
@@ -0,0 +1,28 @@
+server:
+ port: 8500
+eureka:
+ instance:
+ prefer-ip-address: true
+ instance-id: ${spring.cloud.client.ip-address}:${server.port}
+ client:
+ service-url:
+ defaultZone: http://localhost:8801/eureka, http://localhost:8802/eureka
+spring:
+ application:
+ name: gateway
+ cloud:
+ gateway:
+ routes:
+ - id: borrow-service
+ uri: lb://borrowservice
+ predicates:
+ - Path=/borrow/**
+ # 继续添加新的路由配置,这里就以书籍管理服务为例
+ # 注意-要对齐routes:
+ - id: book-service
+ uri: lb://bookservice
+ predicates:
+ - Path=/book/**
+ filters: # 添加过滤器
+ - AddRequestHeader=Test, HelloWorld!
+ # AddRequestHeader 就是添加请求头信息,其他工厂请查阅官网
diff --git a/pom.xml b/pom.xml
index 1a9bae5..b13633e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,6 +21,7 @@
commons
eureka-service
hystrix-dashboard
+ gateway-service
1.8
diff --git a/user-service/src/main/java/com/test/UserApplication.java b/user-service/src/main/java/com/test/UserApplication.java
index 4e6cda6..6137d90 100644
--- a/user-service/src/main/java/com/test/UserApplication.java
+++ b/user-service/src/main/java/com/test/UserApplication.java
@@ -2,7 +2,6 @@ package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
diff --git a/user-service/src/main/java/com/test/controller/UserController.java b/user-service/src/main/java/com/test/controller/UserController.java
index 531dca1..f0f57c5 100644
--- a/user-service/src/main/java/com/test/controller/UserController.java
+++ b/user-service/src/main/java/com/test/controller/UserController.java
@@ -4,7 +4,6 @@ import com.test.entity.User;
import com.test.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;