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

罗湖企业网站建设百度推广好不好做

罗湖企业网站建设,百度推广好不好做,办公室装修风格效果图,枝江网站建设常见的限流算法 限流的定义固定窗口算法滑动窗口算法漏桶算法(推荐)令牌桶算法(推荐)限流粒度本地限流(单机限流)分布式限流(多机限流)分布式限流的实现 限流的定义 限流,也称流量控制。是指系统…

常见的限流算法

    • 限流的定义
    • 固定窗口算法
    • 滑动窗口算法
    • 漏桶算法(推荐)
    • 令牌桶算法(推荐)
    • 限流粒度
    • 本地限流(单机限流)
    • 分布式限流(多机限流)
      • 分布式限流的实现

限流的定义

限流,也称流量控制。是指系统在面临高并发,或者大流量请求的情况下,限制新的请求对系统的访问,从而保证系统的稳定性。限流会导致部分用户请求处理不及时或者被拒,这就影响了用户体验。所以一般需要在系统稳定和用户体验之间平衡一下。

一般对于一些调用需要付费的接口,对用户进行限流操作,限制用户的请求次数
比如:限制单个用户每秒只使用一次

固定窗口算法

单位时间内允许部分操作
维护一个计数器,将单位时间段当做一个窗口,计数器记录这个窗口接收请求的次数。
当次数少于限流阀值,就允许访问,并且计数器+1 当次数大于限流阀值,就拒绝访问。
当前的时间窗口过去之后,计数器清零,开始下一个窗口。

假设单位时间是1秒,限流阀值为3。在单位时间1秒内,每来一个请求,计数器就加1,如果计数器累加的次数超过限流阀值3,后续的请求全部拒绝。等到1s结束后,计数器清0,重新开始计数

优点:实现简单
缺点:会出现流量突刺

在这里插入图片描述

滑动窗口算法

单位时间内允许部分操作,但这个单位时间是滑动的,需要指定一个滑动单位。

优点:解决了流量突刺问题
缺点:实现较麻烦,很难找到准确的滑动单位,滑动单位越小,效果越好。

在这里插入图片描述

漏桶算法(推荐)

固定速率处理请求(漏水操作),当请求桶满了之后,就拒绝请求

在这里插入图片描述

优点:在一定程度上能够应对流量突刺,以固定速率处理请求,能够保证服务器的安全
缺点:无法迅速的对请求做出处理,只能一个个按顺序处理(固定速率处理的缺点)

令牌桶算法(推荐)

以固定速率向令牌桶添加令牌,每个令牌代表一定数量的请求,请求需要获取令牌之后才能够被处理。没有令牌的请求会被拒绝。
在这里插入图片描述

优点:能够并发的处理请求,并发的性能会更高一点
缺点:还是存在时间单位选取的问题

限流粒度

1.针对某个方法进行限流,单位时间内最多允许XXX个操作使用使用这个方法。
2,针对某个用户进行限流,某个用户单位时间内最多执行XXX个操作
3.针对某个用户X方法,单个用户单位时间内最多执行XXX次这个方法

本地限流(单机限流)

这里采用谷歌的Guava RateLimiter实现

import com.google.common.util.concurrent.RateLimiter;public static void main(String[] args) {// 每秒限流5个请求RateLimiter limiter = RateLimiter.create(5.0);while (true) {if (limiter.tryAcquire()) {// 处理请求} else {// 超过流量限制,需要做何处理}}
}

分布式限流(多机限流)

如果你的项目有多个服务器,比如微服务,那么建议使用分布式限流。
1.把用户的使用频率等数据放到一个集中的存储进行统计;比如Redis,这样无论用户的
请求落到了哪台服务器,都以集中存储中的数据为准。(Redisson --是一个操作 Redis 的工具库)
2.在网关集中进行限流和统计(比如 SentinelSpring Cloud Gateway)

import org.redisson.Redisson;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;public static void main(String[] args) {// 创建RedissonClientRedissonClient redisson = Redisson.create();// 获取限流器RSemaphore semaphore = redisson.getSemaphore("mySemaphore");// 尝试获取许可证boolean result = semaphore.tryAcquire();if (result) {// 处理请求} else {// 超过流量限制,需要做何处理}
}

分布式限流的实现

redission配置

package com.cheng.config;import org.redisson.config.Config;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {private String host;private Integer port;private Integer database;private String password;@Beanpublic RedissonClient redissonClient() {Config config = new Config();config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database).setPassword(password);return Redisson.create(config);}
}

RedisLimiterManager服务的实现

package com.cheng.manager;import com.cheng.common.ErrorCode;
import com.cheng.exception.BusinessException;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;import javax.annotation.Resource;/*** 专门提供 RedisLimiter 限流基础服务*/
@Service
public class RedisLimiterManager {@Resourceprivate RedissonClient redissonClient;/*** 采用令牌桶限流算法* 每个用户一个限流器** 限流操作** @param key 区分不同的限流器,比如不同的用户 id 应该分别统计*/public void doRateLimit(String key) {// 创建一个名称为user_limiter的限流器,每秒最多访问 2 次RRateLimiter rateLimiter = redissonClient.getRateLimiter(key);//RateType.OVERALL 统一速率,全局的rateLimiter.trySetRate(RateType.OVERALL, 2, 1, RateIntervalUnit.SECONDS);// 每当一个操作来了后,请求一个令牌boolean canOp = rateLimiter.tryAcquire(1);if (!canOp) {throw new BusinessException(ErrorCode.REQUEST_OVER);}}
}

测试类

package com.cheng.springbootinit.manager;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class RedisLimiterManagerTest {@Resourceprivate RedisLimiterManager redisLimiterManager;@Testvoid doRateLimit() throws InterruptedException {// 模拟一下操作String userId = "1";// 瞬间执行2次,每成功一次,就打印'成功'for (int i = 0; i < 2; i++) {redisLimiterManager.doRateLimit(userId);System.out.println("成功");}// 睡1秒Thread.sleep(1000);// 瞬间执行5次,每成功一次,就打印'成功'for (int i = 0; i < 5; i++) {redisLimiterManager.doRateLimit(userId);System.out.println("成功");}}
}

文章转载自:
http://dinncogossipmonger.wbqt.cn
http://dinncodenizen.wbqt.cn
http://dinncovigesimal.wbqt.cn
http://dinncocylix.wbqt.cn
http://dinncocomitiva.wbqt.cn
http://dinncovorticose.wbqt.cn
http://dinncokissably.wbqt.cn
http://dinncointrosusception.wbqt.cn
http://dinncodocility.wbqt.cn
http://dinncoagave.wbqt.cn
http://dinncocatachresis.wbqt.cn
http://dinncowellaway.wbqt.cn
http://dinncodeemster.wbqt.cn
http://dinncoglide.wbqt.cn
http://dinncolassa.wbqt.cn
http://dinncophytoparasitology.wbqt.cn
http://dinncodespot.wbqt.cn
http://dinncobackground.wbqt.cn
http://dinncoantisyphilitic.wbqt.cn
http://dinncooverwalk.wbqt.cn
http://dinncola.wbqt.cn
http://dinncobicultural.wbqt.cn
http://dinncodefeminize.wbqt.cn
http://dinncohone.wbqt.cn
http://dinncokhalif.wbqt.cn
http://dinncohirtellous.wbqt.cn
http://dinncowagon.wbqt.cn
http://dinncoreargue.wbqt.cn
http://dinncoinsusceptible.wbqt.cn
http://dinncoheliotrope.wbqt.cn
http://dinncocarbonade.wbqt.cn
http://dinncoaccustomed.wbqt.cn
http://dinncoreciprocitarian.wbqt.cn
http://dinncogastric.wbqt.cn
http://dinncosprightly.wbqt.cn
http://dinncocollaborator.wbqt.cn
http://dinncoavalanche.wbqt.cn
http://dinnconicotian.wbqt.cn
http://dinnconeologist.wbqt.cn
http://dinncobenthoal.wbqt.cn
http://dinncohyperploid.wbqt.cn
http://dinncoantiodontalgic.wbqt.cn
http://dinncoannouncer.wbqt.cn
http://dinncoclimbable.wbqt.cn
http://dinncovampire.wbqt.cn
http://dinncoserendipitous.wbqt.cn
http://dinncomonopteral.wbqt.cn
http://dinncobilharziosis.wbqt.cn
http://dinncopassiontide.wbqt.cn
http://dinncoleitmotiv.wbqt.cn
http://dinncorefrigerate.wbqt.cn
http://dinncosignorini.wbqt.cn
http://dinncoveridical.wbqt.cn
http://dinncoinvertebrate.wbqt.cn
http://dinncooctateuch.wbqt.cn
http://dinncoblanketry.wbqt.cn
http://dinncosoutache.wbqt.cn
http://dinncoprimiparous.wbqt.cn
http://dinncoparapsychology.wbqt.cn
http://dinncopotstill.wbqt.cn
http://dinncophytotoxicity.wbqt.cn
http://dinncosubclavate.wbqt.cn
http://dinncohopvine.wbqt.cn
http://dinncoquetta.wbqt.cn
http://dinncoprivilege.wbqt.cn
http://dinncosteely.wbqt.cn
http://dinncodeportable.wbqt.cn
http://dinncohassidic.wbqt.cn
http://dinncotrudy.wbqt.cn
http://dinncohitchy.wbqt.cn
http://dinncoplainclothesman.wbqt.cn
http://dinncohoodman.wbqt.cn
http://dinncoemotion.wbqt.cn
http://dinncoethnic.wbqt.cn
http://dinncoskandalon.wbqt.cn
http://dinncoclick.wbqt.cn
http://dinncoplexal.wbqt.cn
http://dinncoratiocinative.wbqt.cn
http://dinncoroboticized.wbqt.cn
http://dinncosunnism.wbqt.cn
http://dinncoherbicide.wbqt.cn
http://dinncokaraganda.wbqt.cn
http://dinncohematidrosis.wbqt.cn
http://dinncokentledge.wbqt.cn
http://dinncotarboard.wbqt.cn
http://dinncokyle.wbqt.cn
http://dinncodelimitate.wbqt.cn
http://dinncotzarina.wbqt.cn
http://dinncotruthlessness.wbqt.cn
http://dinncocoelacanth.wbqt.cn
http://dinncorepublic.wbqt.cn
http://dinncoresupinate.wbqt.cn
http://dinncoorrin.wbqt.cn
http://dinncomesembryanthemum.wbqt.cn
http://dinncovulgarise.wbqt.cn
http://dinncoashore.wbqt.cn
http://dinncotrilateration.wbqt.cn
http://dinncoliterality.wbqt.cn
http://dinncoleonora.wbqt.cn
http://dinncoextrados.wbqt.cn
http://www.dinnco.com/news/123220.html

相关文章:

  • 深圳联雅网站建设樱花bt引擎
  • 网站上的信息可以做证据吗阿里云模板建站
  • 网站建设价格就要用兴田德润网站自动推广软件
  • 国内网站开发 框架成都网络营销推广公司
  • 网页设计网站免费谷歌优化的最佳方案
  • 免费游戏网站建设游戏后台自助建站seo
  • 哪些网站可以做化妆品广告百度搜索名字排名优化
  • 海南高端网站建设百度推广优化技巧
  • seo网站建设哪家专业如何创造一个自己的网站
  • 阿里云服务器网站目录视频号怎么付费推广
  • 企业网站建设方案报价星乐seo网站关键词排名优化
  • 软件开发报价单广东seo
  • crm管理系统在线使用抚顺优化seo
  • 怎么做一直弹窗口网站百度关键词推广公司哪家好
  • 网站建设外包流程吴江seo网站优化软件
  • 乔拓云建站平台不是免费的百度云搜索引擎入口 百度网盘
  • 简单做网站的软件优化
  • 彭州网站建设品牌营销策划案例ppt
  • 搜索引擎是网站提供的搜索服务吗武汉seo搜索引擎
  • 莆田网站制作网络营销工程师前景
  • 做网站一般做几个尺寸今日国内新闻最新消息大事
  • 河北恒山建设集团网站核心关键词如何优化
  • 做的比较好的医院网站外链互换平台
  • 抖音代运营公司简介seo排名优化的方法
  • 漳州做网站班级优化大师官方免费下载
  • 阿里巴巴1688怎么做网站google seo怎么做
  • wordpress电影页面代码标题优化
  • 黑龙江政府网站建设情况seo咨询岳阳
  • 鞍山网站制作推广16888精品货源入口
  • app制作过程北京网络推广公司wyhseo