中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

哪里有做營銷型網(wǎng)站的公司seo推廣計劃

哪里有做營銷型網(wǎng)站的公司,seo推廣計劃,wordpress更換主題 小工具,收費網(wǎng)站建設(shè)Spring Cloud Gateway實現(xiàn)API訪問頻率限制 一、為什么需要訪問頻率限制?二、使用全局過濾器實現(xiàn)訪問頻率限制步驟:示例代碼: 三、使用特定路由的過濾器實現(xiàn)訪問頻率限制步驟:示例代碼: 四、總結(jié) 在微服務(wù)架構(gòu)中&#x…

Spring Cloud Gateway實現(xiàn)API訪問頻率限制

    • 一、為什么需要訪問頻率限制?
    • 二、使用全局過濾器實現(xiàn)訪問頻率限制
      • 步驟:
      • 示例代碼:
    • 三、使用特定路由的過濾器實現(xiàn)訪問頻率限制
      • 步驟:
      • 示例代碼:
    • 四、總結(jié)

在微服務(wù)架構(gòu)中,API網(wǎng)關(guān)扮演著至關(guān)重要的角色,它不僅負責(zé)路由請求,還能提供諸如安全性、監(jiān)控和限流等功能。Spring Cloud Gateway作為Spring Cloud生態(tài)系統(tǒng)中的一員,提供了強大的路由和過濾功能。本文將詳細介紹如何使用Spring Cloud Gateway的全局過濾器(Global Filters)或特定路由的過濾器(Gateway Filters)來實現(xiàn)對外部接口的訪問頻率限制。

一、為什么需要訪問頻率限制?

訪問頻率限制(Rate Limiting)是保護后端服務(wù)免受惡意或異常流量攻擊的重要手段。通過限制客戶端在一定時間窗口內(nèi)的請求次數(shù),可以有效防止服務(wù)過載,保障系統(tǒng)的穩(wěn)定性和可用性。

二、使用全局過濾器實現(xiàn)訪問頻率限制

全局過濾器適用于對所有路由進行統(tǒng)一的訪問頻率限制。以下是實現(xiàn)這一功能的詳細步驟和示例代碼。

步驟:

  1. 創(chuàng)建一個自定義的全局過濾器: 實現(xiàn)GlobalFilter接口,并在過濾器中實現(xiàn)訪問頻率限制邏輯。

  2. 配置過濾器: 將自定義的全局過濾器注冊到Spring Cloud Gateway中。

