Spring Cloud Gateway 是 Spring Cloud 的一個全新專案,該專案是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等回應式編程和事件流技術開發的網關,它旨在為微服務架構提供一種簡單有效的統一的 API 路由管理方式,
快速入門
1.創建gateway服務,引入依賴
<!--網關-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos服務發現依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.撰寫啟動類
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
3.撰寫基礎配置和路由規則
server:
port: 10010 # 網關埠
spring:
application:
name: gateway # 服務名稱
cloud:
nacos:
server-addr: localhost:8848 # nacos地址
gateway:
routes: # 網關路由配置
- id: user-service # 路由id,自定義,只要唯一即可
# uri: http://127.0.0.1:8081 # 路由的目標地址 http就是固定地址
uri: lb://userservice # 路由的目標地址 lb就是負載均衡,后面跟服務名稱
predicates: # 路由斷言,也就是判斷請求是否符合路由規則的條件
- Path=/user/** # 這個是按照路徑匹配,只要以/user/開頭就符合要求
我們將符合Path 規則的一切請求,都代理到 uri引數指定的地址,
本例中,我們將 /user/**開頭的請求,代理到lb://userservice,lb是負載均衡,根據服務名拉取服務串列,實作負載均衡,
4.重啟測驗
重啟網關,訪問http://localhost:10010/user/1時,符合/user/**規則,請求轉發到uri:http://userservice/user/1
過濾器工廠
GatewayFilter是網關中提供的一種過濾器,可以對進入網關的請求和微服務回傳的回應做處理
1.路由過濾器的種類
Spring提供了31種不同的路由過濾器工廠,例如:
| 名稱 | 說明 |
|---|---|
| AddRequestHeader | 給當前請求添加一個請求頭 |
| RemoveRequestHeader | 移除請求中的一個請求頭 |
| AddResponseHeader | 給回應結果中添加一個回應頭 |
| RemoveResponseHeader | 從回應結果中移除有一個回應頭 |
| RequestRateLimiter | 限制請求的流量 |
2.請求頭過濾器
下面我們以AddRequestHeader 為例來講解,
需求:給所有進入userservice的請求添加一個請求頭:Truth=itcast is freaking awesome!
只需要修改gateway服務的application.yml檔案,添加路由過濾即可:
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://userservice
predicates:
- Path=/user/**
filters: # 過濾器
- AddRequestHeader=Truth, Itcast is freaking awesome! # 添加請求頭
當前過濾器寫在userservice路由下,因此僅僅對訪問userservice的請求有效,
3.默認過濾器
如果要對所有的路由都生效,則可以將過濾器工廠寫到default下,格式如下:
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://userservice
predicates:
- Path=/user/**
default-filters: # 默認過濾項
- AddRequestHeader=Truth, Itcast is freaking awesome!
全域過濾器
全域過濾器的作用也是處理一切進入網關的請求和微服務回應,與GatewayFilter的作用一樣,區別在于GatewayFilter通過配置定義,處理邏輯是固定的;而GlobalFilter的邏輯需要自己寫代碼實作,
定義方式是實作GlobalFilter介面,
public interface GlobalFilter {
/**
* 處理當前請求,有必要的話通過{@link GatewayFilterChain}將請求交給下一個過濾器處理
*
* @param exchange 請求背景關系,里面可以獲取Request、Response等資訊
* @param chain 用來把請求委托給下一個過濾器
* @return {@code Mono<Void>} 回傳標示當前過濾器業務結束
*/
Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
}
在filter中撰寫自定義邏輯,可以實作下列功能:
- 登錄狀態判斷
- 權限校驗
- 請求限流等
解決跨域問題
在gateway服務的application.yml檔案中,添加下面的配置:
spring:
cloud:
gateway:
# ,,,
globalcors: # 全域的跨域處理
add-to-simple-url-handler-mapping: true # 解決options請求被攔截問題
corsConfigurations:
'[/**]':
allowedOrigins: # 允許哪些網站的跨域請求
- "http://localhost:8090"
- "http://127.0.0.1:8090"
allowedMethods: # 允許的跨域ajax的請求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允許在請求中攜帶的頭資訊
allowCredentials: true # 是否允許攜帶cookie
maxAge: 360000 # 這次跨域檢測的有效期
#.斷言工廠
我們在組態檔中寫的斷言規則只是字串,這些字串會被Predicate Factory讀取并處理,轉變為路由判斷的條件
例如Path=/user/**是按照路徑匹配,這個規則是由
org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory類來
處理的,像這樣的斷言工廠在SpringCloudGateway還有十幾個:
| 名稱 | 說明 | 示例 |
|---|---|---|
| After | 是某個時間點后的請求 | - After=2037-01-20T17:42:47.789-07:00[America/Denver] |
| Before | 是某個時間點之前的請求 | - Before=2031-04-13T15:14:47.433+08:00[Asia/Shanghai] |
| Between | 是某兩個時間點之前的請求 | - Between=2037-01-20T17:42:47.789-07:00[America/Denver], 2037-01-21T17:42:47.789-07:00[America/Denver] |
| Cookie | 請求必須包含某些cookie | - Cookie=chocolate, ch.p |
| Header | 請求必須包含某些header | - Header=X-Request-Id, \d+ |
| Host | 請求必須是訪問某個host(域名) | - Host=.somehost.org,.anotherhost.org |
| Method | 請求方式必須是指定方式 | - Method=GET,POST |
| Path | 請求路徑必須符合指定規則 | - Path=/red/{segment},/blue/ |
| Query | 請求引數必須包含指定引數 | - Query=name, Jack或者- Query=name |
| RemoteAddr | 請求者的ip必須是指定范圍 | - RemoteAddr=192.168.1.1/24 |
| Weight | 權重處理 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543957.html
標籤:Java
上一篇:多級快取降低高并發壓力
下一篇:Spring
