溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot中實(shí)現(xiàn)API限流與熔斷

發(fā)布時(shí)間:2024-10-05 08:15:02 來(lái)源:億速云 閱讀:81 作者:小樊 欄目:編程語(yǔ)言

在Spring Boot中,我們可以使用一些現(xiàn)成的庫(kù)來(lái)實(shí)現(xiàn)API限流和熔斷功能。這里我們將介紹如何使用Guava RateLimiter實(shí)現(xiàn)限流,以及如何使用Hystrix實(shí)現(xiàn)熔斷。

  1. 使用Guava RateLimiter實(shí)現(xiàn)限流

首先,需要在項(xiàng)目中引入Guava依賴(lài):

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1-jre</version>
</dependency>

接下來(lái),創(chuàng)建一個(gè)配置類(lèi),用于初始化RateLimiter:

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RateLimiterConfig {

    @Bean
    public RateLimiter rateLimiter() {
        return RateLimiter.create(1); // 設(shè)置每秒最多處理1個(gè)請(qǐng)求
    }
}

現(xiàn)在,可以在Controller中使用@RateLimited注解來(lái)實(shí)現(xiàn)限流:

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @Autowired
    private RateLimiter rateLimiter;

    @GetMapping("/api")
    public String api() {
        if (!rateLimiter.tryAcquire()) {
            return "Too many requests, please try again later.";
        }
        return "Hello, world!";
    }
}
  1. 使用Hystrix實(shí)現(xiàn)熔斷

首先,需要在項(xiàng)目中引入Hystrix依賴(lài):

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

接下來(lái),創(chuàng)建一個(gè)配置類(lèi),用于啟用Hystrix并定義熔斷方法:

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HystrixConfig {

    @Bean
    public HelloWorldCommand helloWorldCommand() {
        return new HelloWorldCommand();
    }
}

創(chuàng)建一個(gè)實(shí)現(xiàn)HystrixCommand接口的類(lèi):

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

public class HelloWorldCommand extends HystrixCommand<String> {

    public HelloWorldCommand() {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
    }

    @Override
    protected String run() {
        // 調(diào)用遠(yuǎn)程服務(wù)或執(zhí)行業(yè)務(wù)邏輯
        return "Hello, world!";
    }

    @Override
    protected String getFallback() {
        // 返回熔斷時(shí)的備選方案
        return "Service is unavailable, please try again later.";
    }
}

現(xiàn)在,可以在Controller中使用@HystrixCommand注解來(lái)實(shí)現(xiàn)熔斷:

import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @GetMapping("/api")
    @HystrixCommand(fallbackMethod = "helloWorldFallback")
    public String api() {
        return new HelloWorldCommand().execute();
    }

    public String helloWorldFallback() {
        return "Service is unavailable, please try again later.";
    }
}

這樣,當(dāng)HelloWorldCommand執(zhí)行超時(shí)時(shí),將會(huì)調(diào)用helloWorldFallback方法作為備選方案。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI