dubbo不常见概念

版权声明:本文为博主原创文章,未经博主允许可以转载。 https://blog.csdn.net/a809918928/article/details/86593378
dubbo隐式传参

RpcContext.getContext().setAttachment(“index”, “1”); // 隐式传参,后面的远程调用都会隐式将这些参数发送到服务器端,类似cookie,用于框架集成,不建议常规业务使用
dubbo通过客户端向服务器端传递参数,传递参数时path,group,version,dubbo,token,timeout即可key有特殊处理,不能使用这几个特殊key
服务提供方使用RpcContext.getContext()
.getAttachments();获取参数,
服务器消费方使用RpcContext.getContext().setAttachment(); 传递参数

dubbo泛化接口调用

泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用Map表示,通常用于框架集成,例如:实现一个通用的服务测试框架,可通过GenericService调用所有服务实现。

dubbo异步调用

在这里插入图片描述

<dubbo:reference id="demoService"   interface="com.test.dubbo.ServiceDemo">
      <dubbo:method name="get" async="true" />
  </dubbo:reference>

方法标记未异步方法之后,可以通过

		Person person=demoServer.get("name", "123");
    	System.err.println("立即返回的为null:"+person);
    	//拿到调用的Future引用,当结果返回后,会被通知和设置到此Future。
        Future<Person> pFuture = RpcContext.getContext().getFuture();
        //如果Person已返回,直接拿到返回值,否则线程wait,等待Person返回后,线程会被notify唤醒。
        person = pFuture.get();

RpcContext.getContext()其实是通过ThreadLocal读取的当前线程的rpc上下文

 private static final ThreadLocal<RpcContext> LOCAL = new ThreadLocal<RpcContext>() {
        protected RpcContext initialValue() {
            return new RpcContext();
        }
    };
    private Future<?> future;
    private List<URL> urls;
    private URL url;
    private String methodName;
    private Class<?>[] parameterTypes;
    private Object[] arguments;
    private InetSocketAddress localAddress;
    private InetSocketAddress remoteAddress;
    private final Map<String, String> attachments = new HashMap();
    private final Map<String, Object> values = new HashMap();

猜你喜欢

转载自blog.csdn.net/a809918928/article/details/86593378