Dubbo 泛化测试
首先定义注册中的配置
Java // 注册中心配置 String address = “zookeeper://zk1test1.joymo.tech:5181?backup=zk2test1.joymo.tech:5181,zk3test1.joymo.tech:5181”; String protocol = “zookeeper”; Integer timeout = 30000; String registryGroup = “dubbo”; String applicationName = “DubboSample”; |
---|
接着将这些配置写入注册中心
| Java
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(applicationName);
referenceConfig.setApplication(applicationConfig);
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(address);
registryConfig.setProtocol(protocol);
registryConfig.setGroup(registryGroup);
registryConfig.setTimeout(timeout); |
| —- |
然后是消费者的配置
JavaScript // 准备两个接口 String interface1 = “tech.joymo.centermember.facade.verifiction.VerificationFacade”; String interface2 = “tech.joymo.centerorder.facade.product.OrderProductFacade”; String interface1 = “tech.joymo.centermember.facade.verifiction.VerificationFacade”; String interface2 = “tech.joymo.centerorder.facade.product.OrderProductFacade”; String version = “1.0.0”; String loadBalance = “leastactive”; String group = “dubbo”; String rpcProtocol = “dubbo”; |
---|
组装消费者
Java ReferenceConfig referenceConfig = new ReferenceConfig(); referenceConfig.setApplication(applicationConfig); referenceConfig.setRegistry(registryConfig); referenceConfig.setProtocol(rpcProtocol); referenceConfig.setVersion(version); referenceConfig.setTimeout(timeout); referenceConfig.setAsync(false); referenceConfig.setLoadbalance(loadBalance); referenceConfig.setGroup(group); |
---|
声明泛化接口
Java // 声明泛化接口 referenceConfig.setGeneric(“true”); // 设置缓存 ReferenceConfigCache cache = ReferenceConfigCache.getCache(address); GenericService genericService = (GenericService) cache.get(referenceConfig); |
---|
接下来测试第一个接口,因为这个接口是多租户的概念,所以要设置一下 token
Java Map attachment.put(“tenantsecretKey”,”c5c36892909e37fa5fc9”); RpcContext._getContext().setAttachments(attachment); |
---|
然后开始测试
| Java
String method = “getVerificationFacade”;
Map
paramters.put(“class”,”tech.joymo.centermember.dto.verification.VerificationDTO”);
paramters.put(“mobile”,”13200005598”);
paramters.put(“bizType”,2);
paramters.put(“length”,6);
paramters.put(“minute”, 100);
Object o = genericService.$invoke(
method,
new String[] {
“tech.joymo.centermember.dto.verification.VerificationDTO”
},
new Object[]{
paramters
}
);
System.out.println(JSON.toJSONString(o)); |
| —- |
返回结果:
9ccbca092cc5c093d477e6f42be28f4f
完整代码如下:
| Java
package tech.joymo.www;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.service.GenericService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author: wenzhongwei
* @since: 2021/4/10
*/
public class TestDubbo {
public static void main(String[] args) {
// 注册中心配置
String address = “zookeeper://zk1test1.joymo.tech:5181?backup=zk2test1.joymo.tech:5181,zk3test1.joymo.tech:5181”;
String protocol = “zookeeper”;
String rpcProtocol = “dubbo”;
Integer timeout = 30000;
String registryGroup = “dubbo”;
String applicationName = “DubboSample”;
// reference 配置
String interface1 = “tech.joymo.centermember.facade.verifiction.VerificationFacade”;
String interface2 = “tech.joymo.centerorder.facade.product.OrderProductFacade”;
String version = “1.0.0”;
String loadBalance = “leastactive”;
String group = “dubbo”;
// 注册中心
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(applicationName);
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(address);
registryConfig.setProtocol(protocol);
registryConfig.setGroup(registryGroup);
registryConfig.setTimeout(timeout);
// 消费者
ReferenceConfig referenceConfig = new ReferenceConfig();
referenceConfig.setApplication(applicationConfig);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setProtocol(rpcProtocol);
referenceConfig.setVersion(version);
referenceConfig.setTimeout(timeout);
referenceConfig.setAsync(false);
referenceConfig.setLoadbalance(loadBalance);
referenceConfig.setGroup(group);
<br /> // ========= 开始测试 =====================<br /> referenceConfig.setInterface(interface1);<br /> <br /> // 声明为泛化接口<br /> referenceConfig.setGeneric("true");<br /> ReferenceConfigCache cache = ReferenceConfigCache._getCache_(address);<br /> GenericService genericService = (GenericService) cache.get(referenceConfig);<br /> // 设置租户<br /> Map<String, String> attachment = new HashMap<>();<br /> attachment.put("tenant_secretKey","c5c36892909e37fa5fc9");<br /> RpcContext._getContext_().setAttachments(attachment);<br /> <br /> // 设置参数<br /> String method = "getVerificationFacade";<br /> Map<String, Object> paramters = new HashMap<>();<br /> paramters.put("class","tech.joymo.centermember.dto.verification.VerificationDTO");<br /> paramters.put("mobile","13200005598");<br /> paramters.put("bizType",2);<br /> paramters.put("length",6);<br /> paramters.put("minute", 100);<br /> <br /> // 执行测试<br /> Object o = genericService.$invoke(<br /> method,<br /> new String[] {<br /> "tech.joymo.centermember.dto.verification.VerificationDTO"<br /> },<br /> new Object[]{<br /> paramters<br /> }<br /> );<br /> System._out_.println(JSON._toJSONString_(o));<br />// System.out.println(referenceConfig.toString());
// Object result = genericService.$invoke(
// “queryOrderProductByOrderNo”,
// new String[]{“java.lang.String”},
// new Object[]{“NO20210409203900000340”}
// );
//System.out.println(JSON.toJSONString(result));
}<br />} |
| —- |