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

京东商城商务网站建设目的白山网络推广

京东商城商务网站建设目的,白山网络推广,wordpress文章url原理,义乌做网站多少钱我们可以使用策略模式来统一单机限流和分布式限流的实现,提高代码的可扩展性和可维护性。 思路是定义一个 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.hkea.cn/news/70025/

相关文章:

  • 最新体育新闻足球百度seo收费
  • 手机网站做跳转好吗个人在百度上发广告怎么发
  • 民宿网站的建设最近热搜新闻事件
  • 企业网站建设的核心是企业推广视频
  • 设计素材网站蜂产品推广文章
  • wordpress站点描述seo哪个软件好
  • 澳门服务器做网站需要备案吗百度ai人工智能平台
  • 做化验的在哪个网站里投简历河南网站关键词优化
  • 百度网址大全网站大全网络整合营销方案ppt
  • 海阳市建设工程交易中心网站品牌推广的作用
  • 江西省住房和城乡建设网站成都网站优化seo
  • java资源网站云优化
  • 小程序源码大全网络seo关键词优化技巧
  • 服务佳的小企业网站建设ip子域名大全
  • 网页与制作唐山seo推广公司
  • 自己做的网站怎么弄到网上在线网页制作
  • 电商网站 设计方案百度的排名规则详解
  • 福建省建设厅网站余外链链接平台
  • 广告营销网站市场推广方案
  • 徐州企业做网站软文是什么文章
  • 网站代码备份如何优化seo
  • 百度网站公司信息推广怎么做天津做网站的网络公司
  • wordpress在线pdfseo百度站长工具查询
  • 太仓网站建设有限公司网站设计公司怎么样
  • 网站去哪做在线crm软件
  • 做360手机网站快速汕头seo排名收费
  • 网站建设总做总结宜兴百度推广公司
  • 做毕业网站的周记外贸建站优化
  • 南昌市住房和城乡建设局网站百度官网推广平台电话
  • 真人做视频网站百度怎么发布广告