- .跑通context-path插件
- 分析源码以及context-path插件底层实现
- 总结
Demo
从soul网关官方文档可以了解到soul网关在对目标服务调用的时候,容许用户使用 context-path 插件来重写请求路径的contextPath。
首先启动项目soul-admin以及soul-bootstrap,以soul-examples
中的soul-example-http
为例,注册到soul网关上。检查soul-bootstrap的pom
文件中是否引入相关依赖:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-context-path</artifactId>
<version>${project.version}</version>
</dependency>
同时在soul-admin插件管理中心开启context_path
插件
同时此处需要手动配置selector和rule的相关配置
selector配置:
rule相关配置
测试Context-Path插件
使用postman对http://localhost:9195/http/test/order/findById?id=1
进行测试,观察插件是否正确开启
同时观察到soul网关控制台打出以下日志,说明context_path插件已经成功开启
2021-02-04 11:21:28.066 INFO 18440 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin : context_path selector success match , selector name :context-http
2021-02-04 11:21:28.067 INFO 18440 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin : context_path rule success match , rule name :context-http-order
2021-02-04 11:21:28.068 INFO 18440 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin : divide selector success match , selector name :/http
2021-02-04 11:21:28.068 INFO 18440 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin : divide rule success match , rule name :/http/test/**
2021-02-04 11:21:28.080 INFO 18440 --- [-work-threads-1] o.d.s.plugin.httpclient.WebClientPlugin : The request urlPath is http://172.28.32.1:8188/order/findById?id=1, retryTimes is 0
如果输入非法路径,则得到以下响应信息:
探究context_path插件
首先soul.springboot.starter.plugin
在项目启动开始就将ContextPathMappingPluginConfiguration
作为bean注入到ioc容器中。
核心类就是ContextPathMappingPlugin
,核心代码仍然是doExecute
@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
assert soulContext != null;
final String handle = rule.getHandle();
final ContextMappingHandle contextMappingHandle = GsonUtils.getInstance().fromJson(handle, ContextMappingHandle.class);
if (Objects.isNull(contextMappingHandle) || StringUtils.isBlank(contextMappingHandle.getContextPath())) {
log.error("context path mapping rule configuration is null :{}", rule);
return chain.execute(exchange);
}
//check the context path illegal
if (!soulContext.getPath().startsWith(contextMappingHandle.getContextPath())) {
Object error = SoulResultWrap.error(SoulResultEnum.CONTEXT_PATH_ERROR.getCode(), SoulResultEnum.CONTEXT_PATH_ERROR.getMsg(), null);
return WebFluxResultUtils.result(exchange, error);
}
this.buildContextPath(soulContext, contextMappingHandle);
return chain.execute(exchange);
}
其主要作用是做一个context-path的check,然后重新构建真实的URL,接着继续插件链的后续调用
private void buildContextPath(final SoulContext context, final ContextMappingHandle handle) {
context.setContextPath(handle.getContextPath());
if (!StringUtils.isBlank(handle.getRealUrl())) {
log.info("context path mappingPlugin replaced old :{} , real:{}", context.getRealUrl(), handle.getRealUrl());
context.setRealUrl(handle.getRealUrl());
return;
}
Optional<String> optional = Arrays.stream(context.getPath()
.split(handle.getContextPath()))
.reduce((first, last) -> last);
optional.ifPresent(context::setRealUrl);
}
总结
context-path插件的实现与其他插件相比比较简单,其主要作用就是对uri的contextPath重新定义,从而起到对外可以屏蔽实际的请求地址的作用。