Gateway网关配置完成

This commit is contained in:
2023-08-16 09:02:44 +08:00
parent ea08aaa2a9
commit c1bcb68295
14 changed files with 127 additions and 15 deletions

30
gateway-service/pom.xml Normal file
View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>SpringCloudStudy</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.example</groupId>
<artifactId>gateway-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -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);
}
}

View File

@@ -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<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//先获取ServerHttpRequest对象注意不是HttpServletRequest
ServerHttpRequest request = exchange.getRequest();
//打印一下所有的请求参数
System.out.println(request.getQueryParams());
//判断是否包含test参数且参数值为1
List<String> 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;
}
}

View File

@@ -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 就是添加请求头信息,其他工厂请查阅官网