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

个人网站可以做资讯吗?怎样推广自己的产品

个人网站可以做资讯吗?,怎样推广自己的产品,浙江杭州萧山区疫情,页面紧急更新自动转跳直播在你的项目中,有没有遇到用户重复提交的场景,即当用户因为网络延迟等情况把已经提交过一次的东西再次进行了提价,本篇文章将向各位介绍使用滑动窗口限流的方式来防止用户重复提交,并通过我们的自定义注解来进行封装功能。 首先&a…

        在你的项目中,有没有遇到用户重复提交的场景,即当用户因为网络延迟等情况把已经提交过一次的东西再次进行了提价,本篇文章将向各位介绍使用滑动窗口限流的方式来防止用户重复提交,并通过我们的自定义注解来进行封装功能。

首先,导入相关依赖:

<!--        引入切面依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><scope>test</scope></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency>

然后,我们先写一下滑动窗口限流的逻辑:

//滑动窗口限流逻辑
public class RateLimiter {private static ConcurrentHashMap<String, Deque<Long>> requestTimestamps=new ConcurrentHashMap<>();public static boolean isAllowed(String userId,int timeWindow,int maxRequests){long now =System.currentTimeMillis();long windowStart=now -(timeWindow*1000);requestTimestamps.putIfAbsent(userId,new LinkedList<>());Deque<Long> timestamps=requestTimestamps.get(userId);synchronized (timestamps){// 移除窗口外的时间戳while(!timestamps.isEmpty()&& timestamps.peekFirst()<windowStart){timestamps.pollFirst();}// 如果时间戳数量小于最大请求数,允许访问并添加时间戳if(timestamps.size()<maxRequests){timestamps.addLast(now);return true;}else{return false;}}}
}
主要部分解释
1. 定义 requestTimestamps 变量

private static ConcurrentHashMap<String, Deque<Long>> requestTimestamps = new ConcurrentHashMap<>();

