javascript
Spring Cloud(六) 服务网关GateWay 入门
前文回顧:
Spring Cloud(一)Eureka Server-單體及集群搭建
Spring Cloud(二) 配置Eureka Client
Spring Cloud(三) 熔斷器Hystrix
Spring Cloud(四) API網(wǎng)關(guān)Zuul
Spring Cloud(五) Zuul Filter
一.簡(jiǎn)介
Spring Cloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,該項(xiàng)目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術(shù)開(kāi)發(fā)的網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的統(tǒng)一的 API 路由管理方式。
Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系統(tǒng)中的網(wǎng)關(guān),目標(biāo)是替代 Netflix Zuul,其不僅提供統(tǒng)一的路由方式,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全,監(jiān)控/指標(biāo),和限流。
二.快速入門(mén)
Spring Cloud Gateway 網(wǎng)關(guān)路由有兩種配置方式:
-
在配置文件 yml 中配置
-
通過(guò)@Bean自定義 RouteLocator,在啟動(dòng)主類(lèi) Application 中配置
(1)pom中添加依賴(lài)
<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:目標(biāo)服務(wù)地址
-
predicates:路由條件,Predicate 接受一個(gè)輸入?yún)?shù),返回一個(gè)布爾值結(jié)果。該接口包含多種默認(rèn)方法來(lái)將 Predicate 組合成其他復(fù)雜的邏輯(比如:與,或,非)。
-
filters:過(guò)濾規(guī)則,本示例暫時(shí)沒(méi)用。
上面這段配置的意思是,配置了一個(gè) id 為 neo_route 的路由規(guī)則,當(dāng)訪(fǎng)問(wèn)地址 http://localhost:8080/fy_java1995時(shí)會(huì)自動(dòng)轉(zhuǎn)發(fā)到地址:https://blog.csdn.net/fy_java1995`。
(3)啟動(dòng)類(lèi)配置
我們可以在啟動(dòng)類(lèi) GateWayApplication 中添加方法 customRouteLocator() 來(lái)定制轉(zhuǎn)發(fā)規(guī)則。
@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();} ? }三.路由規(guī)則
Spring Cloud Gateway 是通過(guò) Spring WebFlux 的 HandlerMapping 做為底層支持來(lái)匹配到轉(zhuǎn)發(fā)路由,Spring Cloud Gateway 內(nèi)置了很多 Predicates 工廠(chǎng),這些 Predicates 工廠(chǎng)通過(guò)不同的 HTTP 請(qǐng)求參數(shù)來(lái)匹配,多個(gè) Predicates 工廠(chǎng)可以組合使用。
Predicate 來(lái)源于 Java 8,是 Java 8 中引入的一個(gè)函數(shù),Predicate 接受一個(gè)輸入?yún)?shù),返回一個(gè)布爾值結(jié)果。該接口包含多種默認(rèn)方法來(lái)將 Predicate 組合成其他復(fù)雜的邏輯(比如:與,或,非)。可以用于接口請(qǐng)求參數(shù)校驗(yàn)、判斷新老數(shù)據(jù)是否有變化需要進(jìn)行更新操作。
在 Spring Cloud Gateway 中 Spring 利用 Predicate 的特性實(shí)現(xiàn)了各種路由匹配規(guī)則,有通過(guò) Header、請(qǐng)求參數(shù)等不同的條件來(lái)進(jìn)行作為條件匹配到對(duì)應(yīng)的路由。網(wǎng)上有一張圖總結(jié)了 Spring Cloud 內(nèi)置的幾種 Predicate 的實(shí)現(xiàn)。
(1)通過(guò)時(shí)間匹配
spring:cloud:gateway:routes:- id: time_routeuri: https://blog.csdn.net/fy_java1995predicates:- After=2018-01-20T06:06:06+08:00[Asia/Shanghai]Spring 是通過(guò) ZonedDateTime 來(lái)對(duì)時(shí)間進(jìn)行的對(duì)比,ZonedDateTime 是 Java 8 中日期時(shí)間功能里,用于表示帶時(shí)區(qū)的日期與時(shí)間信息的類(lèi),ZonedDateTime 支持通過(guò)時(shí)區(qū)來(lái)設(shè)置時(shí)間,中國(guó)的時(shí)區(qū)是:Asia/Shanghai。
After Route Predicate 是指在這個(gè)時(shí)間之后的請(qǐng)求都轉(zhuǎn)發(fā)到目標(biāo)地址。上面的示例是指,請(qǐng)求時(shí)間在 2018年1月20日6點(diǎn)6分6秒之后的所有請(qǐng)求都轉(zhuǎn)發(fā)到地址https://blog.csdn.net/fy_java1995。+08:00是指時(shí)間和UTC時(shí)間相差八個(gè)小時(shí),時(shí)間地區(qū)為Asia/Shanghai。
添加完路由規(guī)則之后,訪(fǎng)問(wèn)地址http://localhost:8080會(huì)自動(dòng)轉(zhuǎn)發(fā)到https://blog.csdn.net/fy_java1995。
Before Route Predicate 剛好相反,在某個(gè)時(shí)間之前的請(qǐng)求的請(qǐng)求都進(jìn)行轉(zhuǎn)發(fā)。我們把上面路由規(guī)則中的 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)通過(guò)Cookie匹配
spring:cloud:gateway:routes:- id: cookie_routeuri: https://blog.csdn.net/fy_java1995predicates:- Cookie=ityouknow, kee.e使用 curl 測(cè)試,命令行輸入:
curl http://localhost:8080 --cookie "ityouknow=kee.e"(3)通過(guò)Header匹配
spring:cloud:gateway:routes:- id: header_routeuri: https://blog.csdn.net/fy_java1995predicates:- Header=X-Request-Id, \d+使用 curl 測(cè)試,命令行輸入:
curl http://localhost:8080 ?-H "X-Request-Id:666666"(4)通過(guò)Method匹配
spring:cloud:gateway:routes:- id: method_routeuri: https://blog.csdn.net/fy_java1995predicates:- Method=GET 使用 curl 測(cè)試,命令行輸入: # curl 默認(rèn)是以 GET 的方式去請(qǐng)求 curl http://localhost:8080(5)通過(guò)Path匹配
spring:cloud:gateway:routes:- id: host_routeuri: https://blog.csdn.net/fy_java1995predicates:- Path=/foo/{segment}如果請(qǐng)求路徑符合要求,則此路由將匹配,例如:/foo/1 或者 /foo/bar。
使用 curl 測(cè)試,命令行輸入:
curl http://localhost:8080/foo/1 curl http://localhost:8080/foo/xx curl http://localhost:8080/boo/xx經(jīng)過(guò)測(cè)試第一和第二條命令可以正常獲取到頁(yè)面返回值,最后一個(gè)命令報(bào)404,證明路由是通過(guò)指定路由來(lái)匹配。
(6)通過(guò)查詢(xún)參數(shù)匹配
spring:cloud:gateway:routes:- id: query_routeuri: https://blog.csdn.net/fy_java1995predicates:- Query=smile這樣配置,只要請(qǐng)求中包含 smile 屬性的參數(shù)即可匹配路由。
使用 curl 測(cè)試,命令行輸入:
curl localhost:8080?smile=x總結(jié)
以上是生活随笔為你收集整理的Spring Cloud(六) 服务网关GateWay 入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring Cloud(五) Zuul
- 下一篇: Spring Cloud(七) Gate