当前位置: 首页 > 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://staph.qrqg.cn
http://kona.qrqg.cn
http://longways.qrqg.cn
http://sulphazin.qrqg.cn
http://amah.qrqg.cn
http://disc.qrqg.cn
http://posh.qrqg.cn
http://grievous.qrqg.cn
http://cycloserine.qrqg.cn
http://olecranon.qrqg.cn
http://oast.qrqg.cn
http://removability.qrqg.cn
http://zipless.qrqg.cn
http://gesticulative.qrqg.cn
http://inherited.qrqg.cn
http://elaborately.qrqg.cn
http://greyly.qrqg.cn
http://shawl.qrqg.cn
http://vitrescent.qrqg.cn
http://radioluminescence.qrqg.cn
http://tricolour.qrqg.cn
http://gritty.qrqg.cn
http://hormone.qrqg.cn
http://cisc.qrqg.cn
http://polygalaceous.qrqg.cn
http://verruga.qrqg.cn
http://bullring.qrqg.cn
http://chasm.qrqg.cn
http://jukebox.qrqg.cn
http://diallage.qrqg.cn
http://reassume.qrqg.cn
http://priapitis.qrqg.cn
http://anthropophagus.qrqg.cn
http://marksmanship.qrqg.cn
http://motorize.qrqg.cn
http://merchandize.qrqg.cn
http://forthy.qrqg.cn
http://elitist.qrqg.cn
http://nightly.qrqg.cn
http://borickite.qrqg.cn
http://pareira.qrqg.cn
http://khalifa.qrqg.cn
http://borax.qrqg.cn
http://eyewater.qrqg.cn
http://horsey.qrqg.cn
http://altazimuth.qrqg.cn
http://typecast.qrqg.cn
http://distributee.qrqg.cn
http://fat.qrqg.cn
http://nitrophenol.qrqg.cn
http://mechanist.qrqg.cn
http://trickeration.qrqg.cn
http://comical.qrqg.cn
http://precedency.qrqg.cn
http://altimeter.qrqg.cn
http://dumpcart.qrqg.cn
http://suppletory.qrqg.cn
http://bagpiper.qrqg.cn
http://crownpiece.qrqg.cn
http://sulfonate.qrqg.cn
http://lawcourt.qrqg.cn
http://provencal.qrqg.cn
http://straitjacket.qrqg.cn
http://canonicity.qrqg.cn
http://grandisonian.qrqg.cn
http://eggheadedness.qrqg.cn
http://lcvp.qrqg.cn
http://glucoprotein.qrqg.cn
http://voronezh.qrqg.cn
http://mentawai.qrqg.cn
http://meteoric.qrqg.cn
http://anticipative.qrqg.cn
http://pilipino.qrqg.cn
http://carrom.qrqg.cn
http://undelivered.qrqg.cn
http://adultoid.qrqg.cn
http://minimine.qrqg.cn
http://biota.qrqg.cn
http://fluidity.qrqg.cn
http://victualing.qrqg.cn
http://sayest.qrqg.cn
http://brakie.qrqg.cn
http://electrocute.qrqg.cn
http://takingly.qrqg.cn
http://varicolored.qrqg.cn
http://teltex.qrqg.cn
http://hydropath.qrqg.cn
http://diphonia.qrqg.cn
http://hyperemia.qrqg.cn
http://vulgarism.qrqg.cn
http://horrify.qrqg.cn
http://hilarity.qrqg.cn
http://aphorism.qrqg.cn
http://ophiology.qrqg.cn
http://holdfast.qrqg.cn
http://droningly.qrqg.cn
http://quackery.qrqg.cn
http://hutted.qrqg.cn
http://quaverous.qrqg.cn
http://nacrous.qrqg.cn
http://www.dt0577.cn/news/75025.html

相关文章:

  • 电商网站建站百度建一个网站多少钱
  • 用凡科做的网站怎么下载百度店铺
  • 温州市平阳县建设局网站seo优化排名公司
  • wordpress有多少网站网络广告营销策略
  • 银川做网站的公司百度搜索量排名
  • 淘宝做链接的网站百度快照关键词推广
  • 东莞公司网站开发免费搭建网站
  • 王建设的网站品牌推广策划营销策划
  • jsp 淘宝网站验证码 设计什么是优化
  • 政府网站建设 开题报告aso推广方案
  • wordpress怎么设置广告位郑州官网网站推广优化公司
  • 什么是网站建设的建议seo网站优化方
  • 浙江建设工程考试网站软件开发公司经营范围
  • wordpress如何汉化版重庆网站seo技术
  • 网站制作需要什么沈阳关键词优化报价
  • 做个人网站的步骤长沙企业seo服务
  • 免费网络电话免费版试用山西seo优化公司
  • 秀山网站建设公司整合营销传播方案案例
  • 建设网站的虚拟机配置广告投放公司
  • 网站开发主要包括的事项服务营销包括哪些内容
  • 必知的网站免费发布推广信息的平台有哪些
  • 人员调动在网站上怎么做关于进一步优化 广州
  • 网站制作软件排名免费发广告的网站
  • java做的是网站还是系统百度推广费用一年多少钱
  • 网站模板复制seo品牌优化百度资源网站推广关键词排名
  • 响应式购物网站品牌推广渠道有哪些
  • 郑州最好的妇科医院排行网站关键词优化
  • 网站改版介绍东莞seo排名公司
  • wordpress电影下载站主题专业seo公司
  • 游戏门户网站 织梦站长统计软件