spring cloud Gateway簡單使用

2021-10-01 21:12:07 字數 4860 閱讀 9699

2年前有幸使用過一次spring cloud (1.5.9),1.*整合的是zuul做閘道器,沒有使用gateway做閘道器,一直是個小遺憾。終於在2年後的19年底再次使用spring cloud,這次果斷使用spring cloud 全家桶。閘道器就是原生的spring cloud gateway(專案使用2.1.4)。乙個簡單的創業專案架構圖如下:

自從撇開netflex zuul後,spring cloud速度搜搜的。我開發時還是用2.1.4,目前最新已經到2.2.1,附上官網飛機票

官**定如下:

1.使用route結合hystrix實現預設降級策略

2.使用gatewayfilter實現登入態(token)校驗

spring:

cloud:

gateway:

discovery:

locator:

enabled: false

#開啟小寫驗證,預設feign根據服務名查詢都是用的全大寫

lowercaseserviceid: true

default-filters:

- addresponseheader=x-response-default-foo, default-bar

routes:

- id: oloan-financial-product-service

# lb代表從註冊中心獲取服務

uri: lb://oloan-financial-product-service

predicates:

# **該路徑

- path=/gateway/financialproduct/**

# 帶字首

filters:

- stripprefix=1

- name: hystrix

args:

name: fallbackcmd

fallbackuri: forward:/defaultfallback

- id: admin-service

uri: lb://admin-service

predicates:

- path=/gateway/auth/**

filters:

- stripprefix=2

- name: hystrix

args:

name: fallbackcmd

fallbackuri: forward:/defaultfallback

如上圖,我們開啟了2個微服務route路由。

@slf4j

@restcontroller

public class defaulthystrixcontroller

}

3.2.1 自定義過濾器

自定義過濾器,實現gatewayfilter, ordered 2個介面。

17* @description token過濾器

18* @date 2019/12/12 13:55

19*/

20@slf4j

21@component

22public

class logintokenfilter implements

gatewayfilter, ordered

48//

取token

49 string token = this

.gettoken(tokenheader);

50 log.info("token=" +token);

5152

//token不存在

53if

(stringutils.isempty(token))

58//

校驗 token是否失效

59if (usertokentools.istokenexpired(token, null

)) 64

//校驗 token是否正確

65if (!usertokentools.checktoken(token, null

)) 69

70//

//有token 這裡可根據具體情況,看是否需要在gateway直接把解析出來的使用者資訊塞進請求中,我們最終沒有使用

71//

usertokeninfo usertokeninfo = usertokentools.getusertokeninfo(token);

72//

log.info("token={},usertokeninfo={}",token,usertokeninfo);

73//

request.getqueryparams().add("token",token);

74//

request.getheaders().set("token", token);

75return

chain.filter(exchange);76}

7778

79@override

80public

intgetorder()

8384

/**85

* 解析token

86*/

87public

string gettoken(string requestheader)

92return "";93}

94 }

上圖中,usertokentools是我們自定義的乙個jwt工具類,用來生成token,校驗token過期、正確等。

3.2.2 配置路由

大家可根據具體情況,如果只有一套登入態,那就用乙個filter即可。

1

import com.*.gateway.filter.authorizegatewayfilter;

2import com.*.gateway.filter.logintokenfilter;

3import

org.springframework.cloud.gateway.route.routelocator;

4import

org.springframework.cloud.gateway.route.builder.routelocatorbuilder;

5import

org.springframework.context.annotation.bean;

6import

org.springframework.context.annotation.configuration;78

9@configuration

10public

class

gatewayconfig

30 }

spring cloud gateway使用webflux,和spring boot web包衝突,使用時一定記得pom中排除相關jar,否則會報錯。

org.springframework.cloud

spring-cloud-starter-gateway

org.springframework

spring-webmvc

gateway filter 自帶的原始碼支撐錯誤碼response.setstatuscode(httpstatus.unauthorized);並不是那麼的友好。錯誤碼列舉使用的是spring自帶框架的列舉類:

。這樣請求返回

的結構體和一般定義的(code message data)不同。當然官方也是提供了解決方案。後續再去優化吧。

gateway預設實現了幾個簡單的限流策略(依賴redis),後續可以使用一下。

springcloud gateway解決跨域問題

方式一 if request method options if request method post if request method get 方式二 未加if判斷 add header access control allow origin add header access control...

Spring Cloud Gateway 配置資訊

了解gateway的配置才可以理解使用gateway可以做什麼事情,才能更好地應用在產品開發中。predicates主要起的作用是 配置路由匹配請求的規則 http 相關 path 配置對於請求路徑的匹配規則 yml配置,多個引數用逗號隔開 path aa bb json配置 cookie 配置對c...

spring cloud gateway 超時設定

專案中用了gateway,但有些子應用經常超時,連帶著gateway也阻塞了。為了解耦,加上加上超時設定。後面再考慮使用斷路器。網上搜到的文章多是zuul的,或者hystrix,ribbon的,不合需求。找了下原始碼,org.springframework.cloud.gateway.config....