Gateway
简单介绍
Spring Cloud Gateway是Spring Cloud自己的产物,基于Spring 5 和Spring Boot 2.0 开发,Spring Cloud Gateway的出现是为了代替zuul,在Spring Cloud 高版本中没有对zuul 2.0进行集成,SpringCloud Gateway使用了高性能的Reactor模式通信框架Netty。
Spring Cloud Gateway 的目标,不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流
所以说其实Gateway和zuul 2.0差别不是特别大,都是采用Netty高性能通信框架,性能都挺不错。
Spring Cloud Gataway的特点
在Spring Cloud官方定义了SpringCloud Gateway 的如下特点:
-
基于 Spring 5,Project Reactor , Spring Boot 2.0
-
默认集成 Hystrix 断路器
-
默认集成 Spring Cloud DiscoveryClient
-
Predicates 和 Filters 作用于特定路由,易于编写的 Predicates 和 Filters
-
支持动态路由、限流、路径重写
Spring Cloud Gataway的核心概念
-
Filter(过滤器):
Spring Cloud Gateway的Filter和Zuul的过滤器类似,可以在请求发出前后进行一些业务上的处理 ,这里分为两种类型的Filter,分别是Gateway Filter网工filter和Global Filter全局filter。
-
Route(路由):
网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。说白了就是把url请求路由到对应的资源(服务),或者说是一个请求过来Gateway应该怎么把这个请求转发给下游的微服务,转发给谁。
-
Predicate(断言):
这是一个 Java 8 的 Predicate,可以使用它来匹配来自Http请求的任何内容,例如header或参数。断言的输入类型是一个 ServerWebExchange。简单理解就是处理HTTP请求的匹配规则,在什么样的请情况下才能命中资源继续访问。
使用gateway
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--不能到web包,gateway底层是webflux,会冲突 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
注意:不能到web包,gateway底层是webflux,会冲突
编写启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
yml文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10010/eureka/
instance:
instance-id: server-gateway
spring:
application:
name: server-gateway
cloud:
gateway:
discovery:
locator:
enabled: false #开放服务名访问方式
lower-case-service-id: true #服务名小写
routes:
- id: application-user #指定服务名
uri: lb://user-server #去注册中心找这个服务名
predicates: #断言,匹配访问的路径,如果访问路径存在 /user 则会请求路由给 uri: lb://user-server
- Path=/user/** #服务访问路径
filters:
- StripPrefix=1 #请求转发的时候会去掉 - Path的第一个参数路径,这里也就是 /user,也可以改成2,就去掉两层
server:
port: 10060
- Path=/user/** 就是在 localhost:10010 后面加一个 /user 后面再跟要访问的接口的 路径
最后启动即可,但要先启动需要的接口的server和Eureka
predicates 断言
什么是断言工程,在Spring Cloud Gateway官方文档有如下解释:
Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础架构的一部分进行匹配。Spring Cloud Gateway包括许多内置的路由断言工厂。所有这些断言都与HTTP请求的不同属性匹配。您可以将多个路由断言工厂与逻辑and语句结合使用。
这里不难理解,其实断言工厂就是用来判断http请求的匹配方式。比如我们再上面案例中配置的:“Path=/user/**
” ,就是使用的是 “Path Route Predicate Factory
” 路径匹配工厂,意思是http请求的资源地址必须是 /user 才会被匹配到对应的路由,然后继续执行对应的服务获取资源。
在Spring Cloud Gateway中,针对不同的场景内置的路由断言工厂,比如
-
Query Route Predicate Factory
:根据查询参数来做路由匹配 -
RemoteAddr Route Predicate Factory
:根据ip来做路由匹配 -
Header Route Predicate Factory
:根据请求头中的参数来路由匹配 -
Host Route Predicate Factory
:根据主机名来进行路由匹配 -
Method Route Predicate Factory
:根据方法来路由匹配 -
Cookie Route Predicate Factory
:根据cookie中的属性值来匹配 -
Before Route Predicate Factory
:指定时间之间才能匹配 -
After Route Predicate Factory
: 指定时间之前才能匹配 -
Weight Route Predicate Factory
: 根据权重把流量分发到不同的主机
如秒杀的时间段的实现
在指定时间段之间断言-Between Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
请求时间在两个时间之内者允许访问
Gateway 的 Filter 过滤器
上面讲了Gateway有两种过滤器GatewayFilter和GlobalFilter,GatewayFilter是针对某一个请求的过滤,GlobalFilter是针对全部的请求
自定义GatewayFilter
写一个类实现GatewayFilter,和Ordered
public class RequestTimeFilter implements GatewayFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 开始时间
long startTime = new Date().getTime();
System.out.println("-------------------------------");
// 链式执行其他的filter
return chain.filter(exchange).then(
Mono.fromRunnable(() -> {
// 结束时间
long endTime = new Date().getTime();
long useTime = endTime - startTime;
// 获取路径
String path = exchange.getRequest().getURI().getPath();
System.out.println("请求"+ path + "所耗时间为:" + useTime);
})
);
}
// 执行顺序,越小越先执行
@Override
public int getOrder() {
return 0;
}
}
这里使用Chain链式执行下一个filter,下一个也有filter,GatewayFilter需要自己写config类,不需要交给spring管理
@Configuration
public class FilterConfig {
//配置Filter作用于那个访问规则上
@Bean
public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
return builder.routes().route(r -> r.path("/services/user2/**")
//去掉2个前缀
.filters(f -> f.stripPrefix(2)
.filter(new RequestTimeFilter())
.addResponseHeader("X-Response-test", "test"))
.uri("lb://server-user")
.order(0)
.id("test-RequestTimeFilter")
).build();
}
}
这里的配置和application文件差不多
自定义GlobalFilter
@Component
public class LoginGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 获取请求和响应
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
// 获取请求头的token,返回的是个list
List<String> token = request.getHeaders().get("token");
// 请求头为空
if(token == null || token.size() == 0){
// 因为是gateway内置的request和response,所以需要按他的方式来响应
DataBuffer buffer = null;
try {
byte[] bytes ="请先登录".getBytes("utf-8");
buffer = (DataBuffer) response.bufferFactory().wrap(bytes);
response.setStatusCode(HttpStatus.UNAUTHORIZED); // 响应码401
response.getHeaders().add("Content-Type","application/json;charset=UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
return response.writeWith(Mono.just(buffer));
}
// 请求头不为空,继续执行其他的filter
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
GlobalFilter交给spring管理,不需要写配置类
Gateway跨域配置
yml文件
spring:
cloud:
globalcors: #跨域配置
cors-configurations:
'[/**]':
allowedOrigins: "https://docs.spring.io" #允许的站点
allowedMethods: #允许的请求方式
- GET
- POST
- DELETE
- PUT
- HEAD
- CONNECT
- TRACE
- OPTIONS
allowHeaders: #允许的请求头
- Content-Type
Gateway超时
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000
response-timeout: 5s
指定路由超时配置(很少用):
spring:
cloud:
gateway:
routes:
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200
connect-timeout: 200