一、Gateway
1.概述
SpringCloud Gateway 使用Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。 主要用于:反向代理、鉴权、流量控制、熔断、日志监控
2.特性
(1)动态路由:能够匹配任何请求属性; (2)可以对路由指定 Predicate(断言)和 Filter(过滤器); (3)集成Hystrix的断路器功能; (4)集成 Spring Cloud 服务发现功能; (5)易于编写的 Predicate(断言)和 Filter(过滤器); (6)请求限流功能; (7)支持路径重写。
3.三大核心概念
(1)Route(路由)
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
(2)Predicate(断言)
开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
(3)Filter(过滤)
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
4.网关搭建
(1)依赖jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
(2)配置文件
server:
port: 9527
spring:
application:
name: springcloud-gateway
cloud:
gateway:
routes:
- id: payment_routh
uri: http://localhost:8001
predicates:
- Path=/payment/**
- id: payment_routh2
uri: http://localhost:8001
predicates:
- Path=/payment/lb/**
eureka:
instance:
hostname: springcloud-gateway-service
prefer-ip-address: true
instance-id: gateway
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
(3)启动类
package cpm.zj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GatewayAppication {
public static void main(String[] args) {
SpringApplication.run(GatewayAppication.class,args);
}
}
5.Gateway动态路由
(1)只需修改网关搭建张的配置文件,开启从注册中心动态创建路由功能
server:
port: 9527
spring:
application:
name: springcloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_routh
uri: lb://SPRINGCLOUD-PAYMENT
predicates:
- Path=/payment/query/**
eureka:
instance:
hostname: springcloud-gateway-service
prefer-ip-address: true
instance-id: gateway
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
6.Predicate的使用
- After Route Predicate:在某个时间之后,断言生效
- Before Route Predicate:在某个时间之前,断言生效
- Between Route Predicate:在某个时间段之内,断言生效
- Cookie Route Predicate:需要两个参数,一个是 Cookie name ,一个是正则表达式,判断某个cookie的值是否符合,符合,断言生效,否则,不生效
- Header Route Predicate:一个是属性名称和一个正则表达式,请求头属性值和正则表达式匹配则执行
- Host Route Predicate: 接收一组匹配的域名列表
- Method Route Predicate:判断是GET还是POST请求,进行匹配
- Path Route Predicate:匹配请求路径
- Query Route Predicate:支持传入两个参数,一个是属性名,一个为属性值,属性值可以是正则表达式
7.Filter
(1)简介
路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。
Spring Cloud Gateway 内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生
(2)分类
GatewayFilter :31种 GlobalFilter:10种
(3)自定义过滤器
package cpm.zj.springcloud.filter;
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
public class MyFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("time:"+new Date()+"\t 执行了自定义的全局过滤器: "+"MyLogGateWayFilter"+"hello");
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (uname == 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;
}
}
|