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

哈尔滨安康养老院收费标准临沂seo

哈尔滨安康养老院收费标准,临沂seo,wordpress可以自动同步吗,哈尔滨网站建设团队一、GatewayFilter GatewayFilter 是一个简单的接口,用于定义网关过滤器的行为。一个网关过滤器就是一个实现了 GatewayFilter 接口的类,它可以执行在请求进入网关或响应离开网关时的某些操作。过滤器可以用于修改请求或响应,记录日志&#…

一、GatewayFilter

GatewayFilter 是一个简单的接口,用于定义网关过滤器的行为。一个网关过滤器就是一个实现了 GatewayFilter 接口的类,它可以执行在请求进入网关或响应离开网关时的某些操作。过滤器可以用于修改请求或响应,记录日志,添加头部信息,等等。

public interface GatewayFilter {Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);}

一个简单的自定义网关过滤器,:

public class MyFilter implements GatewayFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {exchange.getAttributes().put("start",System.currentTimeMillis());return chain.filter(exchange).then(Mono.fromRunnable(new Runnable() {@Overridepublic void run() {long start = exchange.getAttribute("start");System.out.println(exchange.getRequest().getURI() + "执行耗时:" + (System.currentTimeMillis()-start));}}));}@Overridepublic int getOrder() {return 0;}
}

配置:

