泛化调用

1. Dubbogo 泛化调用 Java 服务器

使用 Triple 协议 + hessian2 序列化方案

请参考 Dubbogo 3.0 泛化调用文档

1.1 Java-Server 启动

  1. 传输结构定义
package org.apache.dubbo;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
private String id;

  private String name;

  private int age;

  private Date time = new Date();
}
  1. 接口定义
package org.apache.dubbo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
//import org.apache.dubbo.rpc.filter.GenericFilter;

public interface UserProvider {
User GetUser1(String userId);
}

1.2 Go-Client 泛化调用

以 API 形式构建泛化接口引用,如下所示

// Initialize the Reference configuration
refConf := config. NewReferenceConfigBuilder().
  SetInterface("org. apache. dubbo. UserProvider").
  SetRegistryIDs("zk").
  SetProtocol(tripleConst.TRIPLE).
  SetGeneric(true).
  SetSerialization("hessian2").
  build()

// Construct the Root configuration and import the registry module
rootConfig := config. NewRootConfigBuilder().
  AddRegistry("zk", config. NewRegistryConfigWithProtocolDefaultPort("zookeeper")).
  build()

// Reference configuration initialization, because you need to use the registry for service discovery, you need to pass in the configured rootConfig
if err := refConf.Init(rootConfig); err != nil{
  panic(err)
}

// Generalized call loading, service discovery
refConf. GenericLoad(appName)

time. Sleep(time. Second)

// Initiate generalization call
resp, err := refConf.GetRPCService().(*generic.GenericService).Invoke(
  context. TODO(),
  "getUser1",
  []string{"java. lang. String"},
  []hessian. Object{"A003"},
)

if err != nil {
  panic(err)
}
logger.Infof("GetUser1(userId string) res: %+v", resp)

GenericService 的 Invoke 方法包含三个参数:context.Context、[]string、[]hessian.Object,

第二个参数是对应参数的 Java 类名,例如 java.lang.String、org.apache.dubbo.User,第三个参数是参数列表,hessian.Object 是接口。第二个和第三个参数应与方法签名一致并按顺序对应。

获取 map 结构的返回结果

INFO cmd/client.go:89 GetUser1(userId string) res: map[age:48 class:org.apache.dubbo.User id:A003 name:Joe sex:MAN time:2021-10-04 14:03:03.37 +0800 CST]

上次修改时间:2023 年 1 月 2 日:增强 Dubbogo 文档 (#1800) (71c8e722740)