dubbo 过滤器


dubbo 过滤器

          

              

                                

过滤器

            

                 

说明:dubbo服务调用、服务暴露的过程中均会使用到过滤器,两端的过滤器独立运行,互不影响

             

Filter:过滤器接口

/**
 * Extension for intercepting the invocation for both service provider and consumer, furthermore, most of
 * functions in dubbo are implemented base on the same mechanism. Since every time when remote method is
 * invoked, the filter extensions will be executed too, the corresponding penalty should be considered before
 * more filters are added.
 * <pre>
 *  They way filter work from sequence point of view is
 *    <b>
 *    ...code before filter ...
 *          invoker.invoke(invocation) //filter work in a filter implementation class
 *          ...code after filter ...
 *    </b>
 *    Caching is implemented in dubbo using filter approach. If cache is configured for invocation then before
 *    remote call configured caching type's (e.g. Thread Local, JCache etc) implementation invoke method gets called.
      //缓存在dubbo里使用过滤器实现的
 * </pre>
 *
 * Starting from 3.0, Filter on consumer side has been refactored. There are two different kinds of Filters working at different stages
 * of an RPC request.
 * 1. Filter. Works at the instance level, each Filter is bond to one specific Provider instance(invoker).
 * 2. ClusterFilter. Newly introduced in 3.0, intercepts request before Loadbalancer picks one specific Filter(Invoker).
   //从3.0开始,消费端的过滤器重构了,有两种不同的过滤器在不同的阶段起作用
   //filter:对单个provier实例起作用
   //clusterFilter:集群过滤器,在负载均衡选择invoker前起作用
 *
 * Filter Chain in 3.x
 *
 *                                          -> Filter -> Invoker
 *
 * Proxy -> ClusterFilter -> ClusterInvoker -> Filter -> Invoker
 *
 *                                          -> Filter -> Invoker
 *
 *
 * Filter Chain in 2.x
 *
 *                            Filter -> Invoker
 *
 * Proxy -> ClusterInvoker -> Filter -> Invoker
 *
 *                            Filter -> Invoker
 *
 *
 * Filter. (SPI, Singleton, ThreadSafe)
 *
 * @see org.apache.dubbo.rpc.filter.GenericFilter
 * @see org.apache.dubbo.rpc.filter.EchoFilter
 * @see org.apache.dubbo.rpc.filter.TokenFilter
 * @see org.apache.dubbo.rpc.filter.TpsLimitFilter
 */
@SPI(scope = ExtensionScope.MODULE)
public interface Filter extends BaseFilter {
}

                 

                 

# META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter文件
echo=org.apache.dubbo.rpc.filter.EchoFilter
generic=org.apache.dubbo.rpc.filter.GenericFilter
genericimpl=org.apache.dubbo.rpc.filter.GenericImplFilter
token=org.apache.dubbo.rpc.filter.TokenFilter
accesslog=org.apache.dubbo.rpc.filter.AccessLogFilter
classloader=org.apache.dubbo.rpc.filter.ClassLoaderFilter
classloader-callback=org.apache.dubbo.rpc.filter.ClassLoaderCallbackFilter
context=org.apache.dubbo.rpc.filter.ContextFilter
exception=org.apache.dubbo.rpc.filter.ExceptionFilter
executelimit=org.apache.dubbo.rpc.filter.ExecuteLimitFilter
deprecated=org.apache.dubbo.rpc.filter.DeprecatedFilter
compatible=org.apache.dubbo.rpc.filter.CompatibleFilter
timeout=org.apache.dubbo.rpc.filter.TimeoutFilter
tps=org.apache.dubbo.rpc.filter.TpsLimitFilter

trace=org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter
monitor=org.apache.dubbo.monitor.support.MonitorFilter

metrics=org.apache.dubbo.monitor.dubbo.MetricsFilter

consumersign=org.apache.dubbo.auth.filter.ConsumerSignFilter
providerauth=org.apache.dubbo.auth.filter.ProviderAuthFilter
cache=org.apache.dubbo.cache.filter.CacheFilter
validation=org.apache.dubbo.validation.filter.ValidationFilter

             

dubbo 3.0.5中没有添加activeLimitFilter过滤器,需自行添加

                             

activeLimit=org.apache.dubbo.rpc.filter.ActiveLimitFilter

                  

ClusterFilter:消费端集群过滤器,在负载均衡之前调用

@SPI(scope = ExtensionScope.MODULE)
public interface ClusterFilter extends BaseFilter {
}

                 

# META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.filter.ClusterFilter文件
zone-aware=org.apache.dubbo.rpc.cluster.filter.support.ZoneAwareFilter
consumercontext=org.apache.dubbo.rpc.cluster.filter.support.ConsumerContextFilter
future=org.apache.dubbo.rpc.protocol.dubbo.filter.FutureFilter
monitor=org.apache.dubbo.monitor.support.MonitorClusterFilter

                               

