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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

9.SpringCloud Gateway网关

發(fā)布時間:2023/12/20 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 9.SpringCloud Gateway网关 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

SpringCloud系列文章列表

0. SpringCloud實戰(zhàn)專欄介紹準備
1. SpringCloud父工程搭建
2. 服務注冊中心之Eureka(單機+集群+Ribbon調(diào)用)
3. 服務注冊中心之Zookeeper
4. 服務注冊中心之Consul
5. eureka、zookeeper和consul三種注冊中心之間的區(qū)別
6. 負載均衡服務調(diào)用之Ribbon
7. 服務調(diào)用之OpenFeign
8. Hystrix斷路器全面實戰(zhàn)總結
9. SpringCloud Gateway網(wǎng)關
10. SpringCloud Config配置中心
11. SpringCloud Bus消息總線
12. SpringCloud Stream消息驅(qū)動
13. SpringCloud Sleuth分布式請求鏈路追蹤

目錄

      • **SpringCloud系列文章列表**
      • 1 Gateway介紹
        • 1.1 概述
        • 1.2 功能
        • 1.3 gateway vs zuul
          • 1.3.1 為什么選擇Gateway?
          • 1.3.2 Zuul1.x模型
          • 1.3.3 Gateway模型
        • 1.4 三大核心概念
        • 1.5 Gateway工作流程
      • 2 實戰(zhàn)
        • 2.1 入門配置
        • 2.2 通過微服務名實現(xiàn)動態(tài)路由
          • 2.3 Gateway網(wǎng)關路由配置的兩種方式
        • 2.3 Predicate的使用
        • 2.4 Filter的使用
          • 2.4.1 介紹
          • 2.4.2 自定義全局過濾器

1 Gateway介紹

官方文檔:
https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/
中文 https://www.springcloud.cc/spring-cloud-greenwich.html#_spring_cloud_gateway

1.1 概述


SpringCloud Gateway是在Spring生態(tài)系統(tǒng)之上構建的API網(wǎng)關服務,基于Spring 5,Spring Boot 2和Project Reactor等技術。
SpringCloud Gateway旨在提供一種簡單而有效的方式來對API進行路由,以及提供一些強大的過濾器功能,例如:熔斷、限流、重試等;

SpringCloud Gateway作為Spring Cloud 生態(tài)系統(tǒng)中的網(wǎng)關,目標是替代Zuul,因為在Spring Cloud 2.0以上版本中,沒有對應新版本的Zuul 2.x

Spring Cloud Gateway使用的Webflux中的reactor-netty響應式編程組件,底層使用了Netty通訊框架

1.2 功能

  • 反向代理
  • 鑒權
  • 熔斷
  • 日志監(jiān)控

1.3 gateway vs zuul

1.3.1 為什么選擇Gateway?
  • neflix不太靠譜,zuul2.0一直跳票,遲遲不發(fā)布
    一方面因為Zuul1.0已經(jīng)進入了維護階段,而且Gateway是SpringCloud團隊研發(fā)的,是親兒子產(chǎn)品,值得信賴。而且很多功能Zuul都沒有用起來也非常的簡單便捷。
    Gateway是基于異步非阻塞模型上進行開發(fā)的,性能方面不需要擔心。雖然Netflix早就發(fā)布了最新的Zuul 2x,但Spring Cloud貌似沒有整合計劃。而且Netflix相關組件都宣布進入維護期;不知前景如?
    多方面綜合考慮Gateway是很理想的網(wǎng)關選擇。

  • gateway具有如下特性

  • gateway與zuul的區(qū)別

1.3.2 Zuul1.x模型


1.3.3 Gateway模型

基于Spring WebFlux的異步非阻塞模型

1.4 三大核心概念

  • Route(路由)
    路由是構建網(wǎng)關的基本模塊,它由ID,目標URI,一系列的斷言和過濾器組成,如果斷言為true則匹配i該路由

  • Predicate(斷言)
    參考的是java8的java.util.function.Predicate,開發(fā)人員可以匹配Http請求中的所有內(nèi)容(例如請求頭或請求參數(shù)),如果請求與斷言相匹配則進行路由

  • Filter(過濾)
    指的是Spring框架中GatewayFilter的實例,使用過濾器,可以在請求被路由前或者之后對請求進行修改。


