插件扩展
说明
- 插件是
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()指定插件的名称,命名采用Camel Case,如:dubbo、springCloud。 -
skip()在特定的条件下,该插件是否被跳过。
-
-
注册成
Spring的bean,参考如下,或者直接在实现类上加@Component注解。
@Bean
public ShenyuPlugin myShenyuPlugin() {
return new MyShenyuPlugin();
}