一、概述简介
1、官网
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
2、简介
Cloud全家桶中有个很重要的组件就是网关,在1.x版本中都是使用zuul网关;
但在2.x版本中,zuul升级一直跳票,SpringCloud最后自己研发了一个网关替代Zuul;
那就是SpringCloud Gateway
Gateway是Spring在生态系统之上构建的API网关服务,基于Spring5,SpringBoot2和Project Reactor等技术。
Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能,例如:熔断、限流、重试等。
总结:
Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架
3、作用
- 反向代理
- 鉴权
- 流量控制
- 熔断
- 日志监控
- 。。。
4、微服务架构中网关在哪里
5、有了Zuul了怎么又出来了gateway
① 我们为什么选择Gateway
1.neflix不太靠谱,zuul2.0一直跳票,迟迟不发布
2.SpringCloud Gateway具有如下特性
3.SpringCloud Gateway与Zuul的区别
② Zuul1.x模型
③ GateWay模型
WebFlux
二、三大核心概念
1、Route(路由)
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
2、Predicate(断言)
参考的是java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
3、Filter(过滤)
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
三、Gateway工作流程
1、官网总结
2、核心逻辑
路由转发+执行过滤器链
四、入门配置
1、新建Module
① cloud-gateway-gateway9527
② POM
<dependencies>
<!--新增gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.radish.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
③ YML
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
uri: http://localhost:8001
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
④ 业务类
⑤ 主启动类
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
SpringApplication.run( GateWayMain9527.class,args);
}
}
⑥ YML新增网关配置
⑦ 测试
五、通过微服务名实现动态路由
1、默认情况下Gateway会根据注册中心的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能
2、一个eureka7001+两个服务提供者8001/8002
3、YML
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway 在微服务中自动为我们创建的负载均衡uri
4、测试
六、Predicate的使用
1、启动gatewat9527
2、Route Predicate Factories
3、常用的Route Predicate
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories
说白了,Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理
4. Cookie Route Predicate
用curl访问
七、Filter的使用
1、简介
路由过滤器可以修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。
SpringCloud Gateway 内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生。
2、Spring Cloud Gateway的Filter
① 生命周期,Only Two
- pre:在业务逻辑之前
- post:在业务逻辑之后
② 种类,Only Two
- GatewayFilter:单一
- GlobalFilter:全局
③ 常用的GatewayFilter
AddRequestParameter
④ 自定义过滤器
两个主要接口介绍:
impiemerts GlobalFilter ,Ordered
能干嘛: 全局日志记录、统一网关鉴权
代码案例
@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(uname)){
log.info("*****用户名为Null 非法用户,(┬_┬)");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);//给人家一个回应
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
测试