场景:
将所有请求转化为同一路径请求(方便穿网配置)在请求头内标识原来路径,然后在将请求分发给不同服务
alltoonegatewayfilterfactory
import lombok.getter; import lombok.setter; import lombok.extern.slf4j.slf4j; import org.springframework.cloud.gateway.filter.gatewayfilter; import org.springframework.cloud.gateway.filter.factory.abstractgatewayfilterfactory; import org.springframework.http.server.reactive.serverhttprequest; import org.springframework.stereotype.component; @component @slf4j public class alltoonegatewayfilterfactory extends abstractgatewayfilterfactory<alltoonegatewayfilterfactory.config> { public alltoonegatewayfilterfactory() { super(config.class); } @override public gatewayfilter apply(config config) { return (exchange, chain) -> { serverhttprequest request = exchange.getrequest(); request.geturi(); // 替换路径 string path = request.getpath().tostring(); serverhttprequest modifiedrequest = request.mutate().header(config.headername, path).path(config.gettopath()).build(); exchange = exchange.mutate().request(modifiedrequest).build(); log.info("alltoone: headers{{}:{}}, {} ---> {}", config.getheadername(),path, request.geturi(), modifiedrequest.geturi()); return chain.filter(exchange); }; } @setter @getter public static class config { private string headername; private string topath; } }
spring: cloud: gateway: httpclient: ssl: useinsecuretrustmanager: true routes: - id: alltoone_fnpt uri: http://localhost:19982 predicates: - path=/** filters: - name: alltoone args: headername: api-path topath: /api/unified
注意:1.类名必须以gatewayfilterfactory结尾否则会出现不识别 的情况
2.配置的filters -name 的值为类的前缀(截取gatewayfilterfactory之后的)
到此这篇关于springcloudgateway 自定义局部过滤器的文章就介绍到这了,更多相关springcloudgateway 过滤器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论