Soul源码总结-02-04

  • .跑通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重新定义,从而起到对外可以屏蔽实际的请求地址的作用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值