泛化呼叫
1. Dubbogo 泛化呼叫 Java 伺服器
使用 Triple 協定 + hessian2 序列化方案
請參考 Dubbogo 3.0 泛化呼叫文件
1.1 Java 伺服器啟動
- 傳輸結構定義
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();
}
- 介面定義
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 客戶端泛化呼叫
這裡展示以 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)