博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊spring cloud的RequestHeaderToRequestUriGatewayFilter
阅读量:5772 次
发布时间:2019-06-18

本文共 4122 字,大约阅读时间需要 13 分钟。

本文主要研究一下spring cloud的RequestHeaderToRequestUriGatewayFilter

GatewayAutoConfiguration

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

@Configuration@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)@EnableConfigurationProperties@AutoConfigureBefore(HttpHandlerAutoConfiguration.class)@AutoConfigureAfter({GatewayLoadBalancerClientAutoConfiguration.class, GatewayClassPathWarningAutoConfiguration.class})@ConditionalOnClass(DispatcherHandler.class)public class GatewayAutoConfiguration {    //......    @Bean    public RequestHeaderToRequestUriGatewayFilterFactory requestHeaderToRequestUriGatewayFilterFactory() {        return new RequestHeaderToRequestUriGatewayFilterFactory();    }    //......}

RequestHeaderToRequestUriGatewayFilterFactory

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/filter/factory/RequestHeaderToRequestUriGatewayFilterFactory.java

/** * This filter changes the request uri by a request header * * @author Toshiaki Maki */public class RequestHeaderToRequestUriGatewayFilterFactory extends        AbstractChangeRequestUriGatewayFilterFactory
{ private final Logger log = LoggerFactory .getLogger(RequestHeaderToRequestUriGatewayFilterFactory.class); public RequestHeaderToRequestUriGatewayFilterFactory() { super(NameConfig.class); } @Override public List
shortcutFieldOrder() { return Arrays.asList(NAME_KEY); } @Override protected Optional
determineRequestUri(ServerWebExchange exchange, NameConfig config) { String requestUrl = exchange.getRequest().getHeaders().getFirst(config.getName()); return Optional.ofNullable(requestUrl).map(url -> { try { return new URL(url).toURI(); } catch (MalformedURLException | URISyntaxException e) { log.info("Request url is invalid : url={}, error={}", requestUrl, e.getMessage()); return null; } }); }}
  • 主要是继承了AbstractChangeRequestUriGatewayFilterFactory
  • 这里重写了determineRequestUri方法,根据配置的header取出url

AbstractChangeRequestUriGatewayFilterFactory

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/filter/factory/AbstractChangeRequestUriGatewayFilterFactory.java

/** * This filter changes the request uri by * {@link #determineRequestUri(ServerWebExchange, T)} logic. * * @author Toshiaki Maki */public abstract class AbstractChangeRequestUriGatewayFilterFactory
extends AbstractGatewayFilterFactory
{ private final int order; public AbstractChangeRequestUriGatewayFilterFactory(Class
clazz, int order) { super(clazz); this.order = order; } public AbstractChangeRequestUriGatewayFilterFactory(Class
clazz) { this(clazz, RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER + 1); } protected abstract Optional
determineRequestUri(ServerWebExchange exchange, T config); public GatewayFilter apply(T config) { return new OrderedGatewayFilter((exchange, chain) -> { Optional
uri = this.determineRequestUri(exchange, config); uri.ifPresent(u -> { Map
attributes = exchange.getAttributes(); attributes.put(GATEWAY_REQUEST_URL_ATTR, u); }); return chain.filter(exchange); }, this.order); }}
这里就是取出url,设置GATEWAY_REQUEST_URL_ATTR,然后GlobalFilter里头会根据这个属性取出url出来使用

实例

spring:  cloud:    gateway:      enabled: true      discovery:        locator:          enabled: true      routes:      - id: demo        uri: http://wwww.baidu.com        predicates:        - Path=/baidu/**        filters:        - RequestHeaderToRequestUri=X-New-Url
比如这里设置了根据X-New-Url来进行新的url路由,那么执行下面这段代码,则请求/baidu的则会转向请求163的
curl -i -H "X-New-Url: http://news.163.com" localhost:10000/baidu

小结

RequestHeaderToRequestUriGatewayFilter实现的功能就是根据配置文件指定的header来读取新的路由的url。比较强大,可以用在灰度、生产定位问题等场景上。

doc

转载地址:http://zwoux.baihongyu.com/

你可能感兴趣的文章
poj 2763(LCA + dfs序 +树状数组)
查看>>
计算机学院大学生程序设计竞赛(2015’12) 1006 01 Matrix
查看>>
HDU 5698 瞬间移动
查看>>
用Ant实现Java项目的自动构建和部署
查看>>
2019拼多多前端笔试
查看>>
获取input file 选中的图片,并在一个div的img里面赋值src实现预览
查看>>
Hibernate抽取BaseDao
查看>>
typedef BOOL(WINAPI *MYFUNC) (HWND,COLORREF,BYTE,DWORD);语句的理解
查看>>
cocos2dx继承结构图
查看>>
jsp 特殊标签
查看>>
[BZOJ] 1012 [JSOI2008]最大数maxnumber
查看>>
使用VMware安装CentOS
查看>>
gauss消元
查看>>
天龙八部源码描述
查看>>
多线程-ReentrantLock
查看>>
数据库架构
查看>>
【转】图文详解硬盘安装fedora
查看>>
转:LR和QTP的区别
查看>>
Loadrunner Get&Post方法性能测试脚本解析
查看>>
BZOJ 1833 数位DP
查看>>