Introduction to Soul Chapter 18 rewrite Plugins

introduce

The Soul gateway can use the rewrite plug-in to rewrite the request path when proxying the target service, which may come in handy during system transformation.

How to use
  • Set this to on in soul-admin – > Plug-in Management – > rewrite.

  • Add support for rewrite to the gateway’s POM.xml file.

    <! -- 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-->
    Copy the code
Code analysis
@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("Rewriting rule can not configuration: {}", handle);
        return chain.execute(exchange);
    }
  // Set the overridden path, which is passed in the soul-admin background rule setting
    exchange.getAttributes().put(Constants.REWRITE_URI, rewriteHandle.getRewriteURI());
    return chain.execute(exchange);
}
Copy the code
conclusion

The plug-in is relatively simple.