本篇文章為大家展示了Spring Cloud中怎么利用Gateway實(shí)現(xiàn)擴(kuò)展支持動態(tài)限流,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站專注于寧遠(yuǎn)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供寧遠(yuǎn)營銷型網(wǎng)站建設(shè),寧遠(yuǎn)網(wǎng)站制作、寧遠(yuǎn)網(wǎng)頁設(shè)計、寧遠(yuǎn)網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造寧遠(yuǎn)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供寧遠(yuǎn)網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
配置方式
spring: cloud: gateway: routes: - id: requestratelimiter_route uri: lb://pigx-upms order: 10000 predicates: - Path=/admin/** filters: - name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 1 redis-rate-limiter.burstCapacity: 3 key-resolver: "#{@remoteAddrKeyResolver}" #SPEL表達(dá)式去的對應(yīng)的bean - StripPrefix=1
RequestRateLimiterGatewayFilterFactory
public GatewayFilter apply(Config config) { KeyResolver resolver = getOrDefault(config.keyResolver, defaultKeyResolver); RateLimiter<object> limiter = getOrDefault(config.rateLimiter, defaultRateLimiter); boolean denyEmpty = getOrDefault(config.denyEmptyKey, this.denyEmptyKey); HttpStatusHolder emptyKeyStatus = HttpStatusHolder .parse(getOrDefault(config.emptyKeyStatus, this.emptyKeyStatusCode)); return (exchange, chain) -> { return exchange.getResponse().setComplete(); }); }); }; }
在實(shí)際生產(chǎn)過程中,必定不能滿足我們的需求
生產(chǎn)中路由信息是保存數(shù)據(jù)庫持久化或者配置中心,RequestRateLimiterGatewayFilterFactory
并不能隨著持久化數(shù)據(jù)的改變而動態(tài)改變限流參數(shù),不能做到實(shí)時根據(jù)流量來改變流量閾值
隨著微服務(wù)的流行,服務(wù)和服務(wù)之間的穩(wěn)定性變得越來越重要。Sentinel 以流量為切入點(diǎn),從流量控制、熔斷降級、系統(tǒng)負(fù)載保護(hù)等多個維度保護(hù)服務(wù)的穩(wěn)定性,分布式系統(tǒng)的流量防衛(wèi)兵。
從 1.6.0 版本開始,Sentinel 提供了 Spring Cloud Gateway 的適配模塊,可以提供兩種資源維度的限流: route 維度:即在 Spring 配置文件中配置的路由條目,資源名為對應(yīng)的 routeId 自定義 API 維度:用戶可以利用 Sentinel 提供的 API 來自定義一些 API 分組
<!--Spring Cloud Alibaba 封裝的 sentinel 模塊--> <dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-alibaba-sentinel-gateway</artifactid> </dependency> <!--使用nacos 保存限流規(guī)則--> <dependency> <groupid>com.alibaba.csp</groupid> <artifactid>sentinel-datasource-nacos</artifactid> </dependency>
spring: application: name: sentinel-spring-cloud-gateway cloud: gateway: enabled: true discovery: locator: lower-case-service-id: true routes: - id: pigx_route uri: https://api.readhub.cn predicates: - Path=/topic/** sentinel: datasource.ds1.nacos: server-addr: 127.0.0.1:8848 data-id: gw-flow group-id: DEFAULT_GROUP ruleType: gw-api-group filter: enabled: true
常用限流策略 常量
以客戶端IP作為限流因子 public static final int PARAM_PARSE_STRATEGY_CLIENT_IP = 0; 以客戶端HOST作為限流因子 public static final int PARAM_PARSE_STRATEGY_HOST = 1; 以客戶端HEADER參數(shù)作為限流因子 public static final int PARAM_PARSE_STRATEGY_HEADER = 2; 以客戶端請求參數(shù)作為限流因子 public static final int PARAM_PARSE_STRATEGY_URL_PARAM = 3; 以客戶端請求Cookie作為限流因子 public static final int PARAM_PARSE_STRATEGY_COOKIE = 4;
核心源碼解析 SentinelGatewayFilter
sentinel通過擴(kuò)展Gateway的過濾器,通過選擇的不同GatewayParamParser
過處理請求限流因子和數(shù)據(jù)源中的配置進(jìn)行比較 源碼如下:
public Mono<void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); Mono<void> asyncResult = chain.filter(exchange); if (route != null) { String routeId = route.getId(); Object[] params = paramParser.parseParameterFor(routeId, exchange, r -> r.getResourceMode() == SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID); String origin = Optional.ofNullable(GatewayCallbackManager.getRequestOriginParser()) .map(f -> f.apply(exchange)) .orElse(""); asyncResult = asyncResult.transform( new SentinelReactorTransformer<>(new EntryConfig(routeId, EntryType.IN, 1, params, new ContextConfig(contextName(routeId), origin))) ); } Set<string> matchingApis = pickMatchingApiDefinitions(exchange); for (String apiName : matchingApis) { Object[] params = paramParser.parseParameterFor(apiName, exchange, r -> r.getResourceMode() == SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME); asyncResult = asyncResult.transform( new SentinelReactorTransformer<>(new EntryConfig(apiName, EntryType.IN, 1, params)) ); } return asyncResult; }
以上nacos 配置為 每秒只能通過5個請求,我們使用jmeter 4.0
來并發(fā)10個線程測試一下
通過上圖可以結(jié)果證明sentinel限流確實(shí)有效
sentinel-datasource-nacos
作為sentinel的數(shù)據(jù)源,可以從如上 nacos 管理臺實(shí)時刷新限流參數(shù)及其閾值
目前sentinel dashboard 1.6.2 暫未實(shí)現(xiàn)gateway 流控圖形化控制 , 1.7.0
會增加此功能
上述內(nèi)容就是Spring Cloud中怎么利用Gateway實(shí)現(xiàn)擴(kuò)展支持動態(tài)限流,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:SpringCloud中怎么利用Gateway實(shí)現(xiàn)擴(kuò)展支持動態(tài)限流
網(wǎng)址分享:http://chinadenli.net/article34/ppsose.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、商城網(wǎng)站、標(biāo)簽優(yōu)化、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)