- plugnType():是表示该插件的执行顺序与功能,BEFORE是最开始执行,Function 是中间,Last是最后执行。方便使用与扩展。
- getOrder():是指同一种类型插件执行时候的先后顺序。
- named():插件命名。
- skip(): 该插件是否需要跳过,默认不跳过。
- execute():插件执行的链条,会传递到下一个插件,责任链模式了解一下。
package org.dromara.soul.extend.demo.custom;
import org.dromara.soul.common.dto.zk.RuleZkDTO;
import org.dromara.soul.common.dto.zk.SelectorZkDTO;
import org.dromara.soul.common.enums.PluginTypeEnum;
import org.dromara.soul.common.utils.GsonUtils;
import org.dromara.soul.extend.demo.entity.Test;
import org.dromara.soul.web.cache.ZookeeperCacheManager;
import org.dromara.soul.web.plugin.AbstractSoulPlugin;
import org.dromara.soul.web.plugin.SoulPluginChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* This is your custom plugin.
* He is running in after before plugin, implement your own functionality.
* extends AbstractSoulPlugin so you must user soul-admin And add related plug-in development.
*
* @author xiaoyu(Myth)
*/
public class CustomPlugin extends AbstractSoulPlugin {
/**
* logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(CustomPlugin.class);
public CustomPlugin(final ZookeeperCacheManager dataCacheManager) {
}
/**
* return plugin type.
* The PluginTypeEnum.BEFORE is first
* The PluginTypeEnum.LAST is last.
*
* @return {@linkplain PluginTypeEnum}
*/
@Override
public PluginTypeEnum pluginType() {
return PluginTypeEnum.FUNCTION;
}
/**
* return plugin order .
* The same plugin he executes in the same order.
*
* @return int
*/
@Override
public int getOrder() {
return 0;
}
/**
* acquire plugin name.
* return you custom plugin name.
* It must be the same name as the plug-in you added in the admin background.
*
* @return plugin name.
*/
@Override
public String named() {
return "soul";
}
/**
* plugin is execute.
*
* @param exchange the current server exchange
* @return default false.
*/
@Override
public Boolean skip(final ServerWebExchange exchange) {
return false;
}
@Override
protected Mono<Void> doExecute(ServerWebExchange exchange, SoulPluginChain chain, SelectorZkDTO selector, RuleZkDTO rule) {
LOGGER.debug(".......... function plugin start..............");
/*
* Processing after your selector matches the rule.
* rule.getHandle() is you Customize the json string to be processed.
* for this example.
* Convert your custom json string pass to an entity class.
*/
final String ruleHandle = rule.getHandle();
final Test test = GsonUtils.getInstance().fromJson(ruleHandle, Test.class);
/*
* Then do your own business processing.
* The last execution chain.execute(exchange).
* Let it continue on the chain until the end.
*/
System.out.println(test.toString());
return chain.execute(exchange);
}
}