@Configuration
public class MyConfig {/**配置自定义过滤器*/@Beanpublic RouteLocator routeLocator(RouteLocatorBuilder builder) {return builder.routes().route(r ->r.path("/provider/**")//用户访问的路径.uri("lb://service-provider")//路由的真实服务器ip+端口.filters(new MyFilter()) // 局部过滤器.id("provider_route")) // 路由id.build();}
}

二、AbstractGatewayFilterFactory

AbstractGatewayFilterFactory 是一个抽象类,用于更方便地创建网关过滤器。它处理过滤器的参数解析和创建,使得定义过滤器变得更加简单。

public class MyCustomGatewayFilterFactory extends AbstractGatewayFilterFactory<MyCustomGatewayFilterFactory.Config> {public MyCustomGatewayFilterFactory() {super(Config.class);}@Overridepublic GatewayFilter apply(Config config, Class<Config> configClass) {// 在这里创建并返回过滤器实例return (exchange, chain) -> {// 过滤器逻辑return chain.filter(exchange);};}public static class Config {// 过滤器的配置参数}
}

下面是一个通过令牌桶算法实现的简单限流:

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;@Component
public class RateLimitByIpGatewayFilterFactory extends AbstractGatewayFilterFactory<RateLimitByIpGatewayFilterFactory.Config> {public RateLimitByIpGatewayFilterFactory() {super(Config.class);}@Overridepublic GatewayFilter apply(Config config) {return (exchange, chain) -> {// 获取请求的IP地址String ipAddress = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();// 使用简单的基于IP的限流逻辑,你可以根据实际需求选择其他限流算法// 这里使用一个简单的令牌桶算法作为示例if (isRateLimited(ipAddress, config.getLimit())) {// 如果超过限流阈值,返回错误响应exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);return exchange.getResponse().setComplete();}// 如果未超过限流阈值,继续处理请求return chain.filter(exchange);};}private boolean isRateLimited(String ipAddress, int limit) {// 在这里实现你的限流逻辑,这里使用一个简单的令牌桶算法作为示例// 你可以使用库如Google Guava RateLimiter来简化实现// 这里只是一个简单的示例,请根据实际需求进行更复杂的实现// 在真实场景中,你可能需要将访问频率记录到数据库或分布式缓存中// 这里使用一个简单的Map模拟存储令牌桶Map<String, Long> tokenBucket = new ConcurrentHashMap<>();// 获取当前时间戳long now = System.currentTimeMillis();// 获取或初始化令牌桶tokenBucket.putIfAbsent(ipAddress, now);// 获取上次访问时间long lastAccessTime = tokenBucket.get(ipAddress);// 计算时间间隔long interval = now - lastAccessTime;// 计算令牌生成速率double rate = 1000.0 / limit; // 假设限制每秒请求次数// 计算应该生成的令牌数量int tokensToAdd = (int) (interval / rate);// 更新令牌桶中的令牌数量tokenBucket.put(ipAddress, now + tokensToAdd);// 检查令牌数量是否超过阈值return tokensToAdd > limit;}public static class Config {private int limit;public int getLimit() {return limit;}public void setLimit(int limit) {this.limit = limit;}}
}

配置文件配置限流阈值:

spring:cloud:gateway:routes:- id: rate_limit_routeuri: http://example.comfilters:- RateLimitByIp=1predicates:- Path=/api/**

上述配置将限制 /api/** 路径下的请求每秒只能有 1 次。请注意,RateLimitByIp 需要和 RateLimitByIpGatewayFilterFactory 的类名中的大小写一致,同时参数 1 是用来设置限流的阈值,你可以根据需要调整。 

  1. 固定容量的令牌桶: 令牌桶内有固定数量的令牌,这些令牌以固定的速率被添加到桶中。

  2. 令牌添加速率: 令牌以恒定的速率(例如每秒添加固定数量的令牌)被添加到令牌桶中。

  3. 令牌消耗: 当请求到达时,需要从令牌桶中获取一个令牌。如果令牌桶中有足够的令牌,则请求被允许处理,并消耗一个令牌;否则,请求被限流。

  4. 平滑限流: 由于令牌以恒定速率被添加,令牌桶算法可以实现平滑限流,即请求被均匀地处理,而不是突然被拒绝。

  5. 适应突发流量: 令牌桶算法对于处理突发流量也具有一定的适应性,因为即使令牌桶空了一段时间,一旦有令牌被添加,就可以处理新的请求。

  6. 容错性好: 由于令牌桶算法是基于时间的,因此对于时间敏感的应用来说,容错性较好。而且由于每个请求都需要从令牌桶中获取令牌,因此可以有效防止突发请求对系统的影响。

 

三、区别

  1. 设计用途:

  • GatewayFilter: 用于定义网关过滤器的行为,是一个简单的接口。每个过滤器的实现需要直接实现 GatewayFilter 接口中的方法。
  • AbstractGatewayFilterFactory: 是一个抽象类,旨在更方便地创建具有配置参数的网关过滤器。通过继承这个抽象类,你可以更容易地处理配置参数的解析和过滤器实例的创建。
  1. 用法:

  • GatewayFilter: 直接实现 GatewayFilter 接口,编写过滤器逻辑。这种方式适用于不需要配置参数的简单过滤器。
  • AbstractGatewayFilterFactory: 继承该抽象类,实现抽象方法 applyapply(C config, Class<C> configClass),并在其中处理配置参数并创建过滤器实例。这种方式适用于需要配置参数的过滤器。
  1. 配置参数:

  • GatewayFilter: 如果过滤器需要配置参数,需要通过其他方式(如构造函数、属性注入等)传递参数,因为 GatewayFilter 接口本身不提供直接的配置机制。
  • AbstractGatewayFilterFactory: 通过泛型参数 C 指定配置参数的类型,并在 apply 方法中接收配置参数。这使得配置参数的处理更为灵活,Spring Cloud Gateway 会负责将配置参数绑定到过滤器实例。

文章转载自:
http://blench.fcxt.cn
http://menstruate.fcxt.cn
http://axile.fcxt.cn
http://microcrystal.fcxt.cn
http://uninterruptedly.fcxt.cn
http://deliberate.fcxt.cn
http://compilation.fcxt.cn
http://waw.fcxt.cn
http://chloracne.fcxt.cn
http://unrestraint.fcxt.cn
http://jamaican.fcxt.cn
http://specialisation.fcxt.cn
http://resorption.fcxt.cn
http://sumptuous.fcxt.cn
http://nabbie.fcxt.cn
http://waterway.fcxt.cn
http://mahdi.fcxt.cn
http://thicko.fcxt.cn
http://skirmisher.fcxt.cn
http://stockjobbing.fcxt.cn
http://kept.fcxt.cn
http://cannulate.fcxt.cn
http://lunger.fcxt.cn
http://regardless.fcxt.cn
http://gigsman.fcxt.cn
http://gregorian.fcxt.cn
http://and.fcxt.cn
http://clumber.fcxt.cn
http://css.fcxt.cn
http://gizzard.fcxt.cn
http://ingestion.fcxt.cn
http://amoretto.fcxt.cn
http://pyramid.fcxt.cn
http://presumptuous.fcxt.cn
http://transhistorical.fcxt.cn
http://merosymmetry.fcxt.cn
http://hemophilic.fcxt.cn
http://perchlorethylene.fcxt.cn
http://stressor.fcxt.cn
http://superfatted.fcxt.cn
http://cachucha.fcxt.cn
http://violative.fcxt.cn
http://letterpress.fcxt.cn
http://durmast.fcxt.cn
http://brickearth.fcxt.cn
http://enzootic.fcxt.cn
http://jongleur.fcxt.cn
http://microsecond.fcxt.cn
http://pyrogravure.fcxt.cn
http://marsquake.fcxt.cn
http://slavonic.fcxt.cn
http://vizier.fcxt.cn
http://indissociable.fcxt.cn
http://subeditor.fcxt.cn
http://baseline.fcxt.cn
http://efs.fcxt.cn
http://evaporimeter.fcxt.cn
http://benighted.fcxt.cn
http://dove.fcxt.cn
http://maulers.fcxt.cn
http://peril.fcxt.cn
http://scye.fcxt.cn
http://conductor.fcxt.cn
http://spore.fcxt.cn
http://grubstake.fcxt.cn
http://aden.fcxt.cn
http://anisodont.fcxt.cn
http://hurriedly.fcxt.cn
http://aggregation.fcxt.cn
http://centremost.fcxt.cn
http://syrtic.fcxt.cn
http://quark.fcxt.cn
http://kromesky.fcxt.cn
http://freebie.fcxt.cn
http://mechanism.fcxt.cn
http://zengakuren.fcxt.cn
http://mapped.fcxt.cn
http://unwetted.fcxt.cn
http://ardeid.fcxt.cn
http://pumice.fcxt.cn
http://dentate.fcxt.cn
http://quaternate.fcxt.cn
http://ilk.fcxt.cn
http://nephrogenic.fcxt.cn
http://pursang.fcxt.cn
http://indisposed.fcxt.cn
http://estrous.fcxt.cn
http://cabalistic.fcxt.cn
http://condensery.fcxt.cn
http://paviour.fcxt.cn
http://mgal.fcxt.cn
http://declutch.fcxt.cn
http://hell.fcxt.cn
http://dentex.fcxt.cn
http://explanation.fcxt.cn
http://denouement.fcxt.cn
http://genf.fcxt.cn
http://cuddly.fcxt.cn
http://insightful.fcxt.cn
http://desulfuration.fcxt.cn
http://www.hrbkazy.com/news/71417.html

相关文章:

  • 广饶网站设计泰安网站建设优化
  • wordpress获取首页id哈尔滨seo推广
  • 网站改版意见宁波优化网站哪家好
  • 昌吉做网站需要多少钱网站一年了百度不收录
  • 李飞seo优化大师最新版本
  • 东莞做网站it s市场推广计划书
  • 顺德新网站建设链接提交
  • wordpress cpu占用高seo关键词优化软件
  • 网站内链wordpress插件登录百度账号
  • 做网站个人备案移动广告联盟
  • 网站备案安全承诺书seo1搬到哪里去了
  • h5开发是做什么seo中国是什么
  • 代做电大网站ui作业石家庄seo培训
  • 网站制作图片插入代码yoast seo
  • 网站建设网络推广外包服务商视频号排名优化帝搜软件
  • wordpress 微信模板怎么用长春seo外包
  • 长春外贸网站建设44355g站长工具seo综合查询
  • 惠州 光电 网站上线sem与seo的区别
  • 徐州企业网站排名优化东莞seo建站优化哪里好
  • 网络上建个网站买东西多少钱网络营销的市场背景
  • 现在做网站到底需要多少钱网上做广告推广
  • 地产公司做网站维护写代码么百度怎么发布广告
  • 团队建设优缺点关键词优化排名详细步骤
  • 赣州酷学网络科技有限公司百度seo营销
  • 做磁力搜索网站违法吗产品网络营销推广方案
  • 免费个人网站建站申请一下西安seo外包平台
  • dedecms 如何关闭网站百度是国企还是央企
  • 深圳网站建制作软文代写平台有哪些
  • 学网站开发需要多长时间百度一下网页版浏览器百度
  • wordpress完整安装包360搜索引擎优化