Filter The filter Dubbo (interceptor) to be used --dubbo.rpc.Filter

com.alibaba.dubbo.rpc.Filter Profile

Let's first talk about the "Filter", as the name suggests filter in our daily work is a fervent opponent.
In previous articles I have introduced similar in Spring HandlerInterceptor to use .
com.alibaba.dubbo.rpc.Filter it changed the vest, it is still dry gatekeepers (filter) job, but Ali went to work in dubbo stem from the gatekeepers of security, so low-key things to know him apes are not many, and far less javax.servlet.Filter HandlerInterceptor reputation, children and grandchildren everywhere, playing mahjong are also a few more to buy a horse on the side.

Nonsense pull finished, entered, I took my side boast printing systems business to introduce serial number. Business background check problem is to find the log often comes to boast query system to another system, but how do I quickly find the corresponding log in the log huge amounts of time on the case for screening is very inefficient and stupid. So we each HTTP request is assigned a unique UUID, UUID in the process of carrying in Dubbo interface calls in the past to ensure the realization of the log to quickly locate the same UUID.

1. Definitions Filter

  • Com.alibaba.dubbo.rpc.Filter implement the interface.
  • Adding Activate, which notes the following meanings:
    • group: the owning group such as consumer, service provider Han
    • order: Filter execution order of a plurality of (smaller earlier)
package com.test.trace.remote.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.test.trace.support.AbstractMyThreadContext;
import com.test.trace.support.AbstractUUIDShort;


@Activate(group = {Constants.CONSUMER, Constants.PROVIDER}, order = -30000)
public class RemoteTraceIdFilter implements Filter {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
       //判断是消费者  还是 服务提供者
        if (RpcContext.getContext().isConsumerSide()) {
            //消费者 将trace_id(业务流水号) set至上下文中
            RpcContext.getContext().setAttachment(AbstractMyThreadContext.TRACE_ID,
                    AbstractMyThreadContext.getTraceId());
        } else {
            //取出业务流水号
            String traceId =
                    RpcContext.getContext().getAttachment(AbstractMyThreadContext.TRACE_ID);
            if (traceId == null) {
                traceId = AbstractUUIDShort.generate();
            }
            //流水号set至线程局部变量
            AbstractMyThreadContext.setTraceId(traceId);
            //slf4j 中设置了日志打印格式用作日志链路追踪
            MDC.put(AbstractMyThreadContext.MDC_TRACE_ID, traceId);
        }
        try {
            return invoker.invoke(invocation);
        } finally {
            if (RpcContext.getContext().isProviderSide()) {
                MDC.remove(AbstractMyThreadContext.MDC_TRACE_ID);
                AbstractMyThreadContext.removeTraceId();
            }
        }
    }
}

2, take a name

  • New resources directory in the META-INF folder, and then create sub-folders dubbo, the last new file com.alibaba.dubbo.rpc.Filter. ( Knock blackboard draw the focus pull! Do not make a mistake here )
  • From a name for your filter and attach the corresponding filter implementation class path, as shown below became known as traceId (name of everyone here a name based on their own business)
    File directory name

3, to the porters

  • To find specific items in your configuration file with the following configuration dubbo
  • filter is, we take the second step in a file alias
<dubbo:provider filter="traceId" />
<dubbo:consumer filter="traceId" />

This simple method applicable to the Filter dubbo everyone should understand you can use to get started.
If necessary set of log link tracking code that can wait for the rest of my next article posted.

4, the side story

We are now using maven There is a small pit to note is that the next advance. If you do not pay attention may report: No such extension xxxFilter for filter / com.alibaba.dubbo.rpc.Filter error
error content is not found xxxFilter, there may be your pom specified file types need to be packaged under the resources, modify percent. * end just fine. code show as below

<resource>
	<directory>${basedir}/src/main/resources</directory>
	<filtering>true</filtering>
	<includes>
		<include>**/*.*</include>
	</includes>
</resource>
Published 18 original articles · won praise 45 · views 110 000 +

Guess you like

Origin blog.csdn.net/zhibo_lv/article/details/104796122