soul 入门 第十八章 rewrite 插件

174 阅读1分钟

soul 入门 第十八章 rewrite 插件

介绍

soul网关在对目标服务代理时候,可以通过rewrite插件对请求路径进行重写,这在系统改造时或者用得上。

如何使用
  • soul-admin –> 插件管理 –> rewrite,设置为开启。

  • 在网关的 pom.xml 文件中添加 rewrite 的支持。

    <!-- soul rewrite plugin start-->
      <dependency>
          <groupId>org.dromara</groupId>
          <artifactId>soul-spring-boot-starter-plugin-rewrite</artifactId>
         <version>${last.version}</version>
      </dependency>
      <!-- soul rewrite plugin end-->
    
代码剖析
@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
    String handle = rule.getHandle();
    final RewriteHandle rewriteHandle = GsonUtils.getInstance().fromJson(handle, RewriteHandle.class);
    if (Objects.isNull(rewriteHandle) || StringUtils.isBlank(rewriteHandle.getRewriteURI())) {
        log.error("uri rewrite rule can not configuration:{}", handle);
        return chain.execute(exchange);
    }
  // 设定重写的路径,该路径在soul-admin后台的规则设定,往后传递
    exchange.getAttributes().put(Constants.REWRITE_URI, rewriteHandle.getRewriteURI());
    return chain.execute(exchange);
}
总结

该插件比较简单。