总结
路由=ID+目标URL+断言+过滤器 路由可以在yaml配置文件中配置也可以在代码中配置 有多重HTTP断言方式 两种过滤器全局过滤器和局部过滤器 路由负载均衡 统一网关日志/鉴权:实现GlobalFilter接口的filter方法
作用
路由=ID+目标URL+断言+过滤器 断言:如果HTTP请求和断言匹配则进行路由 过滤器:在HTTP请求被路由前后对HTTP请求进行处理
功能:
- 动态路由
- 统一网关日志:通过全局过滤器实现
- 统一网关鉴权:通过全局过滤器实现
- 服务发现
- 负载均衡
- 反向代理
- 熔断限流
使用/路由入门
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
路由配置方式1:在yaml文件中配置 application.yaml
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: payment_route
uri: http://localhost:8001
predicates:
- Path=/payment/get/**
- id: payment_route2
uri: http://localhost:8001
predicates:
Path=/payment/lb/**
路由配置方式2:在代码中配置
package com.xcrj.springcloud.configuration;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GateWayConfiguration {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes=routeLocatorBuilder.routes();
routes.route("payment_route ",r->r.path("/payment/get/**").uri("http://localhost:8001")).build();
return routes.build();
}
}
main
package com.xcrj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GatewayMain9527 {
public static void main(String[] args) {
SpringApplication.run(GatewayMain9527.class, args);
}
}
使用/路由负载均衡
application.yaml
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_route
uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/**
- id: payment_route2
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/**
使用/断言
官网所有断言规则
application.yaml
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_route
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/**
- Method=Get
- Header=X-Request-Id,\d+
- Cookie=username,xcrj
- After=2022-05-06T17:52:11.834+08:00[Asia/Shanghai]
- Before=2022-04-06T17:52:11.834+08:00[Asia/Shanghai]
- Between=2022-04-06T17:52:11.834+08:00[Asia/Shanghai],2020-04-06T17:52:11.834+08:00[Asia/Shanghai]
使用/过滤器
官网局部过滤器: GatewayFilter 官网全局过滤器: GlobalFilter
application.yaml
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_route
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/**
filters:
- AddRequestParameter=name,xcrj
使用/统一网关鉴权 全局过滤器
package com.xcrj.springcloud.filter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Date;
@Component
@Slf4j
public class GlobalGatewayAuthFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("进入 GlobalGatewayLogFilter");
String username=exchange.getRequest().getQueryParams().getFirst("username");
if(StringUtils.isBlank(username)){
log.info("非法用户");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
使用/统一网关日志 全局过滤器
同理于统一网关鉴权,使用全局过滤器
对比zuul 1.x和gateway
zuul 1.x
- 基于BIO(阻塞IO),IO请求线程被阻塞直到工作线程执行完成返回结果给IO请求线程
- 不支持长连接
- 工作流程类似nginx,IO请求线程将任务交给工作线程,IO请求线程被阻塞直到工作线程执行完成返回结果给IO请求线程,nginx是用c++编写效率很高
- BIO工作流程举例,我到银行柜台去取钱,提款申请,银行工作人员审核处理,我一直要等着银行工作人员处理完,我才能干其他事情
spring cloud gateway:
- 使用web-flux框架,web-flux框架内部使用reactor-netty容器,web-flux是异步非阻塞的框架
- 基于NIO(非阻塞IO)
- 支持web socket
|