• soul 提供了扩展demo 项目是soul-extend-demo

  • soul 提供了2种方式的扩展:

  • 第一种是实现 SoulPlugin接口:

  1. package org.dromara.soul.web.plugin;
  2. import org.dromara.soul.common.enums.PluginTypeEnum;
  3. import org.springframework.web.server.ServerWebExchange;
  4. import reactor.core.publisher.Mono;
  5. /**
  6. * the soul plugin interface.
  7. *
  8. * @author xiaoyu
  9. */
  10. public interface SoulPlugin {
  11. /**
  12. * Process the Web request and (optionally) delegate to the next
  13. * {@code WebFilter} through the given {@link SoulPluginChain}.
  14. *
  15. * @param exchange the current server exchange
  16. * @param chain provides a way to delegate to the next filter
  17. * @return {@code Mono<Void>} to indicate when request processing is complete
  18. */
  19. Mono<Void> execute(ServerWebExchange exchange, SoulPluginChain chain);
  20. /**
  21. * return plugin type.
  22. * the plugin execution order
  23. * before type The first to perform then Function Type ,then last type.
  24. *
  25. * @return {@linkplain PluginTypeEnum}
  26. */
  27. PluginTypeEnum pluginType();
  28. /**
  29. * return plugin order .
  30. * This attribute To determine the plugin execution order in the same type plugin.
  31. *
  32. * @return int order
  33. */
  34. int getOrder();
  35. /**
  36. * acquire plugin name.
  37. * this is plugin name define if you extend {@linkplain AbstractSoulPlugin } you must Provide the right name.
  38. * if you impl AbstractSoulPlugin this attribute not use.
  39. *
  40. * @return plugin name.
  41. */
  42. String named();
  43. /**
  44. * plugin is execute.
  45. * if return true this plugin can not execute.
  46. *
  47. * @param exchange the current server exchange
  48. * @return default false.
  49. */
  50. default Boolean skip(ServerWebExchange exchange) {
  51. return false;
  52. }
  • plugnType():是表示该插件的执行顺序与功能,BEFORE是最开始执行,Function 是中间,Last是最后执行。方便使用与扩展。
  • getOrder():是指同一种类型插件执行时候的先后顺序。
  • named():插件命名。
  • skip(): 该插件是否需要跳过,默认不跳过。
  • execute():插件执行的链条,会传递到下一个插件,责任链模式了解一下。

如果是实现SoulPlugin的话,只需要将你的实现类声明为Spring的bean就行。

  • 第二种是继承 org.dromara.soul.web.plugin.AbstractSoulPlugin
  1. package org.dromara.soul.extend.demo.custom;
  2. import org.dromara.soul.common.dto.zk.RuleZkDTO;
  3. import org.dromara.soul.common.dto.zk.SelectorZkDTO;
  4. import org.dromara.soul.common.enums.PluginTypeEnum;
  5. import org.dromara.soul.common.utils.GsonUtils;
  6. import org.dromara.soul.extend.demo.entity.Test;
  7. import org.dromara.soul.web.cache.ZookeeperCacheManager;
  8. import org.dromara.soul.web.plugin.AbstractSoulPlugin;
  9. import org.dromara.soul.web.plugin.SoulPluginChain;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.web.server.ServerWebExchange;
  13. import reactor.core.publisher.Mono;
  14. /**
  15. * This is your custom plugin.
  16. * He is running in after before plugin, implement your own functionality.
  17. * extends AbstractSoulPlugin so you must user soul-admin And add related plug-in development.
  18. *
  19. * @author xiaoyu(Myth)
  20. */
  21. public class CustomPlugin extends AbstractSoulPlugin {
  22. /**
  23. * logger.
  24. */
  25. private static final Logger LOGGER = LoggerFactory.getLogger(CustomPlugin.class);
  26. public CustomPlugin(final ZookeeperCacheManager dataCacheManager) {
  27. super(dataCacheManager);
  28. }
  29. /**
  30. * return plugin type.
  31. * The type of plug-ins indicates their order at runtime
  32. * The PluginTypeEnum.BEFORE is first
  33. * The PluginTypeEnum.LAST is last.
  34. *
  35. * @return {@linkplain PluginTypeEnum}
  36. */
  37. @Override
  38. public PluginTypeEnum pluginType() {
  39. return PluginTypeEnum.FUNCTION;
  40. }
  41. /**
  42. * return plugin order .
  43. * The same plugin he executes in the same order.
  44. *
  45. * @return int
  46. */
  47. @Override
  48. public int getOrder() {
  49. return 0;
  50. }
  51. /**
  52. * acquire plugin name.
  53. * return you custom plugin name.
  54. * It must be the same name as the plug-in you added in the admin background.
  55. *
  56. * @return plugin name.
  57. */
  58. @Override
  59. public String named() {
  60. return "soul";
  61. }
  62. /**
  63. * plugin is execute.
  64. * Do I need to skip.
  65. * if you need skip return true.
  66. *
  67. * @param exchange the current server exchange
  68. * @return default false.
  69. */
  70. @Override
  71. public Boolean skip(final ServerWebExchange exchange) {
  72. return false;
  73. }
  74. @Override
  75. protected Mono<Void> doExecute(ServerWebExchange exchange, SoulPluginChain chain, SelectorZkDTO selector, RuleZkDTO rule) {
  76. LOGGER.debug(".......... function plugin start..............");
  77. /*
  78. * Processing after your selector matches the rule.
  79. * rule.getHandle() is you Customize the json string to be processed.
  80. * for this example.
  81. * Convert your custom json string pass to an entity class.
  82. */
  83. final String ruleHandle = rule.getHandle();
  84. final Test test = GsonUtils.getInstance().fromJson(ruleHandle, Test.class);
  85. /*
  86. * Then do your own business processing.
  87. * The last execution chain.execute(exchange).
  88. * Let it continue on the chain until the end.
  89. */
  90. System.out.println(test.toString());
  91. return chain.execute(exchange);
  92. }
  93. }
  • 然后把自己定义的插件类注册成为Spring的bean。

  • 注意named() 方法返回的要与你在soul-admin后台添加的插件名称一样。