web請求,通過一些匹配條件,定位到真正的服務節(jié)點。并在這個轉(zhuǎn)發(fā)過程的前后,進行一些精細化控制。
predicate就是我們的匹配條件;而fiter,就可以理解為一個無所不能的攔截器。有了這兩個元素,再加上目標uri,就可以實現(xiàn)一個具體的路由了

1.5 Gateway工作流程

官網(wǎng)總結

Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a route, it is sent to the Gateway Web Handler. This handler runs the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line is that filters can run logic both before and after the proxy request is sent. All “pre” filter logic is executed. Then the proxy request is made. After the proxy request is made, the “post” filter logic is run.
翻譯如下:
客戶端向Spring Cloud網(wǎng)關發(fā)出請求。如果網(wǎng)關處理程序映射確定請求與路由匹配,則將其發(fā)送到網(wǎng)關Web處理程序。該處理程序運行通過特定于請求的篩選器鏈發(fā)送請求。篩選器由虛線分隔的原因是,篩選器可以在發(fā)送代理請求之前或之后執(zhí)行邏輯。執(zhí)行所有“前置”過濾器邏輯,然后發(fā)出代理請求。發(fā)出代理請求后,將執(zhí)行“后”過濾器邏輯。

一句話,核心邏輯就是路由轉(zhuǎn)發(fā)+執(zhí)行過濾器鏈

2 實戰(zhàn)

2.1 入門配置

新建項目cloud-gateway-gateway9527

  • pom.xml
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>
  • application.yml
server:port: 9527spring:application:name: cloud-gateway9527cloud:gateway:routes:- id: baiduuri: http://www.baidu.compredicates:- Path=/baidufilters: # 過濾器,對請求進行攔截,實現(xiàn)自定義的功能,對應 FilterDefinition 數(shù)組- StripPrefix=1 #會將請求的 Path 去除掉前綴,參數(shù)拼接在uri后面;默認0- id: payment_routeuri: http://localhost:8001predicates:- Path=/payment/**

訪問http://localhost:9527/baidu?gg=123的時候,實際訪問到 http://www.baidu.com?gg=123
訪問http://localhost:9527/payment/hello , 實際訪問到 http://localhost:8001/payment/hello

  • 啟動類
    無額外注解
  • 案例測試
    啟動項目cloud-gateway-gateway9527、cloud-provider-payment8001
    瀏覽器訪問:http://localhost:9527/baidu?gg=123

    瀏覽器訪問:http://localhost:9527/payment/hello
    實際訪問到了8001項目的/payment/hello接口

    總結:
    訪問網(wǎng)關url,首先通過一系列predicates斷言配置匹配路由規(guī)則,經(jīng)過對應的filters過濾后,路由到對應的uri

2.2 通過微服務名實現(xiàn)動態(tài)路由

修改項目cloud-gateway-gateway9527,注冊到eureka

  • pom.xml
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
  • yml
server:port: 9527spring:application:name: cloud-gateway9527cloud:gateway:routes:- id: baiduuri: http://www.baidu.compredicates:- Path=/baidufilters: # 過濾器,對請求進行攔截,實現(xiàn)自定義的功能,對應 FilterDefinition 數(shù)組- StripPrefix=1 #會將請求的 Path 去除掉前綴,參數(shù)拼接在uri后面;默認0- id: payment_route#uri: http://localhost:8001uri: lb://PROVIDER-PAYMENT #動態(tài)路由predicates:- Path=/payment/** eureka:instance:hostname: cloud-gateway-serviceclient:service-url:register-with-eureka: falsefetch-registry: truedefaultZone: http://localhost:7001/eureka