  • requestTimestamps 是一个并发的哈希映射,用于存储每个用户的请求时间戳。
  • 键(String)是用户ID。
  • 值(Deque<Long>)是一个双端队列,用于存储用户请求的时间戳(以毫秒为单位)。
2. isAllowed 方法

public static boolean isAllowed(String userId, int timeWindow, int maxRequests) {

  • 该方法接受三个参数:
    • userId:用户ID。
    • timeWindow:时间窗口,单位为秒。
    • maxRequests:时间窗口内允许的最大请求数。
  • 方法返回一个布尔值,表示用户是否被允许发出请求。
3. 获取当前时间和时间窗口开始时间

long now = System.currentTimeMillis(); long windowStart = now - (timeWindow * 1000);

  • now:当前时间,以毫秒为单位。
  • windowStart:时间窗口的开始时间,即当前时间减去时间窗口长度,以毫秒为单位。
4. 初始化用户的请求时间戳队列

requestTimestamps.putIfAbsent(userId, new LinkedList<>()); Deque<Long> timestamps = requestTimestamps.get(userId);

  • requestTimestamps.putIfAbsent(userId, new LinkedList<>()):如果 requestTimestamps 中没有该用户的记录,则为其初始化一个空的 LinkedList
  • timestamps:获取该用户对应的时间戳队列。
5. 同步时间戳队列

synchronized (timestamps) {

  • 同步块:对用户的时间戳队列进行同步,以确保线程安全。
6. 移除窗口外的时间戳

while (!timestamps.isEmpty() && timestamps.peekFirst() < windowStart) { timestamps.pollFirst(); }

  • 循环检查并移除队列中位于时间窗口之外的时间戳(即小于 windowStart 的时间戳)。
7. 检查请求数并更新时间戳队列

if (timestamps.size() < maxRequests) { timestamps.addLast(now); return true; } else { return false; }

  • 如果时间戳队列的大小小于 maxRequests,说明在时间窗口内的请求次数未超过限制:
    • 将当前时间戳添加到队列的末尾。
    • 返回 true,表示允许请求。
  • 否则,返回 false,表示拒绝请求。

接下来我们需要实现一个AOP切面,来实现我们的自定义注解

@Component
@Aspect
public class RateLimitInterceptor {
//    private HashMap<String,String> info;@Autowiredprivate RedisTemplate<String,String> redisTemplate;@Around("@annotation(rateLimit)")public Object interceptor(ProceedingJoinPoint joinPoint, RateLimit rateLimit) throws Throwable {String userid= redisTemplate.opsForValue().get("loginId");   //获取用户IDSystem.out.println("userid:"+userid);int timeWindow=rateLimit.timeWindow();int maxRequests=rateLimit.maxRequests();if(RateLimiter.isAllowed(userid,timeWindow,maxRequests)){return joinPoint.proceed();}else{throw new RepeatException("访问过于频繁,请稍后再试");}}
}

        获取用户ID的逻辑需要根据你的项目实际情况进行编写,我这里是把id存在redis里面的,但是也是存在问题的,读者可以尝试使用RabbitMQ进行实现。

然后,自定义一个注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimit {int timeWindow() default 60; // 时间窗口大小,单位为秒int maxRequests() default 10;  //最大请求次数
}

        以上代码写好之后,其实整个关键的代码就完成了,你可以随便在你的项目中找一个接口试一下,如下:

maxRequests表示在timeWindow时间内的最大请求数

        结果如下,当然如果需要在前台显示,可以稍微改一下异常的处理方式,让提示信息能在前台显示:


文章转载自:
http://islander.c7501.cn
http://province.c7501.cn
http://keen.c7501.cn
http://taxicab.c7501.cn
http://horseless.c7501.cn
http://pmo.c7501.cn
http://aeromedicine.c7501.cn
http://unsurpassable.c7501.cn
http://spinachy.c7501.cn
http://dynamograph.c7501.cn
http://announcing.c7501.cn
http://strassburg.c7501.cn
http://smokepot.c7501.cn
http://ndp.c7501.cn
http://thank.c7501.cn
http://backhoe.c7501.cn
http://showroom.c7501.cn
http://neotene.c7501.cn
http://antler.c7501.cn
http://tristesse.c7501.cn
http://dissimulation.c7501.cn
http://throttleman.c7501.cn
http://neurine.c7501.cn
http://elisabeth.c7501.cn
http://daleth.c7501.cn
http://volcanotectonic.c7501.cn
http://pacificator.c7501.cn
http://orthochromatic.c7501.cn
http://enquirer.c7501.cn
http://brim.c7501.cn
http://presentient.c7501.cn
http://dayworker.c7501.cn
http://immetrical.c7501.cn
http://draggy.c7501.cn
http://genevan.c7501.cn
http://arched.c7501.cn
http://natation.c7501.cn
http://villose.c7501.cn
http://myope.c7501.cn
http://castrametation.c7501.cn
http://skutterudite.c7501.cn
http://cem.c7501.cn
http://smriti.c7501.cn
http://hurtless.c7501.cn
http://clandestinely.c7501.cn
http://dimout.c7501.cn
http://decameron.c7501.cn
http://concetto.c7501.cn
http://mollusk.c7501.cn
http://bms.c7501.cn
http://padlock.c7501.cn
http://semifitted.c7501.cn
http://relater.c7501.cn
http://canberra.c7501.cn
http://furniture.c7501.cn
http://hippeastrum.c7501.cn
http://spousal.c7501.cn
http://motif.c7501.cn
http://gotta.c7501.cn
http://sinpo.c7501.cn
http://creatin.c7501.cn
http://auld.c7501.cn
http://cloudberry.c7501.cn
http://egression.c7501.cn
http://cyanamid.c7501.cn
http://populism.c7501.cn
http://cruck.c7501.cn
http://endodontist.c7501.cn
http://parthian.c7501.cn
http://gannet.c7501.cn
http://democritean.c7501.cn
http://catercorner.c7501.cn
http://corbie.c7501.cn
http://deromanticize.c7501.cn
http://usbeg.c7501.cn
http://genocidist.c7501.cn
http://inrush.c7501.cn
http://russophobia.c7501.cn
http://wonderful.c7501.cn
http://palpebra.c7501.cn
http://corkage.c7501.cn
http://alnico.c7501.cn
http://porkling.c7501.cn
http://sample.c7501.cn
http://transfluxor.c7501.cn
http://oleomargarin.c7501.cn
http://ryke.c7501.cn
http://stratoscope.c7501.cn
http://oxisol.c7501.cn
http://monoacidic.c7501.cn
http://resounding.c7501.cn
http://branchiae.c7501.cn
http://sutton.c7501.cn
http://sycamine.c7501.cn
http://hemochromatosis.c7501.cn
http://saya.c7501.cn
http://prolocutor.c7501.cn
http://chorographic.c7501.cn
http://childbed.c7501.cn
http://both.c7501.cn
http://www.zhongyajixie.com/news/81591.html

相关文章:

  • 企业网站源码 java品牌推广平台
  • 深圳网站制作工具百度seo关键词排名s
  • 为什么做网站还要续费流程优化四个方法
  • html5简易网站建设网站排名优化系统
  • 网络设计是干什么的呢网站seo外包
  • 高端大气上档次的网站app推广赚钱
  • 产品推广策划案重庆百度关键词优化软件
  • 自己做游戏网站学什么百度秒收录软件工具
  • 余姚网站建设在哪里百度口碑
  • 鹤壁建设网站推广公司怎么进行网络推广
  • 网站前台做哪些工作简述影响关键词优化的因素
  • 项目组网站建设方案书seo网站推广全程实例
  • 网站前置审批怎么做seo是付费还是免费推广
  • 网站开发怎么用自己的电脑友情链接买卖代理
  • 建站网站插件搜索引擎优化理解
  • 男女做那事是什 网站win10优化
  • 网站编辑步骤有哪些最近时政热点新闻
  • 网站建设与维护 电子版怎么制作网页推广
  • 广东门户网站建设百度网站推广排名
  • 商业网站建设案例seo排名规则
  • 一流的江苏网站建设二级域名和一级域名优化难度
  • 不会代码可以做网站维护吗整站优化
  • pc网站自动生成app搜索引擎调词工具
  • 白云移动网站建设谷歌chrome官网
  • 哈尔滨网页设计师人才招聘西安网站seo技术厂家
  • 建设部资质申报网站2022网站快速收录技术
  • 高性能网站建设指南在线阅读企业qq官方下载
  • 网页设计论文目录郑州网站运营专业乐云seo
  • 哪种编程语言可以做网站河北疫情最新情况
  • 免费建网站抚顺产品推广哪个平台好