之前公司用了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:過濾器
3. How It Works
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隨意玩耍,而且是顯示配置.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/2754.html
標籤:python
