gateway官网地址 : https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/
Gateway 工作流程
- 客户端向 Spring Cloud Gateway 发出请求, 然后在Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler
- Handler 再通过制定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回
过滤器之间采用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或者之后 (“post”)执行业务逻辑 - Filter 在
“pre”
类型的过滤器可以做参数校验,权限校验、流量监控、日志输出、协议转换
等。 - 在
“post”
类型的过滤器中可以做响应内容、响应头的修改、日志的输出,流量监控
等有着非常重要的作用。
基础示例
1、模块名 cloud-gateway-gateway9527
2、pml.xml
<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>
3、yml 配置文件
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh # http://127.0.0.1:8001
uri: http://127.0.0.1:8001 #配置后提供的路由地址
predicates:
- Path=/payment/get/** # 断言
- id: payment_routh2
uri: http://127.0.0.1:8001
predicates:
- Path=/payment/lb/**
eureka:
instance:
hostname: cloud-gateway-service
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
4、主启动
@SpringBootApplication
@EnableEurekaClient
public class GatewayMain9527 {
public static void main(String[] args){
SpringApplication.run(GatewayMain9527.class,args) ;
}
}
5、测试
通过网关访问 : http://localhost:9527/payment/get/1
Gateway 网关路由有两种配置方式
- 配置文件 如上
- 编码 :
@Configuration
public class GateWayConfig {
@Bean // 一条规则配一个bean
public RouteLocator customRouterLocator(RouteLocatorBuilder routeLocatorBuilder) {
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_route_aqiang9",
r -> r.path("/guonei").uri("https://news.baidu.com/guonei"));
return routes.build() ;
}
}
访问: http://localhost:9527/guonei (会定向
百度新闻)
通过微服务名实现动态路由
默认情况下 Gateway 会根据注册中心
的服务列表
以注册中心上服务名为路径
动态路由进行转发,从而实现动态路由
的功能
配置
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh
uri: lb://cloud-payment-service #配置后提供的路由地址
predicates:
- Path=/payment/get/**
- id: payment_routh2
uri: lb://cloud-payment-service #配置后提供的路由地址
predicates:
- Path=/payment/lb/**
断言(Predicate 的使用)
Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础架构的一部分进行匹配。Spring Cloud Gateway包括许多内置的路由谓词工厂。所有这些谓词都与HTTP请求的不同属性匹配。您可以将多个路由谓词工厂与逻辑and语句结合使用。
Spring Cloud Gateway 创建 Route 对象时,使用 RoutePredicateFactory
创建 Predicate 对象,Predicate 对象可以赋值给Route Predicate Factories.
断言类型 : https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/#gateway-request-predicates-factories
过滤(Filter)
官网过滤器:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/#gatewayfilter-factories
是什么
路由过滤器可用于修改进入HTTP请求和返回 HTTP 相应, 路由过滤器只能指定路由进行使用。
Spring Cloud Gateway 内置了很多路由过滤器, 他们都由 GatewayFilter
的工厂类来产生。
常用的 GatewayFilter
GatewayFilter
、GlobalFilter
自定义过滤器
两个接口 : implements GlobalFilter, Ordered
能做什么 : 全局日志记录 、 统一鉴权网关
@Component
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("come in my log filter ...."+ LocalDate.now() );
String username = exchange.getRequest().getQueryParams().getFirst("username");
if(username == null ){
System.out.println("用户名为 null 非法用户");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE) ;
return exchange.getResponse().setComplete() ;
}
return chain.filter(exchange) ;
}
@Override
public int getOrder() {
// 配置优先级
return 0;
}
}