日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

SpringCloud Alibaba微服务实战(六) - 路由网关(Gateway)

發布時間:2023/11/28 生活经验 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud Alibaba微服务实战(六) - 路由网关(Gateway) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是 Spring Cloud Gateway?

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術開發的網關,Spring Cloud Gateway 旨在為微服務架構提供一種簡單而有效的統一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態系中的網關,目標是替代 Netflix ZUUL,其不僅提供統一的路由方式,并且基于 Filter 鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。

Spring Cloud Gateway 功能特征

  • 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
  • 動態路由
  • Predicates 和 Filters 作用于特定路由
  • 集成 Hystrix 斷路器
  • 集成 Spring Cloud DiscoveryClient
  • 易于編寫的 Predicates 和 Filters
  • 限流
  • 路徑重寫

Spring Cloud Gateway 工程流程


客戶端向 Spring Cloud Gateway 發出請求。然后在 Gateway Handler Mapping 中找到與請求相匹配的路由,將其發送到 Gateway Web Handler。Handler 再通過指定的過濾器鏈來將請求發送到我們實際的服務執行業務邏輯,然后返回。

過濾器之間用虛線分開是因為過濾器可能會在發送代理請求之前(pre)或之后(post)執行業務邏輯。

構建路由網關cloud-alibaba

1.在父項目中創建子module項目名字為cloud-alibaba,在pom中引入Gateway依賴

<!--gateway網關 不能引入starter-web--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>

完整服務消費者cloud-alibaba的pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><artifactId>cloud-alibaba</artifactId><groupId>com.zsy</groupId><version>1.0-SNAPSHOT</version></parent><groupId>com.zsy</groupId><artifactId>cloud-gateway</artifactId><version>0.0.1-SNAPSHOT</version><name>cloud-gateway</name><dependencies><!--gateway網關 不能引入starter-web--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2.配置application.yml或者properties文件

server:port: 7777
spring:application:name: api-gatewaycloud:gateway:discovery:locator:enabled: true #讓gateway可以發現nacos中的微服務routes: # 路由數組  指當請求滿足什么樣的條件的時候,轉發到哪個微服務上- id: cloud-account #當前路由標識,要求唯一 (默認值uuid,一般不用,需要自定義)uri: lb://cloud-account #請求最終要被轉發的地址   lb指的是從nacos中按照名稱獲取微服務,并遵循負載均衡策略order: 1 #路由優先級,數字越小,優先級越高predicates: #斷言 判斷條件,返回值是boolean 轉發請求要返回的條件 (可以寫多個)、- Path=/cloud-account/** #當請求路徑滿足path指定的規則時,此路由信息才會正常轉發filters: #過濾器(在請求傳遞過程中,對請求做一些手腳)- StripPrefix=1 # 在請求轉發之前去掉一層路徑nacos:discovery:server-addr: 127.0.0.1:8848

3.配置消費者啟動服務的啟動類

package com.zsy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGatewayApplication {public static void main(String[] args) {SpringApplication.run(CloudGatewayApplication.class, args);}
}

4.啟動cloud-account服務項目,啟動成功,如下圖

5. 驗證服務,瀏覽器訪問http://localhost:8081/getAccount,返回如下圖

查看cloud-account控制臺,如下圖

6.啟動cloud-gateway項目,啟動成功,如下圖

7. 驗證服務,瀏覽器訪問http://localhost:777/cloud-account/getAccount,返回如下圖

注意:請求方式是 http://路由網關IP:路由網關Port/服務名


查看消費者cloud-account控制臺,如下圖

Spring Cloud Gateway 的路由功能配置成功

碼云地址:https://gitee.com/zlzhaoe/cloud-alibaba

總結

以上是生活随笔為你收集整理的SpringCloud Alibaba微服务实战(六) - 路由网关(Gateway)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。