SpringCloud Gateway

cloud笔记第一部分

cloud笔记第二部分Hystrix

SpringCloud Gateway是SpringCloud的一个全新的项目,基于Spring5+SpringBoot2和Project Reactor等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。

为了提升网关性能,Gateway是基于WebFlux实现的,而WebFlux框架是使用了高性能的Reactor模式通信框架Netty

Spring Cloud Gateway的目标是提供一种统一的路由方式且基于Filter的方式实现网关的基本功能,例如:安全,监控/指标,限流。

Zull的工作模式与Gateway的对比

Zuul采用的是Tomcat容器,使用的是传统的Servlet IO处理模型。

这种模式的缺点是:当在并发不高的场景下这种模型是适用的,但是一旦高并发,线程数会大幅度增加,同时线程的创建销毁这个过程消耗的资源又是昂贵的,严重影响请求的处理时间,在一些简单的业务场景下,不希望为每个request分配一个线程,只需要1个或几个线程就能应对极大并发请求,这种业务场景下servlet模型没有优势,所以Zuul1版本是基于servlet上的阻塞式处理模型,因此无法解决这种弊端。

在Servlet3.1之后有了异步非阻塞的支持。而WebFlux是一个典型的非阻塞的异步框架,他的核心是基于Reactor的相关API实现的。相对于传统的Web框架来说,它可以运行在Netty,Undertow以及支持Servlet3.1的容器上。

SpringWebFlux是Spring5引入的新的响应式的框架,区别在于SpringMVC,他不需要依赖Servlet API,是完全的异步非阻塞,并基于Reactor来实现响应式流规范。

Route(路由)

路由是构建网关的基本模块,它是由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由。

Predicate(断言)

开发人员可以匹配HTTP请求中的所有内容,如果请求与断言相匹配则进行路由。

Filter(过滤)

指的是Spring框架GatewayFilter的实例,使用过滤器,可以在请求被路由前或者路由后进行修改。

总结:客户端向Gateway发出请求,然后在Gateway Handle Mapping中找到与请求相匹配的路由,将其发送到gateway web Hnadle。

Handle再通过指定的过滤器链来将请求发送到我们实际的服务之星业务逻辑,然后返回。过滤器可以在服务端接收请求前过滤,也可以在服务端向客户端发送响应后过滤。

请求到达服务端前可以做:参数校验、权限校验、流量控制、日志输出、协议转换等。

在服务端业务处理之后可以对响应的内容。头部进行修改,日志的输出,流量的监控都有重要作用。

GateWay的配置方式

1、在配置文件中直接配置

server:
port: 9527 spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
# uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
# uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/** # 断言,路径相匹配的进行路由 eureka:
instance:
hostname: cloud-gateway-service
client: #服务提供者provider注册进eureka服务列表内
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka

在使用Gateway做路由转发的功能时,需要引入的依赖。

  <dependencies>
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>org.xzq.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>

需要注意的是,不需要引入web和actual,否则会启动失败。

这时候访问http://localhost:8001/payment/get/1001,查询订单,同时也能通过

http://localhost:9527/payment/get/1001访问到1001的订单信息。这就是当访问的URI符合配置文件中设置的predicate,就能通过Gateway转发。

2、编写GatewayConfig配置类

@Configuration
public class GatewayConfig { @Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
// routes_id=route_guonei uri= http://news.baidu.com/guonei ; predicate Path=/guonei
routes.route("route_guonei",r->r.path("/guonei")
.uri("http://news.baidu.com/guonei"))
.build();
return routes.build();
}
}

访问http://localhost:9527/guonei,会被路由到http://news.baidu.com/guonei

通过微服务名实现动态路由

默认情况下,Gateway会根据注册中心注册的服务列表,以注册中心上的微服务名为路径创建动态路由进行转发,从而实现动态路由的功能。

开启从注册中心动态创建路由的功能,利用微服务名进行路由

discovery:
locator:
enabled: true

