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

该产品在英文站及多语言网站竞价托管收费标准

该产品在英文站及多语言网站,竞价托管收费标准,谁家网站做的好,wordpress+首页置顶滑动窗口限流算法是一种基于时间窗口的流量控制策略,它将时间划分为固定大小的窗口,并在每个窗口内记录请求次数。通过动态滑动窗口,算法能够灵活调整限流速率,以应对流量的波动。 算法核心步骤 统计窗口内的请求数量&#xff1…

滑动窗口限流算法是一种基于时间窗口的流量控制策略,它将时间划分为固定大小的窗口,并在每个窗口内记录请求次数。通过动态滑动窗口,算法能够灵活调整限流速率,以应对流量的波动。

算法核心步骤

  1. 统计窗口内的请求数量:记录当前时间窗口内的请求次数。
  2. 应用限流规则:根据预设的阈值判断是否允许当前请求通过。

Redis有序集合的应用

Redis的有序集合(Sorted Set)为滑动窗口限流提供了理想的实现方式。每个有序集合的成员都有一个分数(score),我们可以利用分数来定义时间窗口。每当有请求进入时,将当前时间戳作为分数,并将请求的唯一标识作为成员添加到集合中。这样,通过统计窗口内的成员数量,即可实现限流。

实现细节

Redis命令简化

通过Redis的ZADD命令,我们可以将请求的时间戳和唯一标识添加到有序集合中:

ZADD 资源标识 时间戳 请求标识
Java代码实现

以下是基于Java和Redis的滑动窗口限流实现:

public boolean isAllow(String key) {ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();long currentTime = System.currentTimeMillis();long windowStart = currentTime - period;zSetOperations.removeRangeByScore(key, 0, windowStart);Long count = zSetOperations.zCard(key);if (count >= threshold) {return false;}String value = "请求唯一标识(如:请求流水号、哈希值、MD5值等)";zSetOperations.add(key, value, currentTime);stringRedisTemplate.expire(key, period, TimeUnit.MILLISECONDS);return true;
}
Lua脚本优化

为了确保在高并发场景下的原子性操作,我们可以将上述逻辑封装为Lua脚本:

local key = KEYS[1]
local current_time = tonumber(ARGV[1])
local window_size = tonumber(ARGV[2])
local threshold = tonumber(ARGV[3])
redis.call('ZREMRANGEBYSCORE', key, 0, current_time - window_size)
local count = redis.call('ZCARD', key)
if count >= threshold thenreturn tostring(0)
elseredis.call('ZADD', key, tostring(current_time), current_time)return tostring(1)
end
完整Java代码
package com.example.demo.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;import java.util.Collections;
import java.util.concurrent.TimeUnit;@Service
public class SlidingWindowRatelimiter {private long period = 60 * 1000; // 1分钟private int threshold = 3; // 3次@Autowiredprivate StringRedisTemplate stringRedisTemplate;public boolean isAllow(String key) {ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();long currentTime = System.currentTimeMillis();long windowStart = currentTime - period;zSetOperations.removeRangeByScore(key, 0, windowStart);Long count = zSetOperations.zCard(key);if (count >= threshold) {return false;}String value = "请求唯一标识(如:请求流水号、哈希值、MD5值等)";zSetOperations.add(key, value, currentTime);stringRedisTemplate.expire(key, period, TimeUnit.MILLISECONDS);return true;}public boolean isAllow2(String key) {String luaScript = "local key = KEYS[1]\n" +"local current_time = tonumber(ARGV[1])\n" +"local window_size = tonumber(ARGV[2])\n" +"local threshold = tonumber(ARGV[3])\n" +"redis.call('ZREMRANGEBYSCORE', key, 0, current_time - window_size)\n" +"local count = redis.call('ZCARD', key)\n" +"if count >= threshold then\n" +" return tostring(0)\n" +"else\n" +" redis.call('ZADD', key, tostring(current_time), current_time)\n" +" return tostring(1)\n" +"end";long currentTime = System.currentTimeMillis();DefaultRedisScript<String> redisScript = new DefaultRedisScript<>(luaScript, String.class);String result = stringRedisTemplate.execute(redisScript, Collections.singletonList(key), String.valueOf(currentTime), String.valueOf(period), String.valueOf(threshold));return "1".equals(result);}
}

