Dubbo自定义扩展filter

写了好多年了,求波点赞,收藏,关注,一键三连!!

 

DUBBO版本:2.7.0

Dubbo的扩展点是带@SPI注解的,比如:

@SPI
public interface Filter {

    /**
     * do invoke filter.
     * 

* * // before filter * Result result = invoker.invoke(invocation); * // after filter * return result; * * * @param invoker service * @param invocation invocation. * @return invoke result. * @throws RpcException * @see org.apache.dubbo.rpc.Invoker#invoke(Invocation) */ Result invoke(Invoker invoker, Invocation invocation) throws RpcException; default Result onResponse(Result result, Invoker invoker, Invocation invocation) { return result; } } @SPI("dubbo") public interface Protocol { /** * Get default port when user doesn't config the port. * * @return default port */ int getDefaultPort(); /** * Export service for remote invocation:
* 1. Protocol should record request source address after receive a request: * RpcContext.getContext().setRemoteAddress();
* 2. export() must be idempotent, that is, there's no difference between invoking once and invoking twice when * export the same URL
* 3. Invoker instance is passed in by the framework, protocol needs not to care
* * @param Service type * @param invoker Service invoker * @return exporter reference for exported service, useful for unexport the service later * @throws RpcException thrown when error occurs during export the service, for example: port is occupied */ @Adaptive Exporter export(Invoker invoker) throws RpcException; /** * Refer a remote service:
* 1. When user calls `invoke()` method of `Invoker` object which's returned from `refer()` call, the protocol * needs to correspondingly execute `invoke()` method of `Invoker` object
* 2. It's protocol's responsibility to implement `Invoker` which's returned from `refer()`. Generally speaking, * protocol sends remote request in the `Invoker` implementation.
* 3. When there's check=false set in URL, the implementation must not throw exception but try to recover when * connection fails. * * @param Service type * @param type Service class * @param url URL address for the remote service * @return invoker service's local proxy * @throws RpcException when there's any error while connecting to the service provider */ @Adaptive Invoker refer(Class type, URL url) throws RpcException; /** * Destroy protocol:
* 1. Cancel all services this protocol exports and refers
* 2. Release all occupied resources, for example: connection, port, etc.
* 3. Protocol can continue to export and refer new service even after it's destroyed. */ void destroy(); }

只是DUBOO增强的SPI机制,按需加载

配置方式和JDK原生的差不太多。在META-INF包下dubbo目录下:

注意:老版本可能是com.alibaba的包名,移交到apache后都要用新的包名了

xxxFilter=com.wm.test.xxxFilter

这就可以实现Filter的扩展了。并不需要注入到spring容器中。这点注意一下。

你可能感兴趣的:(dubbo,分布式,java,spi,dubbo,filter)