Gateway其實是springcloud 原生的東西,但是我還是想放在這里講,因為我們使用nacos時,前端調(diào)用服務(wù)之后,一般會調(diào)用到我們的網(wǎng)關(guān)上面,然后網(wǎng)關(guān)選擇我們的nacos服務(wù),再調(diào)用后端的服務(wù)
在當今微服務(wù)架構(gòu)中,網(wǎng)關(guān)起著至關(guān)重要的角色。它充當著應(yīng)用程序和外部世界之間的門戶,處理來自客戶端的請求并將其轉(zhuǎn)發(fā)到適當?shù)奈⒎?wù)。而Spring Cloud Gateway作為Spring Cloud生態(tài)系統(tǒng)中的一員,為我們提供了一個強大而靈活的網(wǎng)關(guān)解決方案。本文將深入探討Spring Cloud Gateway的配置與使用,幫助讀者更好地理解并應(yīng)用這個優(yōu)秀的網(wǎng)關(guān)工具。
??第一步,創(chuàng)建一個springboot工程
??第二步,添加依賴
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
??第三步,編寫yml文件
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.0.93:8848
gateway:
routes:
- id: service-provider
uri: lb://service-provider
predicates:
- Path=/provider/** #以provider開頭的請求都負載到provider服務(wù)
filters:
- RewritePath=/provider/(?<segment>.*), /$\{segment} #過濾掉url里的provider,例如http://ip:port/provider/getCity -> http://ip:port/getCity
application:
name: gateway
server:
port: 8999
解讀配置文件:
- RewritePath:過濾掉url里的provider,例如http://ip:port/provider/getCity -> http://ip:port/getCity
- Path:以provider開頭的請求都負載到provider服務(wù)
- id:route的唯一id
- uri:需要返回的nacos中間的服務(wù)
這是我們的nacos控制臺,里面有一個service-provider服務(wù),他負責(zé)提供api
所以yml里面的routes我們配置uri為lb://service-provider
假如前端要訪問我們的服務(wù),他只需要訪問http://localhost:8999/provider
就好,他是以provider
開頭,所以
Path這里我們配置為- Path=/provider/**
但是我們服務(wù)提供者提供的api為/echo/{string}
,所以我們需要去掉provider
,因此RewritePath配置為/provider/(?<segment>.*), /$\{segment}
??第四步,啟動主啟動類
之后網(wǎng)關(guān)服務(wù)就會被注冊到nacos中
然后就可以通過我們的gateway服務(wù)訪問我們nacos中的服務(wù)了
提供者提供的api如下
官網(wǎng):
https://spring.io/projects/spring-cloud-gateway
倉庫地址:
https://gitee.com/WangFuGui-Ma/spring-cloud-alibaba/tree/master
通過本文的介紹,我們了解了Spring Cloud Gateway的核心概念和基本配置。它的靈活性和可擴展性使得它成為構(gòu)建可靠、高性能微服務(wù)架構(gòu)的理想選擇。希望本文對于讀者們在使用Spring Cloud Gateway時有所幫助,并能夠加深對該組件的理解和運用。愿您在未來的項目中,能夠借助Spring Cloud Gateway輕松實現(xiàn)安全、高效的網(wǎng)關(guān)服務(wù)。文章來源:http://www.zghlxwxcb.cn/news/detail-491597.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-491597.html
到了這里,關(guān)于【Spring Cloud】Gateway的配置與使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!