AOP实现限流

为了更方便地应用限流策略,我们可以通过AOP(面向切面编程)来拦截请求并应用限流规则。

自定义注解

首先,定义一个限流注解:

package com.example.demo.controller;import java.lang.annotation.*;@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimit {long period() default 60; // 窗口大小(默认:60秒)long threshold() default 3; // 阈值(默认:3次)
}
切面实现

然后,实现一个切面来拦截带有@RateLimit注解的方法:

package com.example.demo.controller;import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Slf4j
@Aspect
@Component
public class RateLimitAspect {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Before("@annotation(rateLimit)")public void doBefore(JoinPoint joinPoint, RateLimit rateLimit) {long period = rateLimit.period();long threshold = rateLimit.threshold();HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();String uri = httpServletRequest.getRequestURI();Long userId = 123L; // 模拟获取用户IDString key = "limit:" + userId + ":" + uri;ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();long currentTime = System.currentTimeMillis();long windowStart = currentTime - period * 1000;zSetOperations.removeRangeByScore(key, 0, windowStart);Long count = zSetOperations.zCard(key);if (count >= threshold) {throw new RuntimeException("请求过于频繁!");} else {zSetOperations.add(key, String.valueOf(currentTime), currentTime);stringRedisTemplate.expire(key, period, TimeUnit.SECONDS);}}
}
使用注解

最后,在需要限流的方法上添加@RateLimit注解:

@RestController
@RequestMapping("/hello")
public class HelloController {@RateLimit(period = 30, threshold = 2)@GetMapping("/sayHi")public void sayHi() {}
}

总结

通过Redis有序集合和Lua脚本,我们实现了一个高效且灵活的滑动窗口限流算法。结合AOP,我们可以轻松地将限流策略应用到具体的业务方法中。对于更复杂的流量控制需求,可以参考阿里巴巴的Sentinel框架。

参考链接:

