简介:
提供了一个构建在Spring生态系统之上的API网关,包括:Spring 5、Spring Boot 2和project Reactor。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到api,并为它们提供跨领域的关注,例如:安全性、监视/度量和弹性。
如何工作:
客户端向Spring Cloud Gateway发出请求。如果网关处理程序映射确定了一个请求与一个路由匹配,那么它将被发送到网关Web处理程序。此处理程序通过特定于该请求的筛选器链运行该请求。过滤器被虚线分割的原因是过滤器可以在发送代理请求之前和之后运行逻辑。所有的“预”过滤器逻辑被执行。然后发出代理请求。发出代理请求后,将运行“post”筛选逻辑。
特点:
Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架
作用:反向代理、鉴权、流量控制、熔断、日志监控等等
位置:
消费者直接调用(没有负载均衡)或者通过负载均衡来对网关进行访问
Route(路由):
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
Predicate(断言):
参考的是java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
Filter(过滤):
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
核心逻辑:路由转发+执行过滤器链
使用:
通过微服务名实现路由
(默认情况下Gateway会根据注册中心的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能)
POM:
<!--新增gateway-->
<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>
YML:
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
# routes:
# - id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:8001 #匹配后提供服务的路由地址
# predicates:
# - Path=/payment/** #断言,路径相匹配的进行路由
#
# - id: payment_routh2
# uri: http://localhost:8001
# predicates:
# - Path=/payment/lb/** #断言,路径相匹配的进行路由
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://CLOUD-ORDER-SERVICE
predicates:
- Path=/payment/** #断言,路径相匹配的进行路由
# 时间限制
# - After=2020-08-22T20:17:58.974+08:00[Asia/Shanghai]
# - Cookie限制
# - Cookie=username,ZhangXinbin
# - Header限制
# - Header=X-Request-Id,\d+
- id: payment_routh2
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://CLOUD-ORDER-SERVICE
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
主启动类:
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
SpringApplication.run( GateWayMain9527.class,args);
}
}
常用的Route Predicate
- 4.1. The After Route Predicate Factory
- 4.2. The Before Route Predicate Factory
- 4.3. The Between Route Predicate Factory
- 4.4. The Cookie Route Predicate Factory
- 4.5. The Header Route Predicate Factory
- 4.6. The Host Route Predicate Factory
- 4.7. The Method Route Predicate Factory
- 4.8. The Path Route Predicate Factory
- 4.9. The Query Route Predicate Factory
- 4.10. The RemoteAddr Route Predicate Factory
-
4.11. The Weight Route Predicate Factory
实现一组匹配规则,让请求过来找到对应的Route进行处理
Spring Cloud Gateway的Filter
单一:
- 5.1. The AddRequestHeader GatewayFilter Factory
- 5.2. The AddRequestParameter GatewayFilter Factory
- 5.3. The AddResponseHeader GatewayFilter Factory
- 5.4. The DedupeResponseHeader GatewayFilter Factory
- 5.5. The Hystrix GatewayFilter Factory
- 5.6. Spring Cloud CircuitBreaker GatewayFilter Factory
- 5.7. The FallbackHeaders GatewayFilter Factory
- 5.8. The MapRequestHeader GatewayFilter Factory
- 5.9. The PrefixPath GatewayFilter Factory
- 5.10. The PreserveHostHeader GatewayFilter Factory
- 5.11. The RequestRateLimiter GatewayFilter Factory
- 5.12. The RedirectTo GatewayFilter Factory
- 5.13. The RemoveHopByHopHeadersFilter GatewayFilter Factory
- 5.14. The RemoveRequestHeader GatewayFilter Factory
- 5.15. RemoveResponseHeader GatewayFilter Factory
- 5.16. The RemoveRequestParameter GatewayFilter Factory
- 5.17. The RewritePath GatewayFilter Factory
- 5.18. RewriteLocationResponseHeader GatewayFilter Factory
- 5.19. The RewriteResponseHeader GatewayFilter Factory
- 5.20. The SaveSession GatewayFilter Factory
- 5.21. The SecureHeaders GatewayFilter Factory
- 5.22. The SetPath GatewayFilter Factory
- 5.23. The SetRequestHeader GatewayFilter Factory
- 5.24. The SetResponseHeader GatewayFilter Factory
- 5.25. The SetStatus GatewayFilter Factory
- 5.26. The StripPrefix GatewayFilter Factory
- 5.27. The Retry GatewayFilter Factory
- 5.28. The RequestSize GatewayFilter Factory
- 5.29. Modify a Request Body GatewayFilter Factory
- 5.30. Modify a Response Body GatewayFilter Factory
- 5.31. Default Filters
Global Filter
6. Global Filters
- 6.1. Combined Global Filter and GatewayFilter Ordering
- 6.2. Forward Routing Filter
- 6.3. The LoadBalancerClient Filter
- 6.4. The ReactiveLoadBalancerClientFilter
- 6.5. The Netty Routing Filter
- 6.6. The Netty Write Response Filter
- 6.7. The RouteToRequestUrl Filter
- 6.8. The Websocket Routing Filter
- 6.9. The Gateway Metrics Filter
- 6.10. Marking An Exchange As Routed
常用的GatewayFilter
AddRequestParameter
自定义过滤器
自定义全局GlobalFilter
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter,Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("*********come in MyLogGateWayFilter: "+new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("username");
if(StringUtils.isEmpty(username)){
log.info("*****用户名为Null 非法用户,(┬_┬)");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);//给人家一个回应
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}