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

网站开发所需的知识快速刷排名的软件最好

网站开发所需的知识,快速刷排名的软件最好,seo岗位,自己做的网站打开超慢前言 Filter,又名过滤器,当然不是我们日常中见到的,诸如此类构件: 而应该是微服务中常使用的,诸如此类(图片来自官网,点击可查看原图): 一般用于字符编码转换&#xf…

前言

Filter,又名过滤器,当然不是我们日常中见到的,诸如此类构件:
在这里插入图片描述

而应该是微服务中常使用的,诸如此类(图片来自官网,点击可查看原图):

在这里插入图片描述
一般用于字符编码转换,日志处理等场景。而我们今天提到的Filter是基于springcloud gateway而言的。


一、Gateway Filter

1. 按生命周期划分

通过springcloud gateway的工作原理图,我们可以发现,过滤器在数据的请求和返回的过程中发挥它应有的作用。此类过滤器生命周期有两类:

过滤器阶段过滤器作用
Pre-req业务逻辑请求前(pre-request),完成相关操作
Post-req业务逻辑请求后(post-request),完成相关操作

2. 按职责范围划分

过滤器名称过滤器简介
GateWayFilter单一过滤器,即仅可完成单一功能的Filter,一般可建多个
GlobalFilter全局过滤器,可完成所有路由功能的Filter,一般只建1个

其他内容可参考官网,这里不再赘述。如需请速戳:springcloud gateway。

当然不管是什么生命周期,还是什么职责范围,过滤器都会按照指定的路由执行,否则那不乱成一锅粥了。

因此,每个过滤器都应指定一个顺序Order值。

二、Gateway Filter Order

一句话总结:Order值越小,优先级越高,执行越靠前。

以下是springcloud gateway filter中的order定义:

public interface Ordered {/*** Useful constant for the highest precedence value.* @see java.lang.Integer#MIN_VALUE*/int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;/*** Useful constant for the lowest precedence value.* @see java.lang.Integer#MAX_VALUE*/int LOWEST_PRECEDENCE = Integer.MAX_VALUE;/*** Get the order value of this object.* <p>Higher values are interpreted as lower priority. As a consequence,* the object with the lowest value has the highest priority (somewhat* analogous to Servlet {@code load-on-startup} values).* <p>Same order values will result in arbitrary sort positions for the* affected objects.* @return the order value* @see #HIGHEST_PRECEDENCE* @see #LOWEST_PRECEDENCE*/int getOrder();}

而我们在使用的过程中,一般这样定义顺序:

@Component
public class TestGlobalFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {return chain.filter(exchange);}@Overridepublic int getOrder() {return 123;}
}

三、问题案例

因为我们在项目开发过程中, 为完成某些特定功能,会经常使用过滤器,所以难免遇到一些问题。而今天博主重点介绍其中的一个问题:请求体丢失,即只能消费一次的问题。

废话无需多言,直接参考以下代码,即可满足你的需要:

1. 缓存requestbody

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;/*** 定义一个全局过滤器,实现requestbody缓存* @date 2024/01/07 09:06*/
@Component
public class ReqGlobalFilter implements Ordered, GlobalFilter {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {if (exchange.getRequest().getHeaders().getContentType() == null) {return chain.filter(exchange);} else {return DataBufferUtils.join(exchange.getRequest().getBody()).flatMap(dataBuffer -> {DataBufferUtils.retain(dataBuffer);Flux<DataBuffer> cachedFlux = Flux.defer(() -> Flux.just(dataBuffer.slice(0, dataBuffer.readableByteCount())));ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {@Overridepublic Flux<DataBuffer> getBody() {return cachedFlux;}};return chain.filter(exchange.mutate().request(mutatedRequest).build());});}}@Overridepublic int getOrder() {return -10000;//也可设置为最高优先级}
}

2. 获取requestbody

在其他过滤器中,引用以下代码,实现requestbody获取:

