context-path插件可以帮我们重新请求的URL,这样对外可以屏蔽实际的请求地址。我们来解析一下如何使用
一、环境搭建
- soul-admin
开启 context-path 插件:系统管理 --> 插件管理
- soul-bootstrap
soul-bootstrap/pom.xml
网关中 rewrite 依赖默认是没有的需要手动添加
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-context-path</artifactId>
<version>${project.version}</version>
</dependency>
- 开启服务
soul-admin
soul-bootstrap
soul-examples-http
二、配置选择器和选择器规则
- devide 插件
我这里通过devide插件代理http请求,所以开启的devide插件,并且soul-examples-http服务开启后,数据会自动同步
无需手动添加,如果需要自定义可以自由修改
- context-path 插件
选择器和选择器规则需要手动配置
contextPath 配置的是自定义的请求路径,实际转发过程中 会匹配自定义的contextPath截取真实的请求URL
例如:
1、请求: http://127.0.0.1:9195/http/test/order/findById?id=5
2、contextPath:/http/test
3、实际转发:http://192.168.161.165:8188/order/findById?id=5
三、测试
查看网关测的日志打印,请求已经被context_path插件匹配
http://127.0.0.1:9195/http/test/order/findById?id=5
匹配 /http/test 实际转发如下地址
http://10.7.254.31:8188/order/findById?id=5
➜ ~ curl -X GET \
'http://127.0.0.1:9195/http/test/order/findById?id=5' \
-H 'cache-control: no-cache' \
-H 'postman-token: 3877184f-0398-180a-12f9-8930ecfa24bf' \
-H 'test: test'
{"id":"5","name":"hello world findById"}% ➜ ~
2021-02-04 10:58:52.953 INFO 26430 --- [work-threads-17] o.d.soul.plugin.base.AbstractSoulPlugin : context_path selector success match , selector name :context_path_test
2021-02-04 10:58:52.953 INFO 26430 --- [work-threads-17] o.d.soul.plugin.base.AbstractSoulPlugin : context_path rule success match , rule name :context_path_test_rule
2021-02-04 10:59:04.702 INFO 26430 --- [work-threads-18] o.d.soul.plugin.base.AbstractSoulPlugin : context_path selector success match , selector name :context_path_test
2021-02-04 10:59:04.702 INFO 26430 --- [work-threads-18] o.d.soul.plugin.base.AbstractSoulPlugin : context_path rule success match , rule name :context_path_test_rule
2021-02-04 10:59:04.702 INFO 26430 --- [work-threads-18] o.d.soul.plugin.base.AbstractSoulPlugin : divide selector success match , selector name :/http
2021-02-04 10:59:04.703 INFO 26430 --- [work-threads-18] o.d.soul.plugin.base.AbstractSoulPlugin : divide rule success match , rule name :/http/test/**
2021-02-04 10:59:04.703 INFO 26430 --- [work-threads-18] o.d.s.plugin.httpclient.WebClientPlugin : The request urlPath is http://192.168.161.165:8188/order/findById?id=5, retryTimes is 0
四、解析context_path源码
1、soul-bootstrap 加入了依赖 soul-spring-boot-starter-plugin-context-path
org.dromara.soul.springboot.starter.plugin.contextpath.ContextPathMappingPluginConfiguration
soul-bootstrap 启动后自动加载 ContextPathMappingPluginConfiguration 配置类
向容器中注入ContextPathMappingPlugin
@Configuration
public class ContextPathMappingPluginConfiguration {
/**
* Context path mapping plugin.
* @return the soul plugin
*/
@Bean
public SoulPlugin contextPathMappingPlugin() {
return new ContextPathMappingPlugin();
}
}
2、ContextPathMappingPlugin
模块:soul-plugin-context-path
org.dromara.soul.plugin.contextpath.ContextPathMappingPlugin
ContextPathMappingPlugin —> doExecute 首先会检查 path:/http/test/order/findById 是否和规则 context-path:/http/test 匹配,如果匹配成功则继续处理
buildContextPath 方法会截取 context-path 设置真正的请求地址
@Slf4j
public class ContextPathMappingPlugin extends AbstractSoulPlugin {
@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);
}
......
/**
* Build the context path and realUrl.
* @param context context
* @param handle handle
*/
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);
}
}
3、debug过程