shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Question] <title>插件执行的是按照排序的顺序执行嘛

Open chao6118 opened this issue 4 months ago • 1 comments

Question

Image

chao6118 avatar Aug 24 '25 01:08 chao6118

不是的,plugin 实现里面有一个 order 方法,是按照 order 去实现的。

插件 order

@Override
public int getOrder() {
    return PluginEnum.DIVIDE.getCode();
}

排序

@Bean("webHandler")
public ShenyuWebHandler shenyuWebHandler(final ObjectProvider<List<ShenyuPlugin>> plugins, final ShenyuConfig config, @Lazy final ShenyuLoaderService shenyuLoaderService) {
    List<ShenyuPlugin> pluginList = plugins.getIfAvailable(Collections::emptyList);
    List<ShenyuPlugin> shenyuPlugins = pluginList.stream()
            .sorted(Comparator.comparingInt(ShenyuPlugin::getOrder)).collect(Collectors.toList());
    shenyuPlugins.forEach(shenyuPlugin -> LOG.info("load plugin:[{}] [{}]", shenyuPlugin.named(), shenyuPlugin.getClass().getName()));
    return new ShenyuWebHandler(shenyuPlugins, shenyuLoaderService, config);
}

执行

@Override
public Mono<Void> execute(final ServerWebExchange exchange) {
    return Mono.defer(() -> {
        if (this.index < plugins.size()) {
            ShenyuPlugin plugin = plugins.get(this.index++);
            boolean skip = plugin.skip(exchange);
            if (skip) {
                return this.execute(exchange);
            }
            try {
                plugin.before(exchange);
                return plugin.execute(exchange, this);
            } finally {
                plugin.after(exchange);
            }
        }
        return Mono.empty();
    });
}

yuluo-yx avatar Aug 28 '25 03:08 yuluo-yx