gateway的官方文檔解讀
標簽: gateway
之前公司用了springcloud的gateway.被一個伙伴留下了一堆的坑,沒辦法只能從頭梳理.
第一步就是確定架構, gateway+consul+springboot
第二步就是確定一個flag 要解決哪些問題
1.解決灰度負載均衡策略問題:如何配置,支持哪些配置
2.解決ip名單和限流的問題:如何配置,支持哪些配置
3.解決路由重寫的問題: 將msg服務,stats服務和action服務整合到business
4.解決consul摘機后服務列表不更新的問題
第三步就是開始官方文檔的解讀
我承認這篇博客不是一個好博客,因為不夠親民. 主要寫下來就是給自己留個筆記罷了.謝謝!!!!
官方文檔地址:https://docs.spring.io/spring-cloud-gateway/docs/2.2.5.RELEASE/reference/html/
1. How to Include Spring Cloud Gateway
這個自己百度吧,一堆堆的
簡單來說里面有個警告: 就是用了netty的思想,有些東西吧不好使.悠著點.
2. Glossary
Route: 路由
Predicate:斷言
Filter:過濾器
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.
以下是一段純google翻譯
客戶端向Spring Cloud Gateway發出請求。 如果網關處理程序映射確定請求與路由匹配,則將其發送到網關Web處理程序。 該處理程序通過特定于請求的過濾器鏈運行請求。 篩選器由虛線分隔的原因是,篩選器可以在發送代理請求之前和之后運行邏輯。 所有“前置”過濾器邏輯均被執行。 然后發出代理請求。 發出代理請求后,將運行“后”過濾器邏輯。
簡單來說請求先匹配Handler Mapping,然后交給gate way web Handler,這哥們有一套獨立的過濾器鏈(感覺有點像是netty的selector)后續需要再校正, 經過一頓prefilter的過濾,拿到返回值在進行一通postfilter
4. Configuring Route Predicate Factories and Gateway Filter Factories
配置路由,斷言和過濾器
4.1. Shortcut Configuration
短鏈方式
spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - Cookie=mycookie,mycookievalue
4.2. Fully Expanded Arguments
長鏈方式
spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - name: Cookie args: name: mycookie regexp: mycookievalue
官方建議短鏈方式:
predicates:
- Cookie=mycookie,mycookievalue
這里面聲明了幾個問題, 一個gateway區塊有id, url, predicates 組成 predicates 可以寫成 - 自定義id=name,value的形式
5. Route Predicate Factories 路由工廠
5.1. The After Route Predicate Factory
spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - After=2017-01-20T17:42:47.789-
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
This route matches any request made after Jan 20, 2017 17:42 Mountain Time (Denver).
5.2. The Before Route Predicate Factory
Example 2. application.yml
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
This route matches any request made before Jan 20, 2017 17:42 Mountain Time (Denver).
5.3. The Between Route Predicate Factory
Example 3. application.yml
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
This route matches any request made after Jan 20, 2017 17:42 Mountain Time (Denver) and before Jan 21, 2017 17:42 Mountain Time (Denver). This could be useful for maintenance windows.
5.4. The Cookie Route Predicate Factory
Example 4. application.yml
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
This route matches requests that have a cookie named chocolate
whose value matches the ch.p
regular expression.
5.5. The Header Route Predicate Factory
Example 5. application.yml
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
This route matches if the request has a header named X-Request-Id
whose value matches the \d+
regular expression (that is, it has a value of one or more digits)
5.6. The Host Route Predicate Factory
Example 6. application.yml
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
URI template variables (such as {sub}.myhost.org
) are supported as well.
This route matches if the request has a Host
header with a value of www.somehost.org
or beta.somehost.org
or www.anotherhost.org
.
5.7. The Method Route Predicate Factory
Example 7. application.yml
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
This route matches if the request method was a GET
or a POST
.
5.8. The Path Route Predicate Factory
5.8. The Path Route Predicate Factory
The Path
Route Predicate Factory takes two parameters: a list of Spring PathMatcher
patterns
and an optional flag called matchOptionalTrailingSeparator
. The following example configures a path route predicate:
Example 8. application.yml
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
This route matches if the request path was, for example: /red/1
or /red/blue
or /blue/green
.
This predicate extracts the URI template variables (such as segment
, defined in the preceding example) as a map of names and values and places it in the ServerWebExchange.getAttributes()
with a key defined in ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE
. Those values are then available for use by GatewayFilter
factories
5.9. The Query Route Predicate Factory
Example 9. application.yml
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
The preceding route matches if the request contained a green
query parameter.
5.10. The RemoteAddr Route Predicate Factory
Example 10. application.yml
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
This route matches if the remote address of the request was, for example, 192.168.1.10
.
5.11. The Weight Route Predicate Factory
Example 11. application.yml
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
This route would forward ~80% of traffic to weighthigh.org and ~20% of traffic to weighlow.org
綜上所述感覺gateway很牛逼吧,相當于拿著七層協議, header, paramter,cookie url隨意玩耍,而且是顯示配置.
智能推薦
Spring AOP編程官方文檔解讀之增強方法參數
系列文章目錄 第一章 : Spring AOP編程官方文檔解讀之切點 第二章 : Spring AOP編程官方文檔解讀之增強 第三章 : Spring AOP編程官方文檔解讀之增強方法參數 文章目錄 系列文章目錄 前言 JoinPoint 當前連接點信息 ProceedingJoinPoint 當前連接點信息 args 通過切點定義傳入參數 傳入annotation參數 泛型類參數 獲取參數名稱 ...
Spring AOP編程官方文檔解讀之增強
系列文章目錄 第一章 : Spring AOP編程官方文檔解讀之切點 第二章 : Spring AOP編程官方文檔解讀之增強 第三章 : Spring AOP編程官方文檔解讀之增強方法參數 文章目錄 系列文章目錄 前言 Advice的定義方式 對命名切入點的簡單運用 就地聲明的切入點表達式 Advice的幾種類型 Before 方法執行前增強 After returning advice:正常返回...
Spring AOP編程官方文檔解讀之代理機制
系列文章目錄 第一章 : Spring AOP編程官方文檔解讀之切點 第二章 : Spring AOP編程官方文檔解讀之增強 第三章 : Spring AOP編程官方文檔解讀之增強方法參數 第四章 : Spring AOP編程官方文檔解讀之引介增強 第五章 : Spring AOP編程官方文檔解讀之另類切面Advisor 第六章 : Spring AOP編程官方文檔解讀之代理機制 文章目錄 系列文...
Spring AOP編程官方文檔解讀之Pointcut API篇
系列文章目錄 第一章 : Spring AOP編程官方文檔解讀之切點 第二章 : Spring AOP編程官方文檔解讀之增強 第三章 : Spring AOP編程官方文檔解讀之增強方法參數 第四章 : Spring AOP編程官方文檔解讀之引介增強 第五章 : Spring AOP編程官方文檔解讀之另類切面Advisor 第六章 : Spring AOP編程官方文檔解讀之代理機制 第七章 : Sp...
Spring AOP編程官方文檔解讀之Advice API篇
系列文章目錄 第一章 : Spring AOP編程官方文檔解讀之切點 第二章 : Spring AOP編程官方文檔解讀之增強 第三章 : Spring AOP編程官方文檔解讀之增強方法參數 第四章 : Spring AOP編程官方文檔解讀之引介增強 第五章 : Spring AOP編程官方文檔解讀之另類切面Advisor 第六章 : Spring AOP編程官方文檔解讀之代理機制 第七章 : Sp...
猜你喜歡
Spring AOP編程官方文檔解讀之ProxyFactoryBean篇
系列文章目錄 第一章 : Spring AOP編程官方文檔解讀之切點 第二章 : Spring AOP編程官方文檔解讀之增強 第三章 : Spring AOP編程官方文檔解讀之增強方法參數 第四章 : Spring AOP編程官方文檔解讀之引介增強 第五章 : Spring AOP編程官方文檔解讀之另類切面Advisor 第六章 : Spring AOP編程官方文檔解讀之代理機制 第七章 : Sp...
freemarker + ItextRender 根據模板生成PDF文件
1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...
requests實現全自動PPT模板
http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...