Soul网关源码阅读18-解析context-path插件

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过程
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、总结

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值