如何在不使用他人jar包的情况下优雅的进行dubbo调用

1、正常dubbo调用流程

引入dubbo依赖
引入他人提供的clinet依赖包;
配置相同的注册中心,使用@Reference注解注入对应的service接口(注意是Reference是dubbo包下的,不是spring那个)

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

dubbo泛化调用-dubbo原生支持的优雅方法
使用场景:

消费者不希望引入生产者提供的clinet依赖;
例如:消费者是一个基础服务,需要调用很多生产者的dubbo方法,那他就要引入很多的jar包;

消费者不使用java语言,而是使用其他的语言(例如:Python);

如何使用:
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>>(){});

}

实现原理:

泛化调用的实现主要涉及两个filter

  1. com.alibaba.dubbo.rpc.filter.GenericFilter
  2. com.alibaba.dubbo.rpc.filter.GenericImplFilter
    复制代码

泛化调用的流程:
调用 <-> GenericImplFilter <-> 网络(RPC) <-> GenericFilter <-> 服务实现

泛化调用的核心源码:

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、总结

从源码设计来看泛化调用提供了在没有接口依赖的情况下进行调用的解决方案;
从扩展上来看泛化调用的侵入性不强,可以很好的用于框架扩展;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dubbo 2.5.3 全部jar包下载 [INFO] dubbo-parent ...................................... SUCCESS [1.042s] [INFO] Hessian Lite(Alibaba embed version) ............... SUCCESS [4.438s] [INFO] dubbo-common ...................................... SUCCESS [9.153s] [INFO] dubbo-container ................................... SUCCESS [0.019s] [INFO] dubbo-container-api ............................... SUCCESS [1.557s] [INFO] dubbo-container-spring ............................ SUCCESS [1.378s] [INFO] dubbo-container-jetty ............................. SUCCESS [1.448s] [INFO] dubbo-container-log4j ............................. SUCCESS [1.566s] [INFO] dubbo-container-logback ........................... SUCCESS [1.775s] [INFO] dubbo-remoting .................................... SUCCESS [0.151s] [INFO] dubbo-remoting-api ................................ SUCCESS [6.705s] [INFO] dubbo-remoting-netty .............................. SUCCESS [5.750s] [INFO] dubbo-remoting-mina ............................... SUCCESS [2.109s] [INFO] dubbo-remoting-grizzly ............................ SUCCESS [0.989s] [INFO] dubbo-remoting-p2p ................................ SUCCESS [2.322s] [INFO] dubbo-remoting-http ............................... SUCCESS [3.506s] [INFO] dubbo-remoting-zookeeper .......................... SUCCESS [1.279s] [INFO] dubbo-rpc ......................................... SUCCESS [0.015s] [INFO] dubbo-rpc-api ..................................... SUCCESS [3.413s] [INFO] dubbo-rpc-default ................................. SUCCESS [4.105s] [INFO] dubbo-rpc-injvm ................................... SUCCESS [3.112s] [INFO] dubbo-rpc-rmi ..................................... SUCCESS [1.349s] [INFO] dubbo-rpc-hessian ................................. SUCCESS [1.721s] [INFO] dubbo-rpc-http .................................... SUCCESS [1.199s] [INFO] dubbo-rpc-webservice .............................. SUCCESS [0.997s] [INFO] dubbo-cluster ..................................... SUCCESS [6.085s] [IN

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值