目录
1:什么是GateWay
Spring Cloud Gateway 是 Spring Cloud 新推出的网关框架,之前是 Netflix Zuul1,Gateway比 Zuul 2 更早的使用 Netty 实现异步 IO,从而实现了一个简单、比 Zuul 1.x 更高效的、与 Spring Cloud 紧密配合的 API 网关。
参考spring官网 Spring Cloud
警告:Spring Cloud Gateway需要Spring Boot和Spring Webflux提供的Netty运行时。它不能在传统的Servlet容器中(比如tomcat)工作,也不能作为WAR构建。
2:网关的作用
在微服务之前对访问进行路径管理,屏蔽原始地址,统一访问网关地址,网关再根据配置的过滤条件决定是否访问地址。业务在网关中统一进行权限、安全、路由等管理。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到API,并为它们提供跨领域的关注点,如:安全性、监控/度量和弹性。
1:对服务进行统一的策略管理
2:所以的api服务统一接入点
3:实现安全、验证、路由、过滤等策略
3:GateWay特点
pom导入,屏蔽web的jar
<dependencies>
<!--引入gateway依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--Spring web 依赖-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--</dependency>-->
<!--Springcloud的hystrix组件 服务降级 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--引入openfeignjar-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--Springcloud的eureka组件 client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--图形监控-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-actuator</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
客户端向 Spring Cloud Gateway 发出请求。如果 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。
3.1:路由功能
路由网关的基本构建块。它由ID,目标URI,谓词集合和过滤器集合定义。如果聚合谓词为真,则匹配路由
3.2:断言
断言只能设置已知的规则,不能修改http请求和响应的规则。
这是一个 Java 8 的 Predicate。主要是匹配规则,用来匹配http的请求内容,到访问的具体路径,也可以通过参数、cookie、日期等等来匹配。规则很多
断言的配置在下图的predicate中,geteWay的noacs配置断言如下
#getawy端口
server:
port: 9000
#getWay的nocas服务注册配置和路由规则配置
spring:
application:
name: getWay9000
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8851,127.0.0.1:8853,127.0.0.1:8855
gateway:
#全局跨域
# globalcors:
# cors-configurations:
# '[/**]':
# allowedOrigins: "*"
# allowedMethods:
# - GET
# - POST
routes:
#Between条件 在指定时间之间 路由到目标地址
# http://localhost:9000/selectAll 可以访问http://localhost:8001/selectAll
- id: Between_route
uri: http://localhost:8002/
predicates:
- Between=2017-01-20T17:42:47.789-07:00, 2025-01-21T17:42:47.789-07:00
geteWay的noacs配置断言效果
将原始服务的地址: http://localhost:8002/selectAll
替换为网关的服务路由地址: http://localhost:9000/selectAll
3.3:过滤器
过滤器的功能是修改http请求或者响应的值。他的配置文件里边有filter属性
Gateway提供很多过滤器,包括全局过滤器、指定过滤器、自定义过滤器(自己编写代码,重点学习)
1、按执行顺序分
pre过滤器(前置过滤器):转发之前执行。参数校验、权限校验、流量监控、日志输出、协议转换等
post过滤器(后置过滤器):转发之后执行。响应内容、响应头的修改,日志的输出,流量监控等
2、按作用范围分
global filter:所有路由
gateway filter:指定的路由
3、AddRequestHeaderGatewayFilterFactory:在匹配的请求上添加请求头的值,如示例中,添加请求头:X-Request-Foo;值:bar
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
filters:
# 添加请求头 X-Request-Foo:Bar
- AddRequestParameter=X-Request-Foo, Bar
4、AddRequestParameterGatewayFilterFactory:在匹配的请求上添加请求参数
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
filters:
# 添加请求参数 foo=bar; 在query串中添加
- AddRequestParameter=foo, bar
5、AddResponseHeaderGatewayFilterFactory:在匹配的请求上添加响应头
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
filters:
# 添加响应头 X-Response-Foo:Bar
- AddResponseHeader=X-Response-Foo, Bar
6:自定义全局过滤器
全局过滤器是应用于所有的路由策略上的,不许要配置声明,写了符合条件就会生效。
/**
* //全局过滤器,实现GlobalFilter接口,和Ordered接口即可。
* @author :huyiju
* @date :2020-04-29 22:55
*/
@Component
public class MyFilter1 implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("全局过滤器");
ServerHttpRequest request= exchange.getRequest();
//获取token
String token= request.getQueryParams().getFirst("token");
if (token==null){
ServerHttpResponse response = exchange.getResponse();
//303状态码表示由于请求对应的资源存在着另一个URI,应使用GET方法定向获取请求的资源
response.setStatusCode(HttpStatus.BAD_REQUEST);
return response.setComplete();
}
System.out.println("aa:"+token);
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}