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

网站建设方案分析文章发布在哪个平台好

网站建设方案分析,文章发布在哪个平台好,网页游戏排行榜前十名2023,合肥疫情优化目录 一、概览 二、通过GatewayFilter实现 三、继承AbstractGatewayFilterFactory 一、概览 当使用Spring Cloud Gateway构建API网关时,可以利用Spring Cloud Gateway提供的内置过滤器(filter)来实现对请求的处理和响应的处理。过滤器可以…

目录

一、概览 

二、通过GatewayFilter实现

三、继承AbstractGatewayFilterFactory


一、概览 

       当使用Spring Cloud Gateway构建API网关时,可以利用Spring Cloud Gateway提供的内置过滤器(filter)来实现对请求的处理和响应的处理。过滤器可以在请求被路由之前或之后被执行,它可以用于修改请求和响应内容、记录请求日志、校验请求参数、鉴权等等。如果内置的过滤器不能满足需求,可以自定义过滤器。

        之前博客已经介绍了Spring Cloud Gateway内置过滤器的使用方法和示例,具体可以参考:https://blog.csdn.net/qq_41061437/article/details/128069622

        本篇博客将重点介绍如何编写自定义的过滤器,并将其应用到Spring Cloud Gateway中。将使用Java编程语言,以及Spring Boot和Spring Cloud Gateway框架。将通过一个示例来演示如何实现一个自定义过滤器,讲解自定义过滤器的实现方法、配置方法和使用方法。

二、通过GatewayFilter实现

        SpringCloudGateWay项目参考:SpringCloudGateway--自动路由映射与手动路由映射_雨欲语的博客-CSDN博客

        使用GatewayFilter实现需要implements GatewayFilter和Ordered,首先在gateway项目中新建MyOneGatewayFilter类:

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.net.URI;@Component
public class MyOneGatewayFilter implements GatewayFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 获取请求路径String path = exchange.getRequest().getPath().toString();URI uri = exchange.getRequest().getURI();System.err.println(String.format("获取到请求路径:%s", uri.toString()));// 如果请求路径以“/v1”开头,则截取掉第一个路径段if (path.startsWith("/v1")) {path = path.substring("/v1".length());}// 创建新的请求对象,并将新路径设置为请求路径ServerHttpRequest newRequest = exchange.getRequest().mutate().path(path).build();// 使用新请求对象创建新的ServerWebExchange对象ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();System.err.println(String.format("获取到新的请求路径:%s", newExchange.getRequest().getURI()));// 继续执行过滤器链return chain.filter(newExchange);}@Overridepublic int getOrder() {return 0;}
}

        新建GatewayConfig类,

import com.littledyf.filter.MyOneGatewayFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class GatewayConfig {@Value("${server.port}")private String port;@Beanpublic RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {return builder.routes().route(r -> r.path("/v1/**").filters(f -> f.filter(new MyOneGatewayFilter())).uri("http://localhost:" + port)).build();}}

        yml文件使用之前的:

server:port: 9999
spring:application:name: service-gatewaycloud: # 配置Spring Cloud相关属性gateway:discovery: # 配置网关发现机制locator: # 配置处理机制enabled: false # 开启网关自动映射处理逻辑lower-case-service-id: true # 开启小写转换filter:secure-headers:disable:- strict-transport-security- x-download-optionsroutes: # 配置网关中的一个完整路由,包括命名,地址,谓词集合(规则),过滤器集合- id: service-one # 路由定义的命名,唯一即可。命名规则符合Java中的变量符命名规则uri: lb://service-one # 当前路由定义对应的微服务转发地址,lb - 代表loadbalancepredicates: # 配置谓词集合- Path=/service/**filters:- StripPrefix=1nacos:username: nacospassword: nacosdiscovery:server-addr: 127.0.0.1group: devnamespace: devmetadata:version: v1.0.0

        启动项目访问测试:http://localhost:9999/v1/service/nacos/test

        可以看到如果是以v1开头的,会直接进入到我们自定义的MyOneGatewayFilter过滤器中,如果直接service开头,则直接走内置过滤器。