server:
port: 9527 spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/** # 断言,路径相匹配的进行路由 eureka:
instance:
hostname: cloud-gateway-service
client: #服务提供者provider注册进eureka服务列表内
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka

常用的Predicate属性

官方文档

  • After Route:在predicates下设置时间轴,在这个符合时间轴之后的请求才能路由。类型是ZoneDateTime
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
  • Before Route:匹配指定时间之前的请求,类型是ZoneDateTime。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
  • Between Route:在指定时间间隔内发起的请求才能匹配成功。predicate才是true。
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
  • Cookie Route:请求中需要包含指定的cookie才能匹配成功。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
  • Header Route:请求头参数中要包含指定键值对,否则不匹配。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
  • Host Route:根据主机名进行匹配。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
  • Method Route: 符合指定的请求方法才能匹配成功。
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
  • Path Route: 符合请求路径,路径中可以使用通配符。
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/**
  • Query Route: 包含指定参数才能匹配成功。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
  • Weight Route:根据请求的流量大小转发不同的服务
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2

上面的例子就是将80%的请求流量转发到https://weighthigh.org,将20%的流量转发到https://weightlow.org。

  • Remote Route:匹配IP地址才能路由。
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24

自定义过滤器

自定义过滤器实现GlobalFilterOrdered接口,重写方法。

@Component
@Slf4j
public class MyLogGatewayFilter implements Ordered, GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 自定义过滤器,打印日志,如果请求参数中uname=null或者没有uname属性,不会放行请求 log.info("----- com in MyLogGatewayFilter -----");
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (uname == null) {
log.info("----- uname 不能为空 -----");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
} @Override
public int getOrder() {
return 0;
}
}

如果请求参数没有加uname或者uname=null都不会放行请求。

SpringCloud Gateway快速入门的更多相关文章

  1. API网关才是大势所趋?SpringCloud Gateway保姆级入门教程

    什么是微服务网关 SpringCloud Gateway是Spring全家桶中一个比较新的项目,Spring社区是这么介绍它的: 该项目借助Spring WebFlux的能力,打造了一个API网关.旨 ...

  2. 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)

    [原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...

  3. SpringCloud Gateway入门

    本文是介绍一下SpringCloud Gateway简单路由转发使用. SpringCloud Gateway简介 SpringCloud是基于Spring Framework 5,Project R ...

  4. 万字长文:SpringCloud gateway入门学习&实践

    官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/# ...

  5. OpenStack云计算快速入门之二:OpenStack安装与配置

    原文:http://blog.chinaunix.net/uid-22414998-id-3265685.html OpenStack云计算----快速入门(2) 该教程基于Ubuntu12.04版, ...

  6. 笔记:Spring Cloud Zuul 快速入门

    Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...

  7. Spring Cloud Zuul 快速入门

    Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...

  8. 使用springcloud gateway搭建网关(分流,限流,熔断)

    Spring Cloud Gateway Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 ...

  9. 体验SpringCloud Gateway

    Spring Cloud Gateway是Spring Cloud技术栈中的网关服务,本文实战构建一个SpringCloud环境,并开发一个SpringCloud Gateway应用,快速体验网关服务 ...

随机推荐

  1. FHQ简要笔记

    前言 原文写于 XJ 集训day2 2020.1.19. 现在想想那时候连模板都还没写,只是刚刚理解就在那里瞎yy--之前果然还是太幼稚了. 今天刷训练指南发现全是 Treap 和 Splay ,不想 ...

  2. XJOI contest 1592

    首先 热烈庆祝"CSP-S 2020全国开放赛前冲刺模拟训练题2"圆满结束!!! 感谢大毒瘤颗粒囊的题目.题目还是很不错的,部分分设置的不合理,各种神仙随便 AK ,蒟蒻只能爆零. ...

  3. 谷歌SameSite策略

    当我新下载谷歌80.x版本的时候,访问一个系统(内嵌跳转到其他系统的iframe),跳转过去没有携带cookie,以前版本的谷歌浏览器是可以的,控制台提示SameSite策略,给阻止了cookie携带 ...

  4. Python轻松入门到项目实战-实用教程

    本课程完全基于Python3讲解,针对广大的Python爱好者与同学录制.通过本课程的学习,可以让同学们在学习Python的过程中少走弯路.整个课程以实例教学为核心,通过对大量丰富的经典实例的讲解.让 ...

  5. ⑥SpringCloud 实战:引入gateway组件,开启网关路由功能

    这是SpringCloud实战系列中第4篇文章,了解前面第两篇文章更有助于更好理解本文内容: ①SpringCloud 实战:引入Eureka组件,完善服务治理 ②SpringCloud 实战:引入F ...

  6. mysql 8.0 MGR组复制配置

    一.配置组复制的步骤 1.初始化数据目录 2.配置主服务器(primary) 3.配置辅助服务器(secondaries) 4.启动mysql实例 5.安装组复制插件(primary and seco ...

  7. 为了Java微信支付V3开发包,我找出了微信支付文档至少六个错误

    1. 前言 最近忙的一批,难得今天有喘气的机会就赶紧把最近在开发中的一些成果分享出来.前几日分享了自己写的一个微信支付V3的开发包payment-spring-boot-starter,就忙里偷闲完善 ...

  8. 密码管理平台ratticdb的部署,在centos7上的部署

    一,前言 一直想用ratticdb这个有web界面的密码管理工具,百度了一下居然没有找到中文的部署文档,访问官网也是notfound.找到了官方的部署指南:https://github.com/til ...

  9. Impala的特点

    Impala的特点 0. 原理 基于内存的分析框架. 1.为什么会有Impala? hive进行计算太慢了,于是就有了Impala,Impala可以理解为是hive的内存版本. 2.Impala的优点 ...

  10. 工具-Git与GitHub-GitHub使用(99.5.3)

    @ 目录 1.在github中添加公钥 2.克隆项目 3.在本地工作区新建分支,修改文件并提交 4.推送到远程仓库 5.从远程分支上拉取代码 关于作者 1.在github中添加公钥 首次使用git必须 ...