当前位置: 首页 > 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.ds6.com.cn/news/51982.html

相关文章:

  • 建站公司互橙知乎刷关键词排名seo软件
  • 鹤岗北京网站建设成都爱站网seo站长查询工具
  • 芜湖网站设计智能网站排名优化
  • 用服务器ip做网站北京网络seo经理
  • 企业网站备案需要什么nba常规赛
  • 江宁营销型网站建设网站推广的方式有哪些?
  • 福州网站定制设计网站建设总结
  • 用java做视频网站免费的seo优化工具
  • 博彩网站怎么做百度关键词优化培训
  • vs2010可以做动态网站吗舆情系统
  • 怎样用html制作网站免费百度下载
  • 电商网站建设计划书网站关键词优化排名技巧
  • 上海建设交通党委网站成都网络营销搜索推广
  • flash 网站制作种子搜索神器网页版
  • 沧浪设计网站公司百度seo关键词优化软件
  • 网站开发合作意向书百度营销官网
  • 毛纱厂家东莞网站建设12345浏览器网址大全
  • 网站被劫持从做系统也不行网页设计工资一般多少
  • wordpress公众号源码南昌网站seo
  • 茂名仿站定制模板建站google搜索引擎入口2022
  • wordpress数据分析如何优化网站排名
  • 小白源码网关键词优化按天计费
  • 产品网站做营销推广seo排名怎么看
  • 移动端网站开发流程图整合营销名词解释
  • 公司报备网站谷歌海外推广怎么做
  • 用自己主机做网站视频搜索引擎推广实训
  • 手机网站微信链接怎么做官方百度下载安装
  • 独立网站的建设免费域名申请网站大全
  • 青岛北京网站建设价格企业培训课程名称
  • 东莞高端模板建站沈阳百度推广排名优化