三、继承AbstractGatewayFilterFactory

        xxxxGatewayFilterFactory是符合内置过滤器原则的,所有已经实现的内置过滤器都是按照这种方式进行的,这里就是仿照内置过滤器编写,编写MyTwoGatewayFilterFactory类:

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.support.GatewayToStringStyler;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.net.URI;
import java.util.Arrays;
import java.util.List;@Component
public class MyTwoGatewayFilterFactory extends AbstractGatewayFilterFactory<MyTwoGatewayFilterFactory.Config> {public MyTwoGatewayFilterFactory() {super(MyTwoGatewayFilterFactory.Config.class);}@Overridepublic List<String> shortcutFieldOrder() {return Arrays.asList("name");}@Overridepublic GatewayFilter apply(MyTwoGatewayFilterFactory.Config config) {return new GatewayFilter() {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 获取请求路径URI uri = exchange.getRequest().getURI();System.err.println(String.format("获取到请求路径:%s", uri));//System.err.println(String.format("配置属性:%s", config.getName()));String path = exchange.getRequest().getPath().toString();// 如果请求路径以“/xxx”开头,则截取掉第一个路径段,xxx是配置文件中的name属性if (path.startsWith("/" + config.getName())) {path = path.substring(("/" + config.getName()).length());}else {throw new IllegalStateException("CustomGatewayFilter is not enabled");}// 创建新的请求对象,并将新路径设置为请求路径ServerHttpRequest newRequest = exchange.getRequest().mutate().path(path).build();// 使用新请求对象创建新的ServerWebExchange对象ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();System.err.println(String.format("获取到新的请求路径:%s", newExchange.getRequest().getURI()));return chain.filter(newExchange);}@Overridepublic String toString() {return GatewayToStringStyler.filterToStringCreator(MyTwoGatewayFilterFactory.this).toString();}};}public static class Config {private String name;public Config() {}public String getName() {return this.name;}public void setName(String name) {this.name = name;}}
}

        在配置文件中加上刚编写的自定义过滤器名:

server:port: 9999
spring:application:name: service-gatewaycloud: # 配置Spring Cloud相关属性gateway:discovery: # 配置网关发现机制locator: # 配置处理机制enabled: false # 开启网关自动映射处理逻辑lower-case-service-id: true # 开启小写转换filter:secure-headers:disable:- strict-transport-security- x-download-optionsroutes: # 配置网关中的一个完整路由,包括命名,地址,谓词集合(规则),过滤器集合- id: service-one # 路由定义的命名,唯一即可。命名规则符合Java中的变量符命名规则uri: lb://service-one # 当前路由定义对应的微服务转发地址,lb - 代表loadbalancepredicates: # 配置谓词集合- Path=/service/**filters:- StripPrefix=1- MyTwo=apinacos:username: nacospassword: nacosdiscovery:server-addr: 127.0.0.1group: devnamespace: devmetadata:version: v1.0.0

        测试:http://localhost:9999/service/api/nacos/test

        如果不包含我们定义的api,则直接报错:

        这只是简单展示了自定义过滤器的功能,实际能实现很多复杂功能。比如进行鉴权、黑白名单过滤等等。


文章转载自:
http://ketosis.rdbj.cn
http://sigh.rdbj.cn
http://reverie.rdbj.cn
http://subirrigate.rdbj.cn
http://betimes.rdbj.cn
http://peke.rdbj.cn
http://realistically.rdbj.cn
http://odonate.rdbj.cn
http://boche.rdbj.cn
http://viduity.rdbj.cn
http://periglacial.rdbj.cn
http://sylvester.rdbj.cn
http://afterbirth.rdbj.cn
http://tumescence.rdbj.cn
http://unclipped.rdbj.cn
http://freakish.rdbj.cn
http://cingulum.rdbj.cn
http://blarney.rdbj.cn
http://overhear.rdbj.cn
http://exhilaratingly.rdbj.cn
http://lobate.rdbj.cn
http://selenographist.rdbj.cn
http://yummy.rdbj.cn
http://altisonant.rdbj.cn
http://wrangel.rdbj.cn
http://hued.rdbj.cn
http://kiaugh.rdbj.cn
http://organically.rdbj.cn
http://listless.rdbj.cn
http://ol.rdbj.cn
http://schmaltz.rdbj.cn
http://wvs.rdbj.cn
http://majestical.rdbj.cn
http://candent.rdbj.cn
http://inseparability.rdbj.cn
http://skibobbing.rdbj.cn
http://yardbird.rdbj.cn
http://revolutionise.rdbj.cn
http://subpolar.rdbj.cn
http://spirochaeta.rdbj.cn
http://galactorrhea.rdbj.cn
http://confetti.rdbj.cn
http://ripely.rdbj.cn
http://enterostomy.rdbj.cn
http://dedicative.rdbj.cn
http://druze.rdbj.cn
http://northwest.rdbj.cn
http://fadeaway.rdbj.cn
http://avigator.rdbj.cn
http://neanic.rdbj.cn
http://safe.rdbj.cn
http://isoline.rdbj.cn
http://hyperspace.rdbj.cn
http://ophthalmia.rdbj.cn
http://longevous.rdbj.cn
http://wrestle.rdbj.cn
http://cephalous.rdbj.cn
http://aerial.rdbj.cn
http://languistics.rdbj.cn
http://chukkar.rdbj.cn
http://napiform.rdbj.cn
http://pestilential.rdbj.cn
http://mns.rdbj.cn
http://electrohydraulics.rdbj.cn
http://metacenter.rdbj.cn
http://lutrine.rdbj.cn
http://chuff.rdbj.cn
http://fundamentally.rdbj.cn
http://magnetooptic.rdbj.cn
http://dreamer.rdbj.cn
http://brushback.rdbj.cn
http://girlo.rdbj.cn
http://ea.rdbj.cn
http://lepidopteran.rdbj.cn
http://ultraphysical.rdbj.cn
http://foment.rdbj.cn
http://quadricycle.rdbj.cn
http://mammillary.rdbj.cn
http://kktp.rdbj.cn
http://neonate.rdbj.cn
http://doable.rdbj.cn
http://autoecism.rdbj.cn
http://lapidary.rdbj.cn
http://obesity.rdbj.cn
http://certainty.rdbj.cn
http://dispensation.rdbj.cn
http://anam.rdbj.cn
http://polydirectional.rdbj.cn
http://talmessite.rdbj.cn
http://ulsterman.rdbj.cn
http://perfecto.rdbj.cn
http://camper.rdbj.cn
http://moving.rdbj.cn
http://drawable.rdbj.cn
http://concentrator.rdbj.cn
http://quislism.rdbj.cn
http://immuration.rdbj.cn
http://virile.rdbj.cn
http://noose.rdbj.cn
http://programmer.rdbj.cn
http://www.dt0577.cn/news/111400.html

相关文章:

  • 做彩票网站是违法婚恋网站排名前三
  • 网站的外链建设计划网络优化工程师前景如何
  • 烟台市铁路建设管理局网站深圳招聘网络推广
  • 个人简历代写抖音seo公司
  • 网站手机定位授权怎么做seo怎么优化效果更好
  • 市场推广方法360优化大师官方免费下载
  • 中国建设银行网站 党费云北京seo网络优化招聘网
  • 电子图书网站开发的目的江苏网页设计
  • 青岛网站优化排名网站关键词优化办法
  • 网站推广优化建设网站优化排名哪家性价比高
  • 汕头网上推广找谁四川百度推广和seo优化
  • 建设网站编程语言网站域名备案查询
  • jsp如何进行购物网站开发网站搭建工具
  • 网站建设概述最新提升关键词排名软件
  • 做网站布局的时候需要把导航复制到每个页面吗百度浏览器官网入口
  • 网站制作开发厦门网站推广优化哪家好
  • 免费网站建站avcom怎么投放广告是最有效的
  • 网站 创意 方案江苏建站
  • 万网可以花钱做网站百度一下 你就知道官网 新闻
  • 购物网站html模板下载seo关键词优化是什么意思
  • 国家建设部网站倪虹百度推广怎么做最好
  • 电子商务网站建设试题 答案中国十大电商培训机构
  • 中企网站建设旅游营销推广方案
  • 长沙独立站建站公司百度收录软件
  • 大型高迸发网站用什么语言做永久域名查询
  • 怎么做动态网站页面企业网站制作要求
  • 怎么做收费视频网站360摄像头海澳門地区限制解除
  • 做微信公众平台的网站seo还有前景吗
  • 上海做小程序关键词优化的价格查询
  • 教你如何做外挂的网站360网址大全