Spring Cloud(六) 服务网关GateWay 入门
前文回顾:
Spring Cloud(一)Eureka Server-单体及集群搭建
Spring Cloud(二) 配置Eureka Client
Spring Cloud(三) 熔断器Hystrix
Spring Cloud(四) API网关Zuul
Spring Cloud(五) Zuul Filter
一.简介
Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。
Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。
二.快速入门
Spring Cloud Gateway 网关路由有两种配置方式:
在配置文件 yml 中配置
通过
@Bean
自定义 RouteLocator,在启动主类 Application 中配置
(1)pom中添加依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version><relativePath/>
</parent>
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
(2)配置文件
server:port: 8080
spring:cloud:gateway:routes:- id: neo_routeuri: https://blog.csdn.netpredicates:- Path=/fy_java1995
各字段含义如下:
id:我们自定义的路由 ID,保持唯一
uri:目标服务地址
predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
filters:过滤规则,本示例暂时没用。
上面这段配置的意思是,配置了一个 id 为 neo_route 的路由规则,当访问地址 http://localhost:8080/fy_java1995
时会自动转发到地址:https://blog.csdn.net/fy_java1995`。
(3)启动类配置
我们可以在启动类 GateWayApplication 中添加方法 customRouteLocator()
来定制转发规则。
@SpringBootApplication
public class GateWayApplication {
public static void main(String[] args) {SpringApplication.run(GateWayApplication.class, args);}
@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder builder) {return builder.routes().route("path_route", r -> r.path("/fy_java1995").uri("https://blog.csdn.net")).build();}
}
三.路由规则
Spring Cloud Gateway 是通过 Spring WebFlux 的 HandlerMapping
做为底层支持来匹配到转发路由,Spring Cloud Gateway 内置了很多 Predicates 工厂,这些 Predicates 工厂通过不同的 HTTP 请求参数来匹配,多个 Predicates 工厂可以组合使用。
Predicate 来源于 Java 8,是 Java 8 中引入的一个函数,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。
在 Spring Cloud Gateway 中 Spring 利用 Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。网上有一张图总结了 Spring Cloud 内置的几种 Predicate 的实现。
(1)通过时间匹配
spring:cloud:gateway:routes:- id: time_routeuri: https://blog.csdn.net/fy_java1995predicates:- After=2018-01-20T06:06:06+08:00[Asia/Shanghai]
Spring 是通过 ZonedDateTime 来对时间进行的对比,ZonedDateTime 是 Java 8 中日期时间功能里,用于表示带时区的日期与时间信息的类,ZonedDateTime 支持通过时区来设置时间,中国的时区是:Asia/Shanghai
。
After Route Predicate 是指在这个时间之后的请求都转发到目标地址。上面的示例是指,请求时间在 2018年1月20日6点6分6秒之后的所有请求都转发到地址https://blog.csdn.net/fy_java1995
。+08:00
是指时间和UTC时间相差八个小时,时间地区为Asia/Shanghai
。
添加完路由规则之后,访问地址http://localhost:8080
会自动转发到https://blog.csdn.net/fy_java1995
。
Before Route Predicate 刚好相反,在某个时间之前的请求的请求都进行转发。我们把上面路由规则中的 After 改为 Before,如下:
spring:cloud:gateway:routes:- id: after_routeuri: https://blog.csdn.net/fy_java1995predicates:- Before=2018-01-20T06:06:06+08:00[Asia/Shanghai]
(2)通过Cookie匹配
spring:cloud:gateway:routes:- id: cookie_routeuri: https://blog.csdn.net/fy_java1995predicates:- Cookie=ityouknow, kee.e
使用 curl 测试,命令行输入:
curl http://localhost:8080 --cookie "ityouknow=kee.e"
(3)通过Header匹配
spring:cloud:gateway:routes:- id: header_routeuri: https://blog.csdn.net/fy_java1995predicates:- Header=X-Request-Id, \d+
使用 curl 测试,命令行输入:
curl http://localhost:8080 -H "X-Request-Id:666666"
(4)通过Method匹配
spring:cloud:gateway:routes:- id: method_routeuri: https://blog.csdn.net/fy_java1995predicates:- Method=GET
使用 curl 测试,命令行输入:
# curl 默认是以 GET 的方式去请求
curl http://localhost:8080
(5)通过Path匹配
spring:cloud:gateway:routes:- id: host_routeuri: https://blog.csdn.net/fy_java1995predicates:- Path=/foo/{segment}
如果请求路径符合要求,则此路由将匹配,例如:/foo/1 或者 /foo/bar。
使用 curl 测试,命令行输入:
curl http://localhost:8080/foo/1
curl http://localhost:8080/foo/xx
curl http://localhost:8080/boo/xx
经过测试第一和第二条命令可以正常获取到页面返回值,最后一个命令报404,证明路由是通过指定路由来匹配。
(6)通过查询参数匹配
spring:cloud:gateway:routes:- id: query_routeuri: https://blog.csdn.net/fy_java1995predicates:- Query=smile
这样配置,只要请求中包含 smile 属性的参数即可匹配路由。
使用 curl 测试,命令行输入:
curl localhost:8080?smile=x
Spring Cloud(六) 服务网关GateWay 入门相关推荐
- Spring Cloud 微服务网关Gateway组件
一.网关介绍 大家都知道在微服务架构中,一个系统会被拆分为多个微服务,那么作为客户端如何去调用这么多的微服务呢?如果没有网关的存在,我们只能在客户端记录每个微服务的地址,然后分别去用. 这样的架构会存 ...
- Spring Cloud微服务网关Zuul过滤链和整合OAuth2+JWT入门实战
一.Spring Cloud Zuul 过滤链 1.1 工作原理 Zuul的核心逻辑是由一系列的Filter来实现的,他们能够在进行HTTP请求或者相应的时候执行相关操作.Zuul Filter的主要 ...
- Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 11.1 Spring Cloud Alibaba Nacos 概述
11.1 Spring Cloud Alibaba Nacos 概述 11.1.1 Spring Cloud Alibaba 简介 在一开始,我们先简单介绍下Spring Cloud Alibaba的 ...
- Spring Cloud微服务之Gateway网关(十三)
Gateway网关 一.网关基本概念 1.API网关介绍 2.Spring Cloud Gateway 3.Spring Cloud Gateway核心概念 4.我们在没有网关下,怎么解决客户端直接与 ...
- Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 10.2 Spring Cloud Sleuth 入门
10.2 Spring Cloud Sleuth 入门 10.2.1 Spring Cloud Sleuth 入门案例 先简单介绍一下案例目标,在本案例中,我们将会使用Feign.RestTempla ...
- Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 1.3 Spring Cloud 与中间件
1.3 Spring Cloud 与中间件 1.3.1 什么是中间件 近年来,越来越多的领域已经离不开计算机.网络技术以及通用技术了.并且随着计算机技术的迅猛发展,更多的软件被要求在很多不同的网络协议 ...
- Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 2.2 Spring Cloud Eureka 进阶
2.2 Spring Cloud Eureka 进阶 上面一节介绍了服务发现以及Eureka的由来,同时展示了Eureka的最基础的搭建以及使用,包括Eureka Server和Eureka Clie ...
- Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 6.5 Spring Cloud Zuul 灰度发布
6.5 Spring Cloud Zuul 灰度发布 6.5.1 灰度发布概述 灰度发布,又有人称金丝雀发布,金丝雀发布这个名称起源于17世纪,17世纪,英国矿井工人发现,金丝雀对瓦斯这种气体十分敏感 ...
- Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 11.3 Nacos 服务配置入门实战
11.3 Nacos 服务配置入门实战 11.3.1 服务配置入门案例 创建子工程ch11_1_config_client 工程依赖pom.xml如下: 代码清单:ch11_1/ch11_1_conf ...
最新文章
- FPGA与ASIC:它们之间的区别以及使用哪一种?
- AI一分钟|许家印抄底FF初见成效,FF91量产在望;苹果新专利:Siri将能识别用户身份...
- 如何防止ISE综合时信号不被优化掉
- 分布式消息队列 — Overview
- 【推荐】8款神奇的动态网页布局及其制作方法
- 前端如何实现音乐盒胶盘的转动_干货来袭!web前端开发工程师必看之如何使用CSS3实现瀑布流效果?...
- 【计算机网络】TCP三次握手
- 关键词组合工具_关键词的优化方法及搜索工具,亚马逊关键词处理
- 100个Python实战项目(十二)Python 并发图像下载器
- 手动卸载金蝶旗舰版KIS软件流程
- 高并发系统设计——API网关技术选型
- chrome最新版总是自动清掉保存的密码
- 百万钱包借款时填资料一直显示服务器异常,百万钱包有连续放款失败,然后突然推过的吗?我已经失败四五...
- POCO C++ 在IOS上的使用
- 【操作系统基础】进程管理(三)进程同步与互斥
- 20个基于 WordPress 搭建的精美网站
- 红外通讯的信号调制及解调电路分析
- 矩阵的负二分之一次方
- SQL*Plus 系统变量之1 - APPI[NFO]
- 【企业工程实践】云服务牛刀小试_DayTwo