使用Spring Boot实现限流

使用Spring Boot实现限流

大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!

一、什么是限流?

在分布式系统中,限流是一种重要的技术手段,用于控制系统的请求流量,防止系统因请求过多而导致的资源耗尽、服务质量下降甚至崩溃。限流可以通过各种算法和工具来实现,例如基于令牌桶、漏桶算法等。

二、Spring Boot中的限流实现

在Spring Boot中,我们可以利用现成的库和工具来实现限流功能,下面介绍基于Guava和Redis两种常用的限流实现方式。

1. 基于Guava实现限流

Guava是Google开发的Java核心库,其中包含了许多实用的工具类和集合类,也包括了限流的支持。

首先,添加Guava依赖:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1-jre</version> <!-- 使用最新版本 -->
</dependency>

然后,编写限流的工具类:

package cn.juwatech.limiter;

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.stereotype.Component;

@Component
public class GuavaRateLimiter {

    private RateLimiter rateLimiter = RateLimiter.create(10.0); // 每秒钟生成的令牌数

    public boolean tryAcquire() {
        return rateLimiter.tryAcquire();
    }
}

在需要进行限流的地方注入 GuavaRateLimiter,并调用 tryAcquire 方法来尝试获取令牌,如果获取成功则处理请求,否则进行限流处理。

package cn.juwatech.controller;

import cn.juwatech.limiter.GuavaRateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyController {

    @Autowired
    private GuavaRateLimiter guavaRateLimiter;

    @GetMapping("/test")
    public String test() {
        if (guavaRateLimiter.tryAcquire()) {
            return "Hello, World!";
        } else {
            return "Rate limit exceeded!";
        }
    }
}

2. 基于Redis实现限流

Redis作为一款高性能的缓存和消息中间件,也常被用来实现分布式系统中的限流。

首先,添加Redis依赖和Spring Boot集成的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

然后,配置Redis连接信息和限流工具:

package cn.juwatech.limiter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisRateLimiter {

    private static final String RATE_LIMIT_KEY = "rate_limit_key";
    private static final long LIMIT = 10; // 限流阈值
    private static final long TIMEOUT = 1; // 时间窗口

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public boolean tryAcquire() {
        Long count = redisTemplate.opsForValue().increment(RATE_LIMIT_KEY, 1);
        if (count != null && count <= LIMIT) {
            redisTemplate.expire(RATE_LIMIT_KEY, TIMEOUT, TimeUnit.SECONDS);
            return true;
        } else {
            return false;
        }
    }
}

在需要进行限流的地方注入 RedisRateLimiter,并调用 tryAcquire 方法来尝试获取令牌,实现限流逻辑。

package cn.juwatech.controller;

import cn.juwatech.limiter.RedisRateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyController {

    @Autowired
    private RedisRateLimiter redisRateLimiter;

    @GetMapping("/test")
    public String test() {
        if (redisRateLimiter.tryAcquire()) {
            return "Hello, World!";
        } else {
            return "Rate limit exceeded!";
        }
    }
}

三、总结

通过以上示例,我们实现了基于Guava和Redis两种不同的限流方式。在实际应用中,根据具体的场景和需求选择合适的限流算法和工具,来保护系统免受高并发请求的影响。

著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过自定义注解和AOP实现接口限流。具体实现步骤如下: 1. 定义注解 自定义一个注解,用于标记需要进行限流的方法。例如: ``` @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RateLimit { int value() default 10; // 默认每秒最多处理10个请求 } ``` 2. 实现拦截器 定义一个切面拦截器,拦截被RateLimit注解标记的方法,并根据注解中定义的限流参数进行限流。例如: ``` @Aspect @Component public class RateLimitInterceptor { private final Map<String, Integer> counterMap = new ConcurrentHashMap<>(); @Pointcut("@annotation(com.example.demo.annotation.RateLimit)") public void rateLimit() {} @Around("rateLimit()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); RateLimit rateLimit = method.getAnnotation(RateLimit.class); String methodName = method.getName(); Integer counter = counterMap.get(methodName); if (counter == null) { counter = 0; } if (counter >= rateLimit.value()) { throw new RuntimeException("接口访问频率过高"); } counterMap.put(methodName, counter + 1); try { return joinPoint.proceed(); } finally { counterMap.put(methodName, counter); } } } ``` 3. 配置拦截器 将切面拦截器注册到Spring容器中,并配置AOP自动代理。 ``` @Configuration @EnableAspectJAutoProxy public class RateLimitConfig { @Bean public RateLimitInterceptor rateLimitInterceptor() { return new RateLimitInterceptor(); } } ``` 4. 使用注解 在需要进行限流的方法上添加RateLimit注解,并指定限流参数。例如: ``` @RestController public class UserController { @RateLimit(5) @GetMapping("/users") public List<User> getUsers() { // ... } } ``` 以上就是在Spring Boot中自己实现接口限流的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值