美文网首页
26.Dubbo隐式参数传递

26.Dubbo隐式参数传递

作者: 山海树 | 来源:发表于2020-09-17 07:31 被阅读0次

首先需要再消费端的AbstractClusterInvoker类的inoke()方法类,把附加属性键值对放到RpcInvocation的attachments变量中,然后经过网络传递到服务端。
服务端则使用ContextFilter对请求进行拦截,并从RpcInvocation中获取attachments中的键值对,然后使用RpcContext.getContext().setAttachement设置到上下文对象中。

消费端:AbstractClusterInvoker方法

 public Result invoke(final Invocation invocation) throws RpcException {
        checkWhetherDestroyed();

        // binding attachments into invocation.
        Map<String, String> contextAttachments = RpcContext.getContext().getAttachments();
        if (contextAttachments != null && contextAttachments.size() != 0) {
            ((RpcInvocation) invocation).addAttachments(contextAttachments);
        }

        List<Invoker<T>> invokers = list(invocation);
        LoadBalance loadbalance = initLoadBalance(invokers, invocation);
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }

添加的隐式参数最终是要清除的,当发送完成后,将会在ConsumerContextFilter的invoker中清除

  public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        RpcContext.getContext().setInvoker(invoker).setInvocation(invocation).setLocalAddress(NetUtils.getLocalHost(), 0).setRemoteAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort());
        if (invocation instanceof RpcInvocation) {
            ((RpcInvocation)invocation).setInvoker(invoker);
        }

        Result var3;
        try {
            RpcContext.removeServerContext();
            var3 = invoker.invoke(invocation);
        } finally {
            RpcContext.getContext().clearAttachments();
        }

        return var3;
    }

服务端:ContextFilter方法

 public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        Map<String, String> attachments = invocation.getAttachments();
        if (attachments != null) {
            attachments = new HashMap((Map)attachments);
            ((Map)attachments).remove("path");
            ((Map)attachments).remove("interface");
            ((Map)attachments).remove("group");
            ((Map)attachments).remove("version");
            ((Map)attachments).remove("dubbo");
            ((Map)attachments).remove("token");
            ((Map)attachments).remove("timeout");
            ((Map)attachments).remove("async");
            ((Map)attachments).remove("dubbo.tag");
            ((Map)attachments).remove("dubbo.force.tag");
        }

        RpcContext.getContext().setInvoker(invoker).setInvocation(invocation).setLocalAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort());
        if (attachments != null) {
            if (RpcContext.getContext().getAttachments() != null) {
                RpcContext.getContext().getAttachments().putAll((Map)attachments);
            } else {
                RpcContext.getContext().setAttachments((Map)attachments);
            }
        }

        if (invocation instanceof RpcInvocation) {
            ((RpcInvocation)invocation).setInvoker(invoker);
        }

        Result var4;
        try {
            var4 = invoker.invoke(invocation);
        } finally {
            RpcContext.removeContext();
            RpcContext.removeServerContext();
        }

        return var4;
    }

服务端会在此时将接受到的隐式参数设置到上下文中。

消费端拦截器的添加时机


image.png

服务端拦截器添加时机


image.png

相关文章

  • 26.Dubbo隐式参数传递

    首先需要再消费端的AbstractClusterInvoker类的inoke()方法类,把附加属性键值对放到Rpc...

  • 隐式转换

    1、隐式参数和隐式值 函数的形式参数如果用implicit修饰则是隐式参数,隐式参数可以有一个默认值,调用的时...

  • scala-隐式机制及Akka

    隐式机制及Akka 隐式转换 隐式转换和隐式参数时Scala中两个非常强大的功能,利用隐式转换和隐式参数,可以提供...

  • 第28课:Scala隐式转换内幕实践解密

    其实隐式转换有几种类型:隐式参数,隐式转换,隐式对象,和隐式类 首先看一下Scala的作用域 隐式参数冲突的情况:...

  • scala implicit 隐式转换和隐式参数

    1.什么是隐式转换和隐式参数?隐式转换是以implicit 声明的带有单个参数的函数隐式参数是函数或方法带有一个标...

  • 函数的参数

    认识两个参数 arguments和this可以隐式地传递给函数,并且可以像其他的参数一样进行使用。++argume...

  • Chapter 21《Implicit Conversions

    隐式转换和隐式参数 如果使用别人的代码库,无法进行修改,Scala进行扩展的方法是隐式转换和隐式参数。允许省略掉冗...

  • js之this

    1.浏览器调用函数每次都会向函数内部传递一个隐式参数,这个参数就是this, 函数调用传的是window,fun(...

  • scala implicit关键字详解(隐式转换函数、隐式类、隐

    scala implicit关键字详解(隐式转换函数、隐式类、隐式参数、隐式值) 一、Overview impli...

  • 好程序员大数据教程分享Scala系列之隐式转换和隐式参数

    好程序员大数据教程Scala系列之隐式转换和隐式参数 5.1.概念 隐式转换和隐式参数是Scala中两个非常强大的...

网友评论

      本文标题:26.Dubbo隐式参数传递

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