注意 動態(tài)路由的uri格式就是 lb://服務名

  • 啟動類
    新增注解@EnableEurekaClient
  • 案例測試
    啟動項目cloud-eureka-server7001、cloud-provider-payment8001、cloud-gateway-gateway9527
    瀏覽器訪問 http://localhost:9527/payment/hello

    成功的訪問到了8001項目
2.3 Gateway網(wǎng)關路由配置的兩種方式

第一種方式就是上面的在application.yml配置文件中配置;
下面介紹第二種代碼配置;
修改cloud-gateway-gateway9527

  • 新增配置類GatewayConfig
@Configuration public class GatewayConfig {@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder builder){RouteLocatorBuilder.Builder routes = builder.routes();routes.route("path_rote1", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();routes.route("path_rote2", r -> r.path("/guoji").uri("http://news.baidu.com/guoji")).build();return routes.build();}}

配置內(nèi)容跟在yml中配置一樣,只是換了種寫法

  • 案例測試
    重啟項目cloud-gateway-gateway9527
    瀏覽器訪問 http://localhost:9527/guoji

2.3 Predicate的使用

這塊用法可以參考官方文檔,寫的比較詳細,而且都有對應的例子,多練練,多理解
可以參考官方文檔: https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#gateway-request-predicates-factories

總結: 說白了,Predicate就是為了實現(xiàn)一組匹配規(guī)則,讓請求過來找到對應的Route進行處理

2.4 Filter的使用

當一個請求到達一個Gateway的路由時,Filtering Web Handler會加載所有的GlobalFilter實例以及這個路由上配置的所有的GatewayFilter過濾器,然后組成一個完整的過濾鏈。
這個過濾鏈中過濾器使用org.springframework.core.Ordered接口進行排序,可以通過實現(xiàn)Ordered接口中的getOrder()方法或直接使用@Order注解修改過濾器的順序。
由于Spring Cloud Gateway分開執(zhí)行“pre”和“post”的過濾器,因此,優(yōu)先級高的過濾器將先執(zhí)行“pre”類型的過濾器,最后執(zhí)行“post”類型的的過濾器

2.4.1 介紹
  • 概念
    路由過濾器可用于修改進入的HTTP請求和返回的HTTP響應,路由過濾器只能指定路由進行使用。
    Spring Cloud Gateway內(nèi)置了多種路由過濾器,他們都由GatewayFilter的工廠類來產(chǎn)生。
  • 生命周期
    pre, 在業(yè)務邏輯之前
    post,在業(yè)務邏輯之后
  • 種類
    單一, GatewayFilter(31種,具體見下面官網(wǎng)地址)
    https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#gatewayfilter-factories
    全局,GlobalFilter (10種,具體見下面官網(wǎng)地址)
    https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/#global-filters
2.4.2 自定義全局過濾器

implements GlobalFilter, Ordered兩個接口
可以用來做全局日志記錄、統(tǒng)一網(wǎng)關鑒權等

代碼實戰(zhàn)演示:
修改項目cloud-gateway-gateway9527
增加配置類

@Component public class GlobalGatewayFilter implements GlobalFilter, Ordered {Logger logger = LoggerFactory.getLogger(GlobalFilter.class);@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {logger.info("========GlobalGatewayFilter========");String uname = exchange.getRequest().getQueryParams().getFirst("uname");if(uname == null){logger.info("========uname is null========");exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);return exchange.getResponse().setComplete();}return chain.filter(exchange);}@Overridepublic int getOrder() {return 0;} }

案例測試:
啟動項目cloud-eureka-server7001,cloud-gateway-gateway9527
瀏覽器訪問:http://localhost:9527/guonei


可見,沒攜帶uname參數(shù),被filter攔截成功了。
瀏覽器訪問:http://localhost:9527/guonei?uname=ws

點贊+評論+關注
本文源碼地址: https://gitee.com/shuaidawang/SpringCloudDemo.git
有錯誤的地方歡迎各位大佬指正!可以加入qq交流群: 700637673

總結

以上是生活随笔為你收集整理的9.SpringCloud Gateway网关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。