Dubbo隐式传参

dubbo通过客户端向服务器端传递参数,传递参数时path,group,version,dubbo,token,timeout即可key有特殊处理,不能使用这几个特殊key
服务提供方使用RpcContext.getContext()
.getAttachments();获取参数,
服务器消费方使用
RpcContext.getContext().setAttachment(); 传递参数。
一下是测试代码:

package com.yncp.dubbo.service;
public interface IDubboParmService {     
    public String print();  
}
package com.yncp.dubbo.service.impl;

import java.util.Map;

import com.alibaba.dubbo.rpc.RpcContext;
import com.yncp.dubbo.service.IDubboParmService;

public class DubboParmServiceImpl implements IDubboParmService {
    public String print() {
        Map attachments = RpcContext.getContext()
                .getAttachments();
        String parm = attachments.get("parm");
        System.out.println("parm:" + parm);
        return "嗨";
    }
}

消费方测试:

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.rpc.RpcContext;
import com.yncp.dubbo.service.IDubboParmService;


public class DubboStart {
    public static void main(String[] args) throws IOException {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //RpcContext.getContext().setAttachment("parm", "嘿嘿");
        IDubboParmService dubboParmService=(IDubboParmService) ctx.getBean("dubboParmService");
        // setAttachment设置的KV,在完成下面一次远程调用会被清空。即多次远程调用要多次设置。
        for(int i=0;i<5;i++){ 
            RpcContext.getContext().setAttachment("parm", "嘿嘿"+i);
            dubboParmService.print();
        }

    }
}

你可能感兴趣的:(dubbo,Java)