程序员

怎么玩转Dubbo Filter

2019-03-05  本文已影响8人  杭宇_8ba6

在resources下新建META-INF/dubbo/com.alibaba.dubbo.rpc.Filter文件(可以是txt后缀文件)

撸代码之前了解个dubbo中@Activate注解小知识

@Activate(group = {Constants.PROVIDER}, value = {"token"},order = -998)

  1. group表示过滤器的使用方 常用参数:Constants.PROVIDER(服务提供方) Constants.CONSUMER(服务消费方)
  2. value表示请求URL中包含该内容则激活使用这个过滤器 加入value后,匹配条件变得更加严格,需要URL中必须有此参数,参数必须有值 。
  3. order就很简单了,order值越小,优先级越高
看张美图休息一下

撸代码吧

@Data
public class DubboLogBean {
    private long elaspedTime;
    private String fromAddress;
    private String toAddress;
    private String qualifiedName;
    private String version;
    private String group;
    private String args;
}
/**
 * @Title:
 * @Auther: hangyu
 * @Date: 2019/3/4
 * @Description
 * @Version:1.0
 */
public abstract class DubboLogFilter implements Filter {

    public abstract Logger getLogger();

    protected DubboLogBean buildDubboLogBean(Invoker invoker, Invocation invocation, String args, long elaspedTime) {
        DubboLogBean logBean = new DubboLogBean();
        RpcContext context = RpcContext.getContext();
        logBean.setFromAddress(context.getLocalAddressString());
        logBean.setToAddress(context.getRemoteAddressString());
        String qualifiedName = invoker.getInterface() + "#" + invocation.getMethodName();
        logBean.setQualifiedName(qualifiedName);
        logBean.setArgs(args);
        logBean.setVersion(invoker.getUrl().getParameter(Constants.VERSION_KEY));
        logBean.setGroup(invoker.getUrl().getParameter(Constants.GROUP_KEY));
        logBean.setElaspedTime(elaspedTime);
        return logBean;
    }

    @Override
    public Result invoke(Invoker invoker, Invocation invocation) throws RpcException {
        // 开始执行时间
        long startTime = System.currentTimeMillis();
        Result result = invoker.invoke(invocation);
        long elaspedTime = System.currentTimeMillis() - startTime;
        Object[] objs = invocation.getArguments();
        StringBuilder sb = new StringBuilder();
        for (Object obj : objs) {
            sb.append(obj).append("|");
        }
        String args = sb.toString().substring(0, sb.length() > 0 ? sb.length() - 1 : 0);
        DubboLogBean inLogBean = buildDubboLogBean(invoker, invocation, args, elaspedTime);
        getLogger().info("入参request:" + JSON.toJSONString(inLogBean));
        DubboLogBean outLogBean = buildDubboLogBean(invoker, invocation, result.toString(), elaspedTime);
        getLogger().info("出参response:" + JSON.toJSONString(outLogBean));
        return result;
    }
}
/**
 * @Title:
 * @Auther: hangyu
 * @Date: 2019/3/4
 * @Description
 * @Version:1.0
 */
@Activate(group = {Constants.PROVIDER},order = -998)
public class DubboProviderLogFilter extends DubboLogFilter {

    private static final String DUBBO_PROVIDER_LOG = "dubbo.provider";
    private final static Logger LOGGER_PROVIDER = LoggerFactory.getLogger(DUBBO_PROVIDER_LOG);

    @Override
    public Logger getLogger() {
        return LOGGER_PROVIDER;
    }
}
/**
 * @Title:
 * @Auther: hangyu
 * @Date: 2019/3/4
 * @Description
 * @Version:1.0
 */
@Activate(group = {Constants.CONSUMER}, order = -999)
public class DubboConsumerLogFilter extends DubboLogFilter {

    private static final String DUBBO_CONSUMER_LOG = "dubbo.consumer";
    private final static Logger LOGGER_CONSUMER = LoggerFactory.getLogger(DUBBO_CONSUMER_LOG);

    @Override
    public Logger getLogger() {
        return LOGGER_CONSUMER;
    }

}

启动服务,调用dubbo接口

观察控制台日志打印

上一篇下一篇

猜你喜欢

热点阅读