当前位置: 首页 > news >正文

做鱫视频网站推广软文

做鱫视频网站,推广软文,对网站访客做简要分析,网站体验调查问卷怎么做我们可以使用策略模式来统一单机限流和分布式限流的实现,提高代码的可扩展性和可维护性。 思路是定义一个 RateLimitStrategy 接口,并分别实现单机限流策略 LocalRateLimitStrategy 和分布式限流策略 DistributedRateLimitStrategy。在 AOP 切面中,根据配置决定使用哪种限流策…

我们可以使用策略模式来统一单机限流和分布式限流的实现,提高代码的可扩展性和可维护性。

思路是定义一个 RateLimitStrategy 接口,并分别实现单机限流策略 LocalRateLimitStrategy 和分布式限流策略 DistributedRateLimitStrategy。在 AOP 切面中,根据配置决定使用哪种限流策略。

定义策略接口

public interface RateLimitStrategy {boolean tryAcquire(String key, double qps, long timeout, TimeUnit timeUnit);
}

实现单机限流策略

import com.google.common.util.concurrent.RateLimiter;import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;public class LocalRateLimitStrategy implements RateLimitStrategy {private final Map<String, RateLimiter> rateLimiters = new ConcurrentHashMap<>();@Overridepublic boolean tryAcquire(String key, double qps, long timeout, TimeUnit timeUnit) {RateLimiter limiter = rateLimiters.computeIfAbsent(key, k -> RateLimiter.create(qps));if (timeout > 0) {return limiter.tryAcquire(timeout, timeUnit);} else {return limiter.tryAcquire();}}
}

实现分布式限流策略

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;public class DistributedRateLimitStrategy implements RateLimitStrategy {private final RedisTemplate<String, Object> redisTemplate;public DistributedRateLimitStrategy(RedisTemplate<String, Object> redisTemplate) {this.redisTemplate = redisTemplate;}@Overridepublic boolean tryAcquire(String key, double qps, long timeout, TimeUnit timeUnit) {long window = timeUnit.toSeconds(timeout);List<String> keys = Collections.singletonList(key);String luaScript = buildLuaScript();RedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript, Long.class);Long currentCount = redisTemplate.execute(redisScript, keys, Collections.singletonList(window), Collections.singletonList(qps));return currentCount <= qps;}private String buildLuaScript() {return "local key = KEYS[1]\n" +"local window = tonumber(ARGV[1])\n" +"local qps = tonumber(ARGV[2])\n" +"local current = redis.call('incrBy', key, 1)\n" +"if current == 1 then\n" +"    redis.call('expire', key, window)\n" +"end\n" +"if current > qps then\n" +"    return redis.call('decrBy', key, 1)\n" +"else\n" +"    return current\n" +"end";}
}

修改切面逻辑

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Aspect
@Component
public class RateLimitAspect {@Autowiredprivate RateLimitStrategy rateLimitStrategy;@Around("@annotation(rateLimitAnnotation)")public Object around(ProceedingJoinPoint joinPoint, RateLimit rateLimitAnnotation) throws Throwable {String key = joinPoint.getSignature().toLongString();double qps = rateLimitAnnotation.qps();long timeout = rateLimitAnnotation.timeout();TimeUnit timeUnit = rateLimitAnnotation.timeUnit();boolean acquired = rateLimitStrategy.tryAcquire(key, qps, timeout, timeUnit);if (!acquired) {throw new RuntimeException("Rate limit exceeded");}return joinPoint.proceed();}
}

在切面逻辑中,我们注入了 RateLimitStrategy 的实现类。根据配置决定使用单机限流还是分布式限流策略。

使用示例

@RestController
public class DemoController {@Autowiredprivate RateLimitStrategy rateLimitStrategy;@GetMapping("/test")@ApiRateLimit(qps = 10, timeout = 60, timeUnit = TimeUnit.SECONDS)public String test() {return "hello world";}
}

在使用时,我们只需要在方法上标注 @RateLimit 注解即可,而不需要关心底层使用的是单机限流还是分布式限流。

配置限流策略

在 Spring 配置中,我们可以根据需求注入不同的 RateLimitStrategy 实现类:

// 单机限流配置
@Bean
public RateLimitStrategy localRateLimitStrategy() {return new LocalRateLimitStrategy();
}// 分布式限流配置
@Bean
public RateLimitStrategy distributedRateLimitStrategy(RedisTemplate<String, Object> redisTemplate) {return new DistributedRateLimitStrategy(redisTemplate);
}

通过使用策略模式,我们将限流算法与具体的限流策略解耦,提高了代码的可扩展性和可维护性。未来如果需要新的限流策略,只需要实现 RateLimitStrategy 接口并配置即可,无需修改核心的限流逻辑。

http://www.dinnco.com/news/60378.html

相关文章:

  • 蒙牛网站是谁做的营销网站建设免费
  • 设计用哪些网站有哪些功能什么是网络推广员
  • vs2010 c 网站开发网络营销的5种方式
  • 时间轴网站代码新浪博客seo
  • 网站建设岗位有哪些品牌营销网站建设
  • 个人合法网站怎么做排名优化网站
  • 永兴县网站建设公司哪家好seo关键词大搜
  • 京推推cms网站建设北京seo服务行者
  • 做网站换域名小吃培训机构排名前十
  • 沈阳学习做网站网络运营具体做什么
  • 小制作小发明视频教程安徽搜索引擎优化
  • 黑客如何攻击网站友情链接获取的途径有哪些
  • 凡科网做网站视频拉新平台哪个好佣金高
  • 企业网站建设对网络营销有哪些影响百度免费推广网站
  • html5网络公司网站模板北京网络推广公司
  • 蛟河市建设局网站百度法务部联系方式
  • 网站开发总结武汉网站排名推广
  • 开发安卓应用惠州网站seo
  • 华为商城官网手机版广州seo网站推广优化
  • 做啪啪网站全网推广推荐
  • 做县城门户网站游戏推广员好做吗
  • 网站营销单页面留言网站优化课程
  • 哪些网站能够免费做公考题seo下拉优化
  • 河北邢台做移动网站优化大师手机版下载
  • 大亚湾住房和规划建设局网站知识营销案例
  • 宝安网站-建设深圳信科免费友链平台
  • DS716 II 做网站公司网站怎么注册
  • 长宁网站建设色盲测试图片
  • 佛山市建设工程交易中心网站站长工具seo综合查询
  • 网页设计与制作的三个阶段成都网站搜索排名优化公司