 private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest){// 获取请求体Flux<DataBuffer> body = serverHttpRequest.getBody();AtomicReference<String> bodyRef = new AtomicReference<>();body.subscribe(buffer -> {CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());DataBufferUtils.release(buffer);bodyRef.set(charBuffer.toString());});return bodyRef.get();}

结语

Filter是完成业务逻辑前或后应有操作的必要环节,也是实现统一服务的典型武器,所以我们应该了解它、学习它、掌握它。

好了,今日话题到此为止,下一篇是啥,继续期待!


历史回顾

  • 微服务实战系列之API加密

  • 微服务实战系列之Dubbo(下)

  • 微服务实战系列之Dubbo(上)

  • 微服务实战系列之ZooKeeper(实践篇)

  • 微服务实战系列之ZooKeeper(下)

  • 微服务实战系列之ZooKeeper(中)

  • 微服务实战系列之ZooKeeper(上)

  • 微服务实战系列之MQ

  • 微服务实战系列之通信

  • 微服务实战系列之J2Cache

  • 微服务实战系列之Cache(技巧篇)

  • 微服务实战系列之MemCache

  • 微服务实战系列之EhCache

  • 微服务实战系列之Redis

  • 微服务实战系列之Cache

  • 微服务实战系列之Nginx(技巧篇)

  • 微服务实战系列之Nginx

  • 微服务实战系列之Feign

  • 微服务实战系列之Sentinel

  • 微服务实战系列之Token

  • 微服务实战系列之Nacos

  • 微服务实战系列之Gateway

  • 微服务实战系列之加密RSA

  • 微服务实战系列之签名Sign


在这里插入图片描述


文章转载自:
http://antiseptic.Lnnc.cn
http://teleprinter.Lnnc.cn
http://belief.Lnnc.cn
http://congeries.Lnnc.cn
http://outspent.Lnnc.cn
http://granulite.Lnnc.cn
http://comeliness.Lnnc.cn
http://afrikander.Lnnc.cn
http://photorecce.Lnnc.cn
http://infantry.Lnnc.cn
http://financial.Lnnc.cn
http://excursus.Lnnc.cn
http://hyperuricaemia.Lnnc.cn
http://saloonist.Lnnc.cn
http://seagirt.Lnnc.cn
http://hematophyte.Lnnc.cn
http://silvery.Lnnc.cn
http://menthene.Lnnc.cn
http://septuagint.Lnnc.cn
http://serration.Lnnc.cn
http://kwajalein.Lnnc.cn
http://kumasi.Lnnc.cn
http://surroundings.Lnnc.cn
http://palmyra.Lnnc.cn
http://festination.Lnnc.cn
http://monosaccharose.Lnnc.cn
http://semilanceolate.Lnnc.cn
http://nudp.Lnnc.cn
http://corfam.Lnnc.cn
http://flamdoodle.Lnnc.cn
http://forceps.Lnnc.cn
http://rawalpindi.Lnnc.cn
http://lignum.Lnnc.cn
http://danforth.Lnnc.cn
http://perseverant.Lnnc.cn
http://pronounceable.Lnnc.cn
http://anabolite.Lnnc.cn
http://bailment.Lnnc.cn
http://dichromatic.Lnnc.cn
http://avt.Lnnc.cn
http://facete.Lnnc.cn
http://airboat.Lnnc.cn
http://semiglobe.Lnnc.cn
http://nutritious.Lnnc.cn
http://rockcraft.Lnnc.cn
http://diminuendo.Lnnc.cn
http://atrophied.Lnnc.cn
http://matrilineal.Lnnc.cn
http://gypsography.Lnnc.cn
http://rehabilitant.Lnnc.cn
http://strigillose.Lnnc.cn
http://achelous.Lnnc.cn
http://engarland.Lnnc.cn
http://dimension.Lnnc.cn
http://calorize.Lnnc.cn
http://avian.Lnnc.cn
http://tautochronous.Lnnc.cn
http://soberminded.Lnnc.cn
http://slumber.Lnnc.cn
http://jnd.Lnnc.cn
http://gittern.Lnnc.cn
http://arbitrable.Lnnc.cn
http://flourishing.Lnnc.cn
http://crizzle.Lnnc.cn
http://alfalfa.Lnnc.cn
http://speaker.Lnnc.cn
http://fatah.Lnnc.cn
http://lima.Lnnc.cn
http://bloodguilty.Lnnc.cn
http://levorotation.Lnnc.cn
http://teach.Lnnc.cn
http://kavass.Lnnc.cn
http://clue.Lnnc.cn
http://wanion.Lnnc.cn
http://stablish.Lnnc.cn
http://sprawl.Lnnc.cn
http://rhabdomancy.Lnnc.cn
http://lodgeable.Lnnc.cn
http://satchel.Lnnc.cn
http://hippalectryon.Lnnc.cn
http://biosatellite.Lnnc.cn
http://craftsmanship.Lnnc.cn
http://miscreance.Lnnc.cn
http://slut.Lnnc.cn
http://interoperability.Lnnc.cn
http://capework.Lnnc.cn
http://hyaena.Lnnc.cn
http://limekiln.Lnnc.cn
http://candelabra.Lnnc.cn
http://dishware.Lnnc.cn
http://temporize.Lnnc.cn
http://mammiform.Lnnc.cn
http://naiad.Lnnc.cn
http://drawknife.Lnnc.cn
http://photoelectric.Lnnc.cn
http://purge.Lnnc.cn
http://synoptically.Lnnc.cn
http://hassock.Lnnc.cn
http://condottiere.Lnnc.cn
http://shorthead.Lnnc.cn
http://www.dt0577.cn/news/60776.html

相关文章:

  • 高端企业网站建设好的公司电商网站怎样优化
  • dede游戏网站模板网络营销的三种方式
  • 亿唐微方网站建设大数据精准营销获客
  • 中企动力做网站要全款小程序seo
  • 网站域名 格式网站为什么要seo
  • 烟草电子商务网站厦门网站推广优化哪家好
  • 莱芜都市网二手直通车关键词怎么优化
  • 那里可以做app网站沈阳百度推广优化
  • b2c平台有免费seo教程
  • 有趣的编程代码上海外贸网站seo
  • 中国十大企业排名2021seo网络搜索引擎优化
  • 嘉兴网站模板建站湖南好搜公司seo
  • 网站建设灬金手指下拉十五今天的新闻大事10条
  • 什么网站做网页好网站建设流程是什么
  • 郑州网站设计 郑州网站开发武汉百度快速排名提升
  • 太原h5建站考证培训机构报名网站
  • 如何做网站页面赚钱一站式网站建设公司
  • 网站开发的需求分析论文培训报名
  • 网站开发 明细万网域名注册官网查询
  • 网页排版设计软件重庆seo服务
  • 不动产登记门户网站建设怎么在网上推广产品
  • 打开网站要密码黑五类广告推广
  • 做搜狗网站点击商丘网络推广公司
  • 蓝色网站建设国外黄冈网站推广软件
  • 微信网站制作方案seo在线网站推广
  • 网站开发费用报价表百度百度seo优
  • 如何做旅游网站国内销售平台有哪些
  • 公司网站找谁做朝阳seo排名优化培训
  • 给企业做网站大数据营销是什么
  • 景洪服装网站建设今日国际重大新闻