共計 9080 個字符,預計需要花費 23 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 SpringCloud Gateway 怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Spring Cloud Gateway 是 Spring Cloud 技術棧中的網關服務,本文實戰構建一個 SpringCloud 環境,并開發一個 SpringCloud Gateway 應用,快速體驗網關服務;
環境信息
操作系統:win10(64 位)
JDK:1.8.0_181
Maven:3.5.0
Spring Cloud:Greenwich.SR
源碼下載
如果您不打算寫代碼,也可以從 GitHub 上下載本次實戰的源碼,地址和鏈接信息如下表所示:
名稱鏈接備注項目主頁 https://github.com/zq2599/blog_demos 該項目在 GitHub 上的主頁 git 倉庫地址 (https)https://github.com/zq2599/blog_demos.git 該項目源碼的倉庫地址,https 協議 git 倉庫地址 (ssh)git@github.com:zq2599/blog_demos.git 該項目源碼的倉庫地址,ssh 協議
/br
這個 git 項目中有多個文件夾,本章的源碼在 gatewaydemo 文件夾下,如下圖紅框所示:
整體設計
本次實戰的源碼涉及到三個應用:注冊中心、服務提供者、網關,它們的關系和業務邏輯如下圖:整個工程基于 maven 構建,采用父子結構,父工程名為 gatewaydemo,里面有三個 modular,分別是:eureka()注冊中心)、provider(服務提供者)、網關(gateway),在 IDEA 上呈現的結構如下圖所示:準備完畢,開始編碼吧;
創建父工程
創建名為 gatewaydemo 的 maven 工程,pom.xml 內容如下,這是個典型的父子工程 pom,dependencyManagement 節點接管了版本匹配:
?xml version= 1.0 encoding= UTF-8 ?
project xmlns= http://maven.apache.org/POM/4.0.0
xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation= http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
modelVersion 4.0.0 /modelVersion
parent
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-parent /artifactId
version 2.1.6.RELEASE /version
relativePath/
/parent
groupId com.bolingcavalry /groupId
artifactId gatewaydemo /artifactId
packaging pom /packaging
version 1.0-SNAPSHOT /version
modules
module eureak /module
module provider /module
module gateway /module
/modules
properties
java.version 1.8 /java.version
spring-boot.version 2.1.6.RELEASE /spring-boot.version
maven-compiler-plugin.version 3.5 /maven-compiler-plugin.version
maven-deploy-plugin.version 2.8.2 /maven-deploy-plugin.version
maven-failsafe-plugin.version 2.18.1 /maven-failsafe-plugin.version
maven-surefire-plugin.version 2.21.0 /maven-surefire-plugin.version
spring-cloud.version Greenwich.SR2 /spring-cloud.version
/properties
dependencyManagement
dependencies
dependency
groupId org.springframework.cloud /groupId
artifactId spring-cloud-dependencies /artifactId
version ${spring-cloud.version} /version
type pom /type
scope import /scope
/dependency
/dependencies
/dependencyManagement
/project
如果您是用 IDEA 創建的工程,那么 IDEA 可能會在 pom.xml 所在目錄自動創建 font color= blue src /font 文件夾,請手動將其刪除,因為用不上;
eureka 工程
接下來是創建注冊中心,鼠標右鍵點擊 font color= blue gatewaydemo /font 文件夾,選擇 New – Module:
在彈出窗口選擇 font color= blue Spring Initializr /font,如下圖:
接下來的窗口填寫 Group、Artifact(這里是 eureka)、Version 等信息,其余的默認,即可完成子工程的創建;
新的 eureka 模塊的 pom.xml,請修改成如下內容,可見除了指定父工程,還依賴了 font color= blue spring-cloud-starter-netflix-eureka-server /font:
?xml version= 1.0 encoding= UTF-8 ?
project xmlns= http://maven.apache.org/POM/4.0.0 xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation= http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
parent
artifactId gatewaydemo /artifactId
groupId com.bolingcavalry /groupId
version 1.0-SNAPSHOT /version
/parent
modelVersion 4.0.0 /modelVersion
artifactId eureak /artifactId
packaging war /packaging
name eureak Maven Webapp /name
dependencies
dependency
groupId org.springframework.cloud /groupId
artifactId spring-cloud-starter-netflix-eureka-server /artifactId
/dependency
/dependencies
build
finalName ${project.artifactId} /finalName
plugins
plugin
groupId org.springframework.boot /groupId
artifactId spring-boot-maven-plugin /artifactId
/plugin
/plugins
/build
/project
src\main\resources 目錄下新增 application.yml 文件,內容如下,這是普通的注冊中心設置:
spring:
application:
name: eureka
server:
port: 8080
eureka:
client:
service-url:
defaultZone: http://localhost:${server.port}/eureka/
fetch-registry: false
register-with-eureka: false
java 文件只有一個,就是啟動類,還通過注解 EnableEurekaServer 開啟了注冊中心服務:
package com.bolingcavalry.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args);
}
}
以上就是注冊中心 eureka 的內容,運行 EurekaApplication 即可啟動服務,訪問 8080 端口的結果如下:現在注冊中心已經就緒,開始編寫服務提供者 provider 應用的代碼吧。
provider 工程
在 gatewaydemo 下創建一個子工程,名為 provider,pom.xml 內容如下,可見用到了 spring-boot-starter-web 和 spring-cloud-starter-netflix-eureka-client 這兩個依賴,分別用來支持 web 服務和注冊發現:
?xml version= 1.0 encoding= UTF-8 ?
project xmlns= http://maven.apache.org/POM/4.0.0
xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation= http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
parent
artifactId gatewaydemo /artifactId
groupId com.bolingcavalry /groupId
version 1.0-SNAPSHOT /version
/parent
modelVersion 4.0.0 /modelVersion
artifactId provider /artifactId
dependencies
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-web /artifactId
/dependency
dependency
groupId org.springframework.cloud /groupId
artifactId spring-cloud-starter-netflix-eureka-client /artifactId
/dependency
/dependencies
build
finalName ${project.artifactId} /finalName
plugins
plugin
groupId org.springframework.boot /groupId
artifactId spring-boot-maven-plugin /artifactId
/plugin
/plugins
/build
/project
配置文件 application.yml 如下,指定了注冊中心地址,并且自身端口為 8081:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
server:
port: 8081
spring:
application:
name: provider
啟動類 ProviderApplication.java:
package com.bolingcavalry.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args);
}
}
增加一個 controller,用于響應 web 請求,注意 hello 方法會從請求的 header 中取出名為 font color= blue extendtag /font 的屬性值,返回給瀏覽器:
package com.bolingcavalry.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
@RequestMapping(/hello)
public class HelloController { @RequestMapping(value = time , method = RequestMethod.GET)
public String hello(HttpServletRequest request){
return hello,
+ new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(new Date())
+ , extendtag [ + request.getHeader( extendtag)
+ ]
}
}
啟動應用,再次刷新 eureka 的頁面 localhost:8080,可見 provider 應用已經注冊上去了,如下圖紅框所示:
訪問地址:http://localhost:8081/hello/time,這是 controller 提供的 web 服務接口,得到響應如下圖,因為 header 中沒有名為 extendtag 的屬性,因此返回了 null:提供服務的 provider 已經 OK,可以開發網關服務了;
gateway 工程
在 gatewaydemo 下創建一個子工程,名為 gateway,pom.xml 內容如下,可見用到了 spring-cloud-starter-gateway 和 spring-cloud-starter-netflix-eureka-client 這兩個依賴,分別用來支持網關服務和注冊發現:
?xml version= 1.0 encoding= UTF-8 ?
project xmlns= http://maven.apache.org/POM/4.0.0
xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation= http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
parent
artifactId gatewaydemo /artifactId
groupId com.bolingcavalry /groupId
version 1.0-SNAPSHOT /version
/parent
modelVersion 4.0.0 /modelVersion
artifactId gateway /artifactId
dependencies
dependency
groupId org.springframework.cloud /groupId
artifactId spring-cloud-starter-netflix-eureka-client /artifactId
/dependency
dependency
groupId org.springframework.cloud /groupId
artifactId spring-cloud-starter-gateway /artifactId
/dependency
/dependencies
build
finalName ${project.artifactId} /finalName
plugins
plugin
groupId org.springframework.boot /groupId
artifactId spring-boot-maven-plugin /artifactId
/plugin
/plugins
/build
/project
配置文件 application.yml 如下,指定了注冊中心地址,并且自身端口為 8082,還有開啟了網關服務:
server:
port: 8082
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lowerCaseServiceId: true
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka/
啟動類 GatewayApplication .java,可見實例化了一個 RouteLocator,該實例就是路由規則,具體的功能請參考代碼中的注釋:
package com.bolingcavalry.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes()
// 增加一個 path 匹配,以 /gateway/hello/ 開頭的請求都在此路由
.route(r - r.path( /customize/hello/**)
// 表示將路徑中的第一級參數刪除,用剩下的路徑與 provider 的路徑做拼接, // 這里就是 lb://provider/hello/,能匹配到 provider 的 HelloController 的路徑
.filters(f - f.stripPrefix(1)
// 在請求的 header 中添加一個 key value
.addRequestHeader(extendtag , geteway- + System.currentTimeMillis()))
// 指定匹配服務 provider,lb 是 load balance 的意思
.uri(lb://provider)
).build();
}
}
啟動應用,再次刷新 eureka 的頁面 localhost:8080,可見 gateway 應用已經注冊上去了,如下圖紅框所示:
訪問地址:http://localhost:8082/customize/hello/time,這是符合前面我們配置的路由規則的路徑,customize 被刪除掉之后,將剩余的路徑轉發到 provider 服務,于是請求的真正地址就是 provider 服務的 /hello/time,得到響應如下圖,因為 gateway 在轉發的時候給 header 中設置了名為 extendtag 的屬性,因此返回了 extendtag 是有內容的:
以上是“SpringCloud Gateway 怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!