插件扩展
说明
- 插件是
Apache ShenYu
网关的核心执行者,每个插件在开启的情况下,都会对匹配的流量,进行自己的处理。 - 在
Apache ShenYu
网关里面,插件分为两类。- 一类是单一职责的调用链,不能对流量进行自定义的筛选。
- 一类是能对匹配的流量,执行自己的职责调用链。
- 用户可以参考 shenyu-plugin 模块,新增自己的插件处理,如果有好的公用插件,可以向官网提交
pr
。
单一职责插件
- 引入如下依赖:
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-plugin-api</artifactId>
<version>${project.version}</version>
</dependency>
- 用户新增一个类
MyShenyuPlugin
,直接实现org.apache.shenyu.plugin.api.ShenyuPlugin
public interface ShenyuPlugin {
/**
* Process the Web request and (optionally) delegate to the next
* {@code WebFilter} through the given {@link ShenyuPluginChain}.
*
* @param exchange the current server exchange
* @param chain provides a way to delegate to the next filter
* @return {@code Mono<Void>} to indicate when request processing is complete
*/
Mono<Void> execute(ServerWebExchange exchange, ShenyuPluginChain chain);
/**
* return plugin order .
* This attribute To determine the plugin execution order in the same type plugin.
*
* @return int order
*/
int getOrder();
/**
* acquire plugin name.
* this is plugin name define you must Provide the right name.
* if you impl AbstractShenyuPlugin this attribute not use.
*
* @return plugin name.
*/
default String named() {
return "";
}
/**
* plugin is execute.
* if return true this plugin can not execute.
*
* @param exchange the current server exchange
* @return default false.
*/
default Boolean skip(ServerWebExchange exchange) {
return false;
}
}
接口方法详细说明
execute()
方法为核心的执行方法,用户可以在里面自由的实现自己想要的功能。getOrder()
指定插件的排序。named()
指定插件的名称。skip()
在特定的条件下,该插件是否被跳过。
注册成
Spring
的bean
,参考如下,或者直接在实现类上加@Component
注解。
@Bean
public ShenyuPlugin myShenyuPlugin() {
return new MyShenyuPlugin();
}
匹配流量处理插件
- 引入如下依赖:
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-plugin-base</artifactId>
<version>${project.version}</version>
</dependency>
新增一个类
CustomPlugin
,继承org.apache.shenyu.plugin.base.AbstractShenyuPlugin
以下是参考:
/**
* This is your custom plugin.
* He is running in after before plugin, implement your own functionality.
* extends AbstractShenyuPlugin so you must user shenyu-admin And add related plug-in development.
*
* @author xiaoyu(Myth)
*/
public class CustomPlugin extends AbstractShenyuPlugin {
/**
* 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 "shenyu";
}
/**
* plugin is execute.
* Do I need to skip.
* if you need skip return true.
*
* @param exchange the current server exchange
* @return default false.
*/
@Override
public Boolean skip(final ServerWebExchange exchange) {
return false;
}
/**
* this is Template Method child has Implement your own logic.
*
* @param exchange exchange the current server exchange
* @param chain chain the current chain
* @param selector selector
* @param rule rule
* @return {@code Mono<Void>} to indicate when request handling is complete
*/
@Override
protected abstract Mono<Void> doExecute(ServerWebExchange exchange, ShenyuPluginChain chain, SelectorData selector, RuleData 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);
}
}
详细讲解:
继承该类的插件,插件会进行选择器规则匹配。
首先在
shenyu-admin
后台管理系统 —> 基础配置 —> 插件管理 中,新增一个插件,注意 名称与 你自定义插件的named()
方法要一致。重新登陆
shenyu-admin
后台,可以看见刚新增的插件,然后就可以进行选择器规则匹配。在 基础配置 —> 插件处理管理 中,可以给插件的不同位置添加自定义字段。然后在相应位置添加条目时,就可以为这些字段指定值,它们将以 json 格式发送给插件,从而达到动态配置插件的目的。其中
选择器
与规则
处的字段,分别可以在doExecute()
方法中,通过selector.getHandle();
与rule.getHandle();
获取,然后进行你的操作。
注册成
Spring
的bean
,参考如下或者直接在实现类上加@Component
注解。
@Bean
public ShenyuPlugin customPlugin() {
return new CustomPlugin();
}
订阅你的插件数据,进行自定义的处理
- 新增一个类
PluginDataHandler
,实现org.apache.shenyu.plugin.base.handler.PluginDataHandler
public interface PluginDataHandler {
/**
* Handler plugin.
*
* @param pluginData the plugin data
*/
default void handlerPlugin(PluginData pluginData) {
}
/**
* Remove plugin.
*
* @param pluginData the plugin data
*/
default void removePlugin(PluginData pluginData) {
}
/**
* Handler selector.
*
* @param selectorData the selector data
*/
default void handlerSelector(SelectorData selectorData) {
}
/**
* Remove selector.
*
* @param selectorData the selector data
*/
default void removeSelector(SelectorData selectorData) {
}
/**
* Handler rule.
*
* @param ruleData the rule data
*/
default void handlerRule(RuleData ruleData) {
}
/**
* Remove rule.
*
* @param ruleData the rule data
*/
default void removeRule(RuleData ruleData) {
}
/**
* Plugin named string.
*
* @return the string
*/
String pluginNamed();
}
注意
pluginNamed()
要和你自定义的插件名称相同。注册成
Spring
的bean
,参考如下或者直接在实现类上加@Component
注解。
@Bean
public PluginDataHandler pluginDataHandler() {
return new PluginDataHandler();
}