服务端过滤器

@Activate(group = PROVIDER, value = ACCESS_LOG_KEY)  //服务端过滤器
public class AccessLogFilter implements Filter {

              

消费端过滤器

@Activate(group = CONSUMER, value = ACTIVES_KEY)     //消费端过滤器
public class ActiveLimitFilter implements Filter, Filter.Listener {

             

服务端过滤器说明

# accesslogFilter:日志过滤器,输出服务请求日志
输出到日志组件(如logback、log4j2等):true、default
输出到指定文件:access.log
  <dubbo:service interface="com.xxx.MenuService" accesslog="true | default" />
  <dubbo:service interface="com.xxx.MenuService" accesslog="access.log" />
日志文件输出样例:
  [2022-02-14 17:39:48] 172.25.12.168:56672 -> 172.25.12.168:20880 - com.example.demo.service.HelloService hello() 

# executeLimitFilter:限制服务中每个方法的最大并发数
  <dubbo:service interface="com.xxx.MenuService" executes="10" />
  <dubbo:service interface="com.xxx.MenuService" >
     <dubbo:method name="getMenuItems" executes="10" />
  <dubbo:service/>

# cacheFilter:缓存过滤器,可选值:lru, threadlocal, jcache
lru:最近最少使用原则删除多余缓存,保持最热的数据被缓存
threadlocal:当前线程缓存,比如一个页面渲染,用到很多portal,每个portal都要去查用户信息,通过线程缓存,可以减少这种多余访问。
jcache:与 JSR107 集成,可以桥接各种缓存实现

# classLoaderFilter:类加载器过滤器,切换当前线程的类加载器为接口的类加载器
# classLoaderCallbackFilter:在过滤器回调接口中切换类加载器(Switch thread context class loader on filter callback)

# contextFilter:记录当前线程的请求上下文
ContextFilter set the provider RpcContext with invoker, invocation, local port it is using and host for current execution thread.

# exceptionFilter:处理服务端自定义异常(用RuntimeException包装),防止消费端序列化失败
unexpected exception will be logged in ERROR level on provider side. 
Unexpected exception are unchecked exception not declared on the interface
Wrap the exception not introduced in API package into RuntimeException. 
Framework will serialize the outer exception but stringnize its cause in order to avoid of possible serialization problem on client side

# timeoutFilter:超时过滤器,超时打印日志,不会影响程序执行
Log any invocation timeout, but don't stop server from running

# tokenFilter:服务端对请求做令牌检验,验证通过后执行请求
Perform check whether given provider token is matching with remote token or not. 
If it does not match it will not allow invoking remote method.

# tpsLimitFilter:对服务端限流,使用令牌桶限流算法
TpsLimitFilter limit the TPS (transaction per second) for all method of a service or a particular method.
Service or method url can define tps or tps.interval to control this control. It use {@link DefaultTPSLimiter} as it limit checker. 
If a provider service method is configured with tps、tps.interval, then if invocation count exceed the configured tps value (default is -1 which means unlimited) then invocation will get RpcException.
服务或者方法使用tps或者tps.interval限流,如果超过设定的值,会抛出rpcException

                         

消费端过滤器说明

# activeLimitFilter:消费端并发限制,使用参数actives设置最大并发数
ActiveLimitFilter restrict the concurrent client invocation for a service or service's method from client side.
To use active limit filter, configured url with actives and provide valid >0 integer value

# cacheFilter:缓存过滤器,可选值:lru, threadlocal, jcache
lru:最近最少使用原则删除多余缓存,保持最热的数据被缓存
threadlocal:当前线程缓存,比如一个页面渲染,用到很多portal,每个portal都要去查用户信息,通过线程缓存,可以减少这种多余访问。
jcache:与 JSR107 集成,可以桥接各种缓存实现

# deprecatedFilter:如果有注解@Deprecated的方法被调用,会打印error日志
DeprecatedFilter logs error message if a invoked method has been marked as deprecated. 
To check whether a method is deprecated or not it looks for deprecated attribute value and consider it is deprecated it value is true

# futureFilter:调用异常时,触发onreturn、oninvoker、onthrow方法
<bean id ="demoCallback" class = "org.apache.dubbo.callback.implicit.NotifyImpl" />
<dubbo:reference id="demoService" interface="org.apache.dubbo.callback.implicit.IDemoService" version="1.0.0" group="cn" >
      <dubbo:method name="get" async="true" onreturn = "demoCallback.onreturn" onthrow="demoCallback.onthrow" />
</dubbo:reference>

           

                

                        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值