美文网首页程序员
怎么玩转Dubbo Filter

怎么玩转Dubbo Filter

作者: 杭宇_8ba6 | 来源:发表于2019-03-05 10:55 被阅读8次

    在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;
    }
    
    • 构建dubbo过滤器
    /**
     * @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;
        }
    
    }
    
    • com.alibaba.dubbo.rpc.Filter文件中识别该过滤器
      dubboProviderLog=com.bbkmobile.iqoo.vivoshop.core.filter.DubboProviderLogFilter
      dubboConsumerLog=com.bbkmobile.iqoo.vivoshop.core.filter.DubboConsumerLogFilter

    启动服务,调用dubbo接口

    观察控制台日志打印

    相关文章

      网友评论

        本文标题:怎么玩转Dubbo Filter

        本文链接:https://www.haomeiwen.com/subject/mchnuqtx.html