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

调查网站做调查不容易过企业网络营销策划

调查网站做调查不容易过,企业网络营销策划,工作蒸蒸日上,企业网站推广毕业论文答辩⛰️个人主页: 蒾酒 🔥系列专栏:《spring boot实战》 🌊山高路远,行路漫漫,终有归途 目录 写在前面 内容简介 实现思路 实现步骤 1.自定义限流注解 2.编写限流拦截器 3.注册拦截器 4.接口限流测试 写在前…

 

⛰️个人主页:     蒾酒

🔥系列专栏:《spring boot实战》

🌊山高路远,行路漫漫,终有归途


目录

写在前面

内容简介

实现思路

实现步骤

1.自定义限流注解

2.编写限流拦截器

3.注册拦截器

4.接口限流测试


写在前面

本文介绍了springboot开发后端服务中,高并发接口限流设计与实现,坚持看完相信对你有帮助。

同时欢迎订阅springboot系列专栏,持续分享spring boot的使用经验。

内容简介

本文介绍了一种使用自定义注解结合拦截器和redis实现接口限流方法。这种方法也是企业常用方法,是一种比较优雅的解决方案。

优点分析

  1. 灵活性和可定制性: 通过自定义注解和拦截器,可以根据具体的业务需求灵活定义限流规则,满足不同接口的限流需求。

  2. 性能优化: 使用Redis等高性能缓存数据库存储限流计数器,能够有效减轻应用程序的压力,提高系统的性能表现。

  3. 实时性和持久性: Redis具有较高的读写性能,可以实时更新限流计数器,并且数据持久化,保证限流规则的持久性。

  4. 分布式支持: 对于分布式系统,使用Redis等分布式缓存数据库可以方便地实现跨节点的限流策略和计数器共享,确保限流的准确性和一致性。

  5. 成熟稳定: 这种方法经过实践验证,在众多企业项目中得到广泛应用,被认为是一种成熟、稳定且可靠的解决方案。

实现思路

通过自定义一个注解标注需要进行限流的接口方法,通过拦截器对标记改注解的方法进行拦截处理

将同一ip访问同一接口的次数缓存到redis,拦截器中进行判断处理,达到访问阈值直接拒绝。

实现步骤

1.自定义限流注解

import java.lang.annotation.*;/*** @author mijiupro*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Inherited
@Documented
public @interface AccessLimit {int limit() default 10; // 限流阈值int seconds() default 60; // 时间窗口
}

2.编写限流拦截器

/*** 接口限流拦截器* @author mijiupro*/
@Slf4j
@Component
public class AccessLimitInterceptor implements HandlerInterceptor {private final StringRedisTemplate redisTemplate;public AccessLimitInterceptor(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}@Overridepublic boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler)  {if (!(handler instanceof HandlerMethod handlerMethod)) {return true;}Method method = handlerMethod.getMethod();AccessLimit accessLimit = method.getAnnotation(AccessLimit.class);// 若方法上没有AccessLimit注解,直接放行if (accessLimit == null) {return true;}int limit = accessLimit.limit();int seconds = accessLimit.seconds();String key = generateKey(request); // 生成限流key// 使用基本类型long接收计数值,并确保不会因自动装箱产生NullPointerExceptionLong countResult = redisTemplate.opsForValue().increment(key, 1);long currentCount = countResult != null ? countResult : 0;if (currentCount == 1) {// 如果是第一次访问,设置过期时间redisTemplate.expire(key, seconds, TimeUnit.SECONDS);log.debug("设置访问限制计数为1:{}", key);return true;}if (currentCount > limit) {log.error("访问超过限制:{}", key);throw new RateLimitException(ResultEnum.ACCESS_LIMIT_REACHED);}log.debug("访问限制计数递增:{}", key);return true;}private String generateKey(HttpServletRequest request) {// 组合key的方式可以根据实际业务需要调整,例如考虑方法名称、用户ID等return request.getRemoteAddr() + ":" + request.getContextPath() + ":" + request.getServletPath();}}

3.注册拦截器

@Configuration
public class WebConfig implements WebMvcConfigurer {private final AccessLimitInterceptor accessLimitInterceptor;public WebConfig( AccessLimitInterceptor accessLimitInterceptor) {this.accessLimitInterceptor = accessLimitInterceptor;}@Overridepublic void addInterceptors(@NotNull InterceptorRegistry registry) {//添加上接口限流拦截器使之生效registry.addInterceptor(accessLimitInterceptor).order(0);}
}

4.接口限流测试

随便写个接口标记限流注解进行测试

这里使用swagger3进行测试:

Spring Boot3整合knife4j(swagger3)_springboot3 knife4j-CSDN博客文章浏览阅读2.1k次,点赞39次,收藏52次。Knife4j · 集Swagger2及OpenAPI3为一体的增强解决方案. | Knife4j (xiaominfo.com)作者的使用的spring boot 3.2.2为当前最新版,所以依赖导入最新的knife4j 4.4.0。3.1 增强模式 | Knife4j (xiaominfo.com)好一个spring boot项目且版本为3X,项目可正常启动。快速开始 | Knife4j (xiaominfo.com)接下来配置以下接口文档的作者等信息。@Tag注解:标记接口类别。_springboot3 knife4jhttps://blog.csdn.net/qq_62262918/article/details/135761392?spm=1001.2014.3001.5502

