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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Cloud Gateway(续)

發布時間:2025/3/21 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud Gateway(续) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要介紹 Spring Cloud Gateway 的路由熔斷、路由重試和高可用。

路由熔斷

在前面學習 Hystrix 的時候,我們知道 Hystrix 有服務降級的能力,即如果服務調用出現了異常,則執行指定的 fallback 方法。Spring Cloud Gateway 也融合了 Hystrix,可以為我們提供路由層面服務降級。我們就來看看如何來做。

在之前 gateway 工程的基礎上引入 Hystrix 依賴

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>

配置文件添加 Filter

spring:cloud:gateway:routes:- id: service_customeruri: lb://CONSUMERpredicates:- Path=/customer/**filters:- name: Hystrixargs:name: fallbackcmdfallbackUri: forward:/fallback

Hystrix 支持兩個參數:

  • name:即HystrixCommand的名字
  • fallbackUri:即 fallback 對應的 uri,這里的 uri 僅支持forward:?schemed 的

最后在 gateway 的項目中創建一個 endpoint:/fallback

@RestController public class FallbackController {@GetMapping("/fallback")public String fallback() {return "Hello World!\nfrom gateway";}}

僅做以上配置就 OK 了,如果需要更強大的功能,可以參考HystrixGatewayFilterFactory進行自定義。

路由重試

雖然目前官方文檔上還沒有關于重試的內容,不過我們能在代碼中找到RetryGatewayFilterFactory。但是據我測試這個好像還是有些問題。

理論上只要在 application.xml 里這么配置就可以了

spring:cloud:gateway:routes:- id: service_customeruri: lb://CONSUMERpredicates:- Path=/customer/**filters:- Retry=5

為了測試我們再改造一下 consumer 中的 HelloController,我們讓它在方法入口處打印 log,然后 sleep 10 分鐘

@CommonsLog @RequestMapping("/hello") @RestController public class HelloController {@AutowiredHelloRemote helloRemote;@GetMapping("/{name}")public String index(@PathVariable("name") String name) throws InterruptedException {log.info("the name is " + name);if ("retry".equals(name)) {TimeUnit.MINUTES.sleep(10);}return helloRemote.hello(name) + "\n" + new Date().toString();}}

但是實際測試發現,當 Gateway 向 consumer 請求的時候,根本就沒有超時這個概念,我也沒找到能設置超時的地方。唯一能找到的一個超時是通過 Hystrix 設置(參考?github),比如上邊我們HystrixCommand的 name 是 fallbackcmd,那么就設置如下

hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000

但是配置了 Hystrix 之后,超時就直接 fallback 了,所以根本輪不到 retry

java.lang.NullPointerException: nullat org.springframework.cloud.gateway.filter.factory.RetryGatewayFilterFactory.lambda$null$0(RetryGatewayFilterFactory.java:55) ~[spring-cloud-gateway-core-2.0.0.RC1.jar:2.0.0.RC1]

那我 Hystrix 只配置 name 不配置 fallbackUri 行不行?也不行。

雖然這樣確實 retry 了,在 consumer 的 log 中能看到多條打印,但是在 gateway 里邊卻報錯了

com.netflix.hystrix.exception.HystrixRuntimeException: fallbackcmd command executed multiple times - this is not permitted.

所以看樣子這個 Retry 現在還不能用,等有時間了再研究下。

高可用

我們實際使用 Spring Cloud Gateway 的方式如上圖,不同的客戶端使用不同的負載將請求分發到后端的 Gateway,Gateway 再通過 Eureka 調用后端服務,最后對外輸出。因此為了保證 Gateway 的高可用性,前端可以同時啟動多個 Gateway 實例進行負載,在 Gateway 的前端使用 Nginx 或者 F5 進行負載轉發以達到高可用性。

參考

Spring Cloud Gatewa - Hystrix GatewayFilter Factory

示例代碼可以從 Github 獲取:https://github.com/zhaoyibo/spring-cloud-study

  • 本文作者:?Yibo
  • 本文鏈接:?https://windmt.com/2018/05/11/spring-cloud-16-spring-cloud-gateway-others/
  • 版權聲明:?本博客所有文章除特別聲明外,均采用?CC BY-NC-SA 4.0?許可協議。轉載請注明出處!

總結

以上是生活随笔為你收集整理的Spring Cloud Gateway(续)的全部內容,希望文章能夠幫你解決所遇到的問題。

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