shenyu
shenyu copied to clipboard
[Question] <title>插件执行的是按照排序的顺序执行嘛
Question
不是的,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();
});
}