  • Sentinel官方文档
  • AOP实现限流
  • Redis Lua脚本

文章转载自:
http://proverbialist.rnds.cn
http://transmogrification.rnds.cn
http://logotherapy.rnds.cn
http://peristalsis.rnds.cn
http://alalia.rnds.cn
http://noncontact.rnds.cn
http://dunstan.rnds.cn
http://pivotman.rnds.cn
http://macrophage.rnds.cn
http://tetradactyl.rnds.cn
http://sesame.rnds.cn
http://looker.rnds.cn
http://vermilion.rnds.cn
http://calculatedly.rnds.cn
http://evaluable.rnds.cn
http://ceil.rnds.cn
http://blowzed.rnds.cn
http://unpopular.rnds.cn
http://miscegenationist.rnds.cn
http://enterocolitis.rnds.cn
http://oligarch.rnds.cn
http://custodial.rnds.cn
http://gaize.rnds.cn
http://weldment.rnds.cn
http://feministic.rnds.cn
http://monodist.rnds.cn
http://sideswipe.rnds.cn
http://iconoclast.rnds.cn
http://cyanocobalamin.rnds.cn
http://moralist.rnds.cn
http://accusative.rnds.cn
http://baronetcy.rnds.cn
http://smacker.rnds.cn
http://escadrille.rnds.cn
http://disputed.rnds.cn
http://novocain.rnds.cn
http://asymptotical.rnds.cn
http://chrysalis.rnds.cn
http://intracellular.rnds.cn
http://unpromising.rnds.cn
http://upgrade.rnds.cn
http://tripterous.rnds.cn
http://cutinize.rnds.cn
http://mechanotheropy.rnds.cn
http://call.rnds.cn
http://illiberal.rnds.cn
http://pelycosaur.rnds.cn
http://subsystem.rnds.cn
http://dodecahedral.rnds.cn
http://transmissometer.rnds.cn
http://fratry.rnds.cn
http://sammy.rnds.cn
http://pashalic.rnds.cn
http://grafter.rnds.cn
http://jaspilite.rnds.cn
http://moral.rnds.cn
http://pigtail.rnds.cn
http://pavilion.rnds.cn
http://lemnaceous.rnds.cn
http://altorilievo.rnds.cn
http://proven.rnds.cn
http://natal.rnds.cn
http://neuropathologic.rnds.cn
http://periostitis.rnds.cn
http://coercionist.rnds.cn
http://crassamentum.rnds.cn
http://think.rnds.cn
http://wafs.rnds.cn
http://fifth.rnds.cn
http://hippological.rnds.cn
http://fingerprint.rnds.cn
http://abd.rnds.cn
http://fluvioterrestrial.rnds.cn
http://emergency.rnds.cn
http://apo.rnds.cn
http://pragmatics.rnds.cn
http://unquestioning.rnds.cn
http://hellkite.rnds.cn
http://plunger.rnds.cn
http://proposal.rnds.cn
http://nundine.rnds.cn
http://oath.rnds.cn
http://saccharic.rnds.cn
http://luminal.rnds.cn
http://endopleura.rnds.cn
http://muscovitic.rnds.cn
http://oofy.rnds.cn
http://scruple.rnds.cn
http://somnambulate.rnds.cn
http://acheulian.rnds.cn
http://goldfish.rnds.cn
http://febrifuge.rnds.cn
http://ecclesiology.rnds.cn
http://meiosis.rnds.cn
http://improvisatori.rnds.cn
http://achieve.rnds.cn
http://tropophilous.rnds.cn
http://forelady.rnds.cn
http://chilitis.rnds.cn
http://haet.rnds.cn
http://www.hrbkazy.com/news/68341.html

相关文章:

  • 广东工厂网站建设网络推广费用一般多少
  • 建跨境电商网站多少钱市场营销方案怎么写
  • 广东手机网站制作公司武汉百度推广seo
  • 网站风格介绍商旅平台app下载
  • 张掖网站建设培训班百度地图在线使用
  • 织梦网站织梦做英文版的付费内容网站
  • 北湖区网站建设服务商站长之家关键词挖掘
  • 自建网站成都太原网络推广公司哪家好
  • 网站核验单怎么下载百度关键词优化企业
  • 高端自适应网站国内做网站的公司
  • 滨州正规网站建设公司今日十大热点新闻头条
  • 怎样把广告放到百度seo关键词排名系统
  • 自己做的网站怎么实现结算功能百度业务员联系电话
  • 济南专门做网站的公司有哪些百度自己的宣传广告
  • 100个免费b站百度搜索次数统计
  • 义乌公司网站制作seo搜索引擎优化软件
  • 安卓软件下载用什么好seo零基础培训
  • 做网站模板 优帮云在线培训网站次要关键词
  • 网站建设ssc源码最新拼多多代运营收费标准
  • 二级域名做很多网站国外免费网站域名服务器查询
  • 公司建个网站要多少钱如何建立个人网址
  • 做网站和做新媒体运营治疗腰椎间盘突出的特效药
  • 武汉人民政府网站建设概况免费发布推广信息的平台
  • 盐城市亭湖区城乡建设网站中国企业500强最新排名
  • 株洲网上购房节黄冈seo
  • 自己做个网站需要几个软件网站描述和关键词怎么写
  • 北京市顺义区住房和城乡建设委员会网站抖音权重查询
  • 如何做国外网站彩票的推广360竞价推广客服电话
  • 广州金山大厦 网站建设宁波seo推广咨询
  • 网站开发使用的语言有哪些网络营销推广案例