Zuul
Gateway
This project provides a library for building an API Gateway on top of Spring WebFlux. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.
特性:
对比:
网关由路由、断言、过滤器构成:
通过上述组件可以实现:
依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
注意,网关的使用需要 排除 下面两个依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
配置
YML 方式
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh #payment_route #路由的唯一id
uri: http://localhost:8001 #服务的路由地址
predicates:
- Path=/payment/get/** # 服务下接口的地址
#多个路由如下配置
- id: payment_routh2 #payment_route
uri: lb://cloud-payment-service #配置 spring.cloud.gateway.discovery.locator.enabled = true后,可以使用服务名作为服务地址
predicates:
- Path=/payment/lb/**
Configuration 方式
@Configuration
public class GatewayRoutelocatorConfig {
@Bean
RouteLocator routeLocator(RouteLocatorBuilder builder){
RouteLocatorBuilder.Builder routes = builder.routes();
routes.route("n",
r->r.path("/").uri("http://www.gamersky.com/"));
return routes.build();
}
}
由 RoutePredicateFactory 实现
Path 路由断言工厂接收一个参数,根据 Path 定义好的规则来判断访问的 URI 是否匹配。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://c.biancheng.net
predicates:
- Path=/blog/detail/{segment}
如果请求路径为 /blog/detail/xxx,则此路由将匹配。也可以使用正则,例如 /blog/detail/** 来匹配 /blog/detail/ 开头的多级 URI。
path 断言匹配成功后会将断言部分拼接上 uri 作为实际请求路径
即,若断言为 /a,那实际路径一定是 http://…/a
示例
http://localhost:9527/blog/detail/36185 -->
http://c.biancheng.net/blog/detail/36185
Query 路由断言工厂接收两个参数
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://c.biancheng.net
predicates:
- Query=foo, ba.
示例
http://localhost:2001/?foo=baz
Method 路由断言工厂接收一个参数,匹配的是 http 的 method
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://baidu.com
predicates:
- Method=GET
Header 路由断言工厂接收两个参数
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://example.org
predicates:
- Header=X-Request-Id, \d+
cookie 路由断言工厂接收两个参数
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://c.biancheng.net
predicates:
- Cookie=chocolate, ch.p
时间匹配规定路由路由规则生效的时间范围,有三种模式
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://c.biancheng.net
predicates:
- Between=2022-01-20T17:42:47.789-07:00[Asia/Shanghai],2023-01-20T17:42:47.789-07:00[Asia/Shanghai]
由 GatewayFilterFactroies 实现,原理和 servlet 的 filter 类似。
filter 分为两类
加入类扫描,实现默认接口
@Component
@Slf4j
public class TestGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("<<>> start" );
String name = exchange.getRequest().getQueryParams().getFirst("name");
if(StringUtils.isEmpty(name)){
log.info("<<>> no-name" );
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
本文部分内容来自 Spring Cloud Gateway的常用路由断言工厂
传送门:
微服务架构 | 组件目录