亲宝软件园·资讯

展开

不用他人jar包调用dubbo

spider集控团队 人气:0

1、正常dubbo调用流程

2、如果想在不引用他人jar包的情况下如何调用呢?

dubbo泛化调用-dubbo原生支持的优雅方法

使用场景:

如何使用:

1.需要知道对方方法的全路径名称(interfaceName)

2.如果对方的dubbo有指定的版本号(version)和组(group)要指定一下,不然有很大可能会调用不通

3.要知道对方的注册中心地址和连接方式(如果是在同一个zk注册中心就更好了)

//1.获取泛化对象
private GenericService getGenericService(String interfaceName, String version, String group) {
    ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
    // set application
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setId(APPLICATION_ID);
    applicationConfig.setName(APPLICATION_ID);
    reference.setApplication(applicationConfig);
    // 通过zookeeper连接
    RegistryConfig registryConfig = new RegistryConfig();
    registryConfig.setAddress(zkAddress);
    registryConfig.setProtocol(ZOOKEEPER);
    registryConfig.setCheck(false);
    reference.setRegistry(registryConfig);
    // set timeout
    reference.setTimeout(syncForwardTimeout);
    // set retry times
    reference.setRetries(syncRetries);
    // set check
    reference.setCheck(false);
    // set generic
    reference.setGeneric(true);
    // set interface version
    if (version != null && version.length() != 0) {
        reference.setVersion(version);
    }
    // set interface name
    reference.setInterface(interfaceName);
    // set service id
    reference.setId(interfaceName);
    // set dubbo protocol
    reference.setProtocol(DUBBO);
    //
    if (group != null && group.length() != 0) {
        reference.setGroup(group);
    }
    // 从zookeeper获取缓存,从缓存中获取泛化
    return ReferenceConfigCache.getCache().get(reference);
}
//2.泛化调用入参处理
```
private ResponseDataDto<R> invoke(GenericService genericService, UpDataReqDto upDataReqDto, String method) {
    String[] paramTypes = new String[1];
    Object[] paramValues = new Object[1];
    int i = 0;
    paramTypes[i] = upDataReqDto.getClass().getTypeName();
    paramValues[i] = upDataReqDto;
    Object object = genericService.$invoke(method, paramTypes, paramValues);
    return JSON.parseObject(JSON.toJSONString(object), new TypeReference<ResponseDataDto<R>>(){});
}
```

实现原理:

1.  com.alibaba.dubbo.rpc.filter.GenericFilter
2.  com.alibaba.dubbo.rpc.filter.GenericImplFilter
GenericImplFilter:
```
//判断是不是泛化调用
if (isCallingGenericImpl(generic, invocation)) {
    RpcInvocation invocation2 = new RpcInvocation(invocation);
    /**
     * Mark this invocation as a generic impl call, this value will be removed automatically before passing on the wire.
     * See {@link RpcUtils#sieveUnnecessaryAttachments(Invocation)}
     */
    invocation2.put(GENERIC_IMPL_MARKER, true);
    String methodName = invocation2.getMethodName();
    Class<?>[] parameterTypes = invocation2.getParameterTypes();
    Object[] arguments = invocation2.getArguments();
    String[] types = new String[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; i++) {
        types[i] = ReflectUtils.getName(parameterTypes[i]);
    }
    Object[] args;
    if (ProtocolUtils.isBeanGenericSerialization(generic)) {
        args = new Object[arguments.length];
        for (int i = 0; i < arguments.length; i++) {
            args[i] = JavaBeanSerializeUtil.serialize(arguments[i], JavaBeanAccessor.METHOD);
        }
    } else {
        args = PojoUtils.generalize(arguments);
    }
    if (RpcUtils.isReturnTypeFuture(invocation)) {
        invocation2.setMethodName($INVOKE_ASYNC);
    } else {
        invocation2.setMethodName($INVOKE);
    }
    invocation2.setParameterTypes(GENERIC_PARAMETER_TYPES);
    invocation2.setParameterTypesDesc(GENERIC_PARAMETER_DESC);
    invocation2.setArguments(new Object[]{methodName, types, args});
    return invoker.invoke(invocation2);
}
// 普通服务进行通用调用
else if (isMakingGenericCall(generic, invocation)) {
    Object[] args = (Object[]) invocation.getArguments()[2];
    if (ProtocolUtils.isJavaGenericSerialization(generic)) {
        for (Object arg : args) {
            if (byte[].class != arg.getClass()) {
                error(generic, byte[].class.getName(), arg.getClass().getName());
            }
        }
    } else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
        for (Object arg : args) {
            if (!(arg instanceof JavaBeanDescriptor)) {
                error(generic, JavaBeanDescriptor.class.getName(), arg.getClass().getName());
            }
        }
    }
    invocation.setAttachment(
            GENERIC_KEY, invoker.getUrl().getParameter(GENERIC_KEY));
}
GenericFilter:
if ((inv.getMethodName().equals($INVOKE) || inv.getMethodName().equals($INVOKE_ASYNC))
    && inv.getArguments() != null
    && inv.getArguments().length == 3
    && !GenericService.class.isAssignableFrom(invoker.getInterface())){
    .......泛化调用处理逻辑......
    }

3、总结

加载全部内容

相关教程
猜你喜欢
用户评论