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

淘宝购物返利网站建设app如何做计算机网站

淘宝购物返利网站建设app,如何做计算机网站,人力资源公司代缴社保合法吗,网上做设计的网站有哪些Spring Cloud Gateway实现API访问频率限制 一、为什么需要访问频率限制#xff1f;二、使用全局过滤器实现访问频率限制步骤#xff1a;示例代码#xff1a; 三、使用特定路由的过滤器实现访问频率限制步骤#xff1a;示例代码#xff1a; 四、总结 在微服务架构中#x… Spring Cloud Gateway实现API访问频率限制 一、为什么需要访问频率限制二、使用全局过滤器实现访问频率限制步骤示例代码 三、使用特定路由的过滤器实现访问频率限制步骤示例代码 四、总结 在微服务架构中API网关扮演着至关重要的角色它不仅负责路由请求还能提供诸如安全性、监控和限流等功能。Spring Cloud Gateway作为Spring Cloud生态系统中的一员提供了强大的路由和过滤功能。本文将详细介绍如何使用Spring Cloud Gateway的全局过滤器Global Filters或特定路由的过滤器Gateway Filters来实现对外部接口的访问频率限制。 一、为什么需要访问频率限制 访问频率限制Rate Limiting是保护后端服务免受恶意或异常流量攻击的重要手段。通过限制客户端在一定时间窗口内的请求次数可以有效防止服务过载保障系统的稳定性和可用性。 二、使用全局过滤器实现访问频率限制 全局过滤器适用于对所有路由进行统一的访问频率限制。以下是实现这一功能的详细步骤和示例代码。 步骤 创建一个自定义的全局过滤器 实现GlobalFilter接口并在过滤器中实现访问频率限制逻辑。 配置过滤器 将自定义的全局过滤器注册到Spring Cloud Gateway中。 示例代码 import org.springframework.cloud.gateway.filter.GatewayFilterChain;import org.springframework.cloud.gateway.filter.GlobalFilter;import org.springframework.core.Ordered;import org.springframework.http.HttpStatus;import org.springframework.stereotype.Component;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicInteger;Componentpublic class RateLimitGlobalFilter implements GlobalFilter, Ordered {private final ConcurrentHashMapString, AtomicInteger requestCounts new ConcurrentHashMap();Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {String ipAddress exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();AtomicInteger count requestCounts.computeIfAbsent(ipAddress, k - new AtomicInteger(0));if (count.incrementAndGet() 10) { // 每秒最多10次请求exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);return exchange.getResponse().setComplete();}return chain.filter(exchange).then(Mono.fromRunnable(() - {if (count.decrementAndGet() 0) {requestCounts.remove(ipAddress);}}));}Overridepublic int getOrder() {return Ordered.LOWEST_PRECEDENCE;}} 三、使用特定路由的过滤器实现访问频率限制 特定路由的过滤器适用于对特定路由进行访问频率限制。 步骤 创建一个自定义的GatewayFilter工厂 实现GatewayFilterFactory接口并在工厂中实现访问频率限制逻辑。 配置路由过滤器 在路由配置中使用自定义的GatewayFilter工厂。 示例代码 import org.springframework.cloud.gateway.filter.GatewayFilter;import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;import org.springframework.http.HttpStatus;import org.springframework.stereotype.Component;import reactor.core.publisher.Mono;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicInteger;Componentpublic class RateLimitGatewayFilterFactory extends AbstractGatewayFilterFactoryRateLimitGatewayFilterFactory.Config {private final ConcurrentHashMapString, AtomicInteger requestCounts new ConcurrentHashMap();public RateLimitGatewayFilterFactory() {super(Config.class);}Overridepublic GatewayFilter apply(Config config) {return (exchange, chain) - {String routeId exchange.getRequest().getPath().toString();AtomicInteger count requestCounts.computeIfAbsent(routeId, k - new AtomicInteger(0));if (count.incrementAndGet() config.getMaxRequestsPerSecond()) { // 每秒最多config.getMaxRequestsPerSecond()次请求exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);return exchange.getResponse().setComplete();}return chain.filter(exchange).then(Mono.fromRunnable(() - {if (count.decrementAndGet() 0) {requestCounts.remove(routeId);}}));};}public static class Config {private int maxRequestsPerSecond;public int getMaxRequestsPerSecond() {return maxRequestsPerSecond;}public void setMaxRequestsPerSecond(int maxRequestsPerSecond) {this.maxRequestsPerSecond maxRequestsPerSecond;}}}在application.yml中配置路由过滤器 cloud:gateway:routes:- id: rate_limited_routeuri: http://example.compredicates:- Path/rate_limited_pathfilters:- name: RateLimitargs:maxRequestsPerSecond: 10四、总结 通过以上步骤和示例代码可以在Spring Cloud Gateway中实现对外部接口的访问频率限制。根据具体需求选择使用全局过滤器或特定路由的过滤器可以有效保护后端服务免受异常流量攻击提升系统的稳定性和可用性。
http://www.w-s-a.com/news/339954/

相关文章:

  • 大型电子商务网站 服务器硬件 cpu 内存 硬盘 2014美食网站开发意义
  • 建立网站的目的和意义网站建设寻求
  • 邢台手机网站建设设计师培训心得
  • 营销网站怎么做丽水微信网站建设哪家好
  • 南昌定制网站开发多少钱东阿县城市建设局网站
  • 浙江网站建设公司南昌seo招聘
  • 工业软件有哪些专业seo站长工具全面查询网站
  • 山东兴华建设集团有限公司网站和京东一样做电子产品的网站
  • 网站建设谢辞关于h5的网站模板
  • 网站改版提交WordPress360收录
  • 省级网站 开发建设 资质在国外怎么做网站
  • 中商华兴建设有限公司网站我的世界查找建筑网站
  • 广东网站设计公司百度推广免费送网站
  • 高密做网站哪家好网站建设预算
  • 免费wordpress网站模板重庆如何做聚政网站
  • 人才网站app建设建议系统开发生命周期法的优点表现
  • 门户网站想要微信登录怎么做湖南网站seo推广
  • 襄阳 网站建设管理系统网站
  • 重庆工程建设招标投标交易信息网广州外贸seo优化
  • 一个一起做网站东莞设计兼职网站建设
  • 杭州网站程序开发公司在哪个公司建设网站好
  • 网店的网站设计方案济南手机建站价格
  • 网站做了301重定向域名会自动跳转吗唐山地方志网站建设
  • 学校网站建设说明书海南省建设执业资格注册管理中心网站
  • 东莞哪家网站建设好网站风格设定
  • 自驾游网站模板搭建wordpress步骤
  • wordpress视频网站上传视频提升学历是什么意思
  • 江西省城乡建设厅建设网站浙江建设
  • 网站联系我们页面临平做网站
  • 如何用网站做cpa交互比较好的网站