示例代碼:

    import org.springframework.cloud.gateway.filter.GatewayFilterChain;import org.springframework.cloud.gateway.filter.GlobalFilter;import org.springframework.core.Ordered;import org.springframework.http.HttpStatus;import org.springframework.stereotype.Component;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicInteger;@Componentpublic class RateLimitGlobalFilter implements GlobalFilter, Ordered {private final ConcurrentHashMap<String, AtomicInteger> requestCounts = new ConcurrentHashMap<>();@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {String ipAddress = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();AtomicInteger count = requestCounts.computeIfAbsent(ipAddress, k -> new AtomicInteger(0));if (count.incrementAndGet() > 10) { // 每秒最多10次請求exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);return exchange.getResponse().setComplete();}return chain.filter(exchange).then(Mono.fromRunnable(() -> {if (count.decrementAndGet() == 0) {requestCounts.remove(ipAddress);}}));}@Overridepublic int getOrder() {return Ordered.LOWEST_PRECEDENCE;}}

三、使用特定路由的過濾器實現(xiàn)訪問頻率限制

特定路由的過濾器適用于對特定路由進行訪問頻率限制。

步驟:

  1. 創(chuàng)建一個自定義的GatewayFilter工廠: 實現(xiàn)GatewayFilterFactory接口,并在工廠中實現(xiàn)訪問頻率限制邏輯。

  2. 配置路由過濾器: 在路由配置中使用自定義的GatewayFilter工廠。

示例代碼:

    import org.springframework.cloud.gateway.filter.GatewayFilter;import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;import org.springframework.http.HttpStatus;import org.springframework.stereotype.Component;import reactor.core.publisher.Mono;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicInteger;@Componentpublic class RateLimitGatewayFilterFactory extends AbstractGatewayFilterFactory<RateLimitGatewayFilterFactory.Config> {private final ConcurrentHashMap<String, AtomicInteger> requestCounts = new ConcurrentHashMap<>();public RateLimitGatewayFilterFactory() {super(Config.class);}@Overridepublic GatewayFilter apply(Config config) {return (exchange, chain) -> {String routeId = exchange.getRequest().getPath().toString();AtomicInteger count = requestCounts.computeIfAbsent(routeId, k -> new AtomicInteger(0));if (count.incrementAndGet() > config.getMaxRequestsPerSecond()) { // 每秒最多config.getMaxRequestsPerSecond()次請求exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);return exchange.getResponse().setComplete();}return chain.filter(exchange).then(Mono.fromRunnable(() -> {if (count.decrementAndGet() == 0) {requestCounts.remove(routeId);}}));};}public static class Config {private int maxRequestsPerSecond;public int getMaxRequestsPerSecond() {return maxRequestsPerSecond;}public void setMaxRequestsPerSecond(int maxRequestsPerSecond) {this.maxRequestsPerSecond = maxRequestsPerSecond;}}}

application.yml中配置路由過濾器:

      cloud:gateway:routes:- id: rate_limited_routeuri: http://example.compredicates:- Path=/rate_limited_pathfilters:- name: RateLimitargs:maxRequestsPerSecond: 10

四、總結(jié)

通過以上步驟和示例代碼,可以在Spring Cloud Gateway中實現(xiàn)對外部接口的訪問頻率限制。根據(jù)具體需求選擇使用全局過濾器或特定路由的過濾器,可以有效保護后端服務(wù)免受異常流量攻擊,提升系統(tǒng)的穩(wěn)定性和可用性。

http://www.risenshineclean.com/news/50330.html

相關(guān)文章:

  • 無錫做網(wǎng)站建設(shè)手機軟文廣告300字
  • 建網(wǎng)站的哪家好農(nóng)產(chǎn)品網(wǎng)絡(luò)營銷策劃書
  • 高清世界街景地圖如何退訂夫唯seo教程
  • 網(wǎng)站建設(shè)完成后如何備案杭州網(wǎng)站推廣與優(yōu)化
  • 如何做電商網(wǎng)站 昆明東莞互聯(lián)網(wǎng)推廣
  • 哪個網(wǎng)站是做旅游B2B的關(guān)鍵詞優(yōu)化排名軟件怎么樣
  • 網(wǎng)頁版微信官網(wǎng)seo優(yōu)化排名教程百度技術(shù)
  • 設(shè)計圖的網(wǎng)站seo哪家強
  • 南寧學(xué)網(wǎng)站建設(shè)自助發(fā)外鏈網(wǎng)站
  • 網(wǎng)站關(guān)鍵字優(yōu)化軟件免費關(guān)鍵詞排名優(yōu)化軟件
  • 做網(wǎng)站前段用什么軟件百度seo排名培訓(xùn) 優(yōu)化
  • 做網(wǎng)站建設(shè)一年能賺多少中國搜索網(wǎng)站排名
  • 青浦手機網(wǎng)站制作seo實戰(zhàn)密碼第三版
  • 河北省住房城鄉(xiāng)建設(shè)廳網(wǎng)站防城港網(wǎng)站seo
  • 影響網(wǎng)站速度嗎網(wǎng)站優(yōu)化哪家好
  • 網(wǎng)頁制作成品網(wǎng)站寧波百度推廣優(yōu)化
  • 鄭州做網(wǎng)站好的公企業(yè)官方網(wǎng)站推廣
  • 自己做外貿(mào)網(wǎng)站站長平臺官網(wǎng)
  • 電腦網(wǎng)站開發(fā)seo發(fā)包技術(shù)教程
  • 企業(yè)網(wǎng)站特點分析與描述百度收錄時間
  • 做網(wǎng)站 提要求win7系統(tǒng)優(yōu)化軟件
  • 做網(wǎng)站原型圖是用什么軟件業(yè)務(wù)網(wǎng)站制作
  • 網(wǎng)站后臺怎么掛廣告 怎么做長沙百度貼吧
  • 天津建設(shè)合同備案網(wǎng)站特大新聞凌晨剛剛發(fā)生
  • 小型玩具企業(yè)網(wǎng)站建設(shè)初期階段任務(wù)服務(wù)器
  • 做網(wǎng)站開發(fā)的網(wǎng)站做外鏈平臺有哪些
  • 上海裝修網(wǎng)官網(wǎng)長沙電商優(yōu)化
  • 洛陽做網(wǎng)站漢獅網(wǎng)絡(luò)seo優(yōu)化是什么意思
  • 微微網(wǎng)站建設(shè)交換友情鏈接推廣法
  • 東莞專業(yè)做網(wǎng)站的公司有哪些seo外包推廣