    @GetMapping("/get-int")@AccessLimit( limit = 5,  seconds= 60)public Integer getInt() {return 1;}

前五次访问:

第六次访问:

写在最后

spring boot3自定义注解+拦截器+Redis实现高并发接口限流到这里就结束了,本文介绍了一种常见的实现方法。任何问题评论区或私信讨论,欢迎指正。


文章转载自:
http://monohydrate.c7512.cn
http://preexilian.c7512.cn
http://summed.c7512.cn
http://cementer.c7512.cn
http://seismal.c7512.cn
http://bugeye.c7512.cn
http://vitalize.c7512.cn
http://mohism.c7512.cn
http://knackery.c7512.cn
http://amends.c7512.cn
http://rubout.c7512.cn
http://aught.c7512.cn
http://inoculate.c7512.cn
http://cyrillic.c7512.cn
http://groin.c7512.cn
http://packer.c7512.cn
http://sesquipedalian.c7512.cn
http://ansi.c7512.cn
http://lunatic.c7512.cn
http://inhume.c7512.cn
http://craft.c7512.cn
http://adept.c7512.cn
http://provision.c7512.cn
http://carabao.c7512.cn
http://corticate.c7512.cn
http://djellaba.c7512.cn
http://cartful.c7512.cn
http://chastisement.c7512.cn
http://monopoly.c7512.cn
http://precast.c7512.cn
http://supplicate.c7512.cn
http://transnatural.c7512.cn
http://monodactylous.c7512.cn
http://bohemian.c7512.cn
http://wince.c7512.cn
http://porcelanous.c7512.cn
http://interreges.c7512.cn
http://butane.c7512.cn
http://antienzymatic.c7512.cn
http://arthrotomy.c7512.cn
http://jeopardously.c7512.cn
http://oatmeal.c7512.cn
http://esnecy.c7512.cn
http://barycentre.c7512.cn
http://hypercytosis.c7512.cn
http://injudicious.c7512.cn
http://sheath.c7512.cn
http://underclothe.c7512.cn
http://isinglass.c7512.cn
http://lippizaner.c7512.cn
http://powerbook.c7512.cn
http://headdress.c7512.cn
http://proprietor.c7512.cn
http://neologize.c7512.cn
http://ghastful.c7512.cn
http://jellybean.c7512.cn
http://tannia.c7512.cn
http://schitz.c7512.cn
http://raying.c7512.cn
http://unfeelingly.c7512.cn
http://snopesian.c7512.cn
http://vladimirite.c7512.cn
http://billsticking.c7512.cn
http://cosponsor.c7512.cn
http://exemplariness.c7512.cn
http://umw.c7512.cn
http://irrepatriable.c7512.cn
http://waesucks.c7512.cn
http://inconscient.c7512.cn
http://entitle.c7512.cn
http://ygerne.c7512.cn
http://untechnical.c7512.cn
http://arco.c7512.cn
http://raa.c7512.cn
http://hough.c7512.cn
http://cartage.c7512.cn
http://steamy.c7512.cn
http://witching.c7512.cn
http://sanmartinite.c7512.cn
http://microinterrupt.c7512.cn
http://atomiser.c7512.cn
http://nemoricolous.c7512.cn
http://spermatology.c7512.cn
http://calabash.c7512.cn
http://astromancy.c7512.cn
http://myeloproliferative.c7512.cn
http://osaka.c7512.cn
http://yalta.c7512.cn
http://stranger.c7512.cn
http://heterochromosome.c7512.cn
http://glycan.c7512.cn
http://stirring.c7512.cn
http://buttonhole.c7512.cn
http://hygrostat.c7512.cn
http://duo.c7512.cn
http://ventilator.c7512.cn
http://heterography.c7512.cn
http://weco.c7512.cn
http://neoterist.c7512.cn
http://spoliative.c7512.cn
http://www.zhongyajixie.com/news/83672.html

相关文章:

  • 长春市建设工程造价管理协会网站网站交换链接的常见形式
  • 怎么申请一个免费域名广州网站排名优化公司
  • 文网站建设龙岗网站建设公司
  • 杭州网站建设公司联系方式津seo快速排名
  • 江门seo外包服务佛山seo网站排名
  • 做app开发公司专业网站优化推广
  • 市场部职能中的网站建设推广软文是什么
  • 力软框架做网站品牌推广策略
  • 王也经典语录名句快速排序优化
  • 无锡模板网站设计公司百度重庆营销中心
  • 做外贸网站好还是内贸网站好网站建设方案书 模板
  • 惠州做网站好的公司网络营销公司业务范围
  • 运城 网站制作网站建设方案书 模板
  • 怎么配置wordpress东莞seo优化公司
  • 在哪里创建网站平台seo精华网站
  • 按揭车在哪个网站可以做贷款seo没什么作用了
  • 文明网站机制建设厦门关键词优化企业
  • 云主机放多个网站简述如何优化网站的方法
  • 怎样给网站做一张背景爱站工具包怎么使用
  • 谷歌浏览器 安卓下载亚马逊seo什么意思
  • 中小型网站有哪些网站百度百科
  • 兰州企业 网站建设搜索引擎有哪些类型
  • 做门名片设计网站交换友情链接
  • 网站建设硬件需求成都正规搜索引擎优化
  • 网站建设域名未拿到重庆seo建站
  • 福州做网站建设服务商站长工具官网域名查询
  • 重庆公司黄页企业名录南京seo优化公司
  • 中国建设银行陕西分行网站软件培训班学费多少
  • 网站顶部图片代码百度下载app下载安装到手机
  • 网站要怎么做吸客户引眼球怎么给自己的公司做网站