Skip to content

Commit 9babfa4

Browse files
committed
feat(generic): add generic call sample for both Dubbo and Triple protocols
- Support both Dubbo protocol (port 20000) and Triple protocol (port 50052) - Java server exposes services with different groups (dubbo/triple) - Go client uses NewGenericService API for generic calls - Add comprehensive integration tests for both protocols - Simplify project structure (remove nested java-*/java-*/ directories) - Remove ZooKeeper dependency, use direct connection mode Test coverage: - Dubbo protocol: 6 test cases (basic types, collections, POJO) - Triple protocol: 13 test cases (basic, collections, POJO, edge cases, errors) - Benchmarks for both protocols Signed-off-by: TsukiKage <chongyanx@163.com>
1 parent 4d00f88 commit 9babfa4

File tree

38 files changed

+2714
-1131
lines changed

38 files changed

+2714
-1131
lines changed

generic/README.md

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,97 @@
1-
# Generic Call
1+
# Generic Sample (Triple Generic Call)
22

3-
Generic call is a mechanism that ensures information is correctly transmitted when the client does not have interface information. It generalizes POJOs into generic formats (such as dictionaries, strings), and is generally used in scenarios like integration testing and gateways.
3+
[English](README.md) | [中文](README_zh.md)
44

5-
This example demonstrates generic calls between Dubbo-Go and Dubbo Java services, showing how services can interoperate regardless of the language they're implemented in.
5+
This sample demonstrates how to use generic invocation with the Triple protocol for Go-Java interoperability. Generic invocation allows calling remote services without generating stubs or having the service interface locally.
66

7-
## Directory Structure
7+
## Layout
88

9-
- go-server: Dubbo-Go server example
10-
- go-client: Dubbo-Go client example with generic calls
11-
- java-client: Dubbo Java client example
12-
- java-server: Dubbo Java server example
13-
- build: For integration test
14-
15-
Dubbo Java examples can be used to test interoperability with Dubbo-Go. You can start java server with go client, or go server with java client for testing.
9+
```
10+
generic/
11+
├── go-server/ # Go provider listening on :50052
12+
├── go-client/ # Go consumer with generic invocation
13+
├── java-server/ # Java provider listening on :50052
14+
└── java-client/ # Java consumer with generic invocation
15+
```
1616

1717
## Prerequisites
1818

19-
- Docker and Docker Compose for running ZooKeeper registry
20-
- Go 1.23+ for Dubbo-Go examples
21-
- Java 8+ and Maven for Dubbo Java examples
19+
Start ZooKeeper:
2220

23-
## Registry
21+
```bash
22+
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.8
23+
```
2424

25-
This example uses ZooKeeper as the registry. The following command starts ZooKeeper from docker, so you need to ensure that docker and docker-compose are installed first.
25+
## Run the Go Server
2626

27-
```shell
28-
# Start ZooKeeper registry
29-
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.4.14
27+
```bash
28+
cd generic/go-server/cmd
29+
go run .
3030
```
3131

32-
## Running the Examples
32+
The server listens on port `50052` and registers to ZooKeeper.
3333

34-
### Dubbo-Go Server
34+
## Run the Go Client
3535

36-
Using Dubbo-Go as provider, you can start it from command line tool:
37-
38-
```shell
39-
cd go-server/cmd && go run server.go
36+
```bash
37+
cd generic/go-client/cmd
38+
go run .
4039
```
4140

42-
### Dubbo-Go Client (Generic Call)
41+
The client discovers the service via ZooKeeper and uses `client.WithGenericType("true")` to perform generic calls.
4342

44-
Using Dubbo-Go as consumer with generic calls:
43+
## Run the Java Server
4544

46-
```shell
47-
cd go-client/cmd && go run client.go
48-
```
45+
Build and run from the java-server directory:
4946

50-
### Dubbo Java Server
47+
```bash
48+
cd generic/java-server
49+
mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiProvider"
50+
```
5151

52-
Using Dubbo Java as provider:
52+
## Run the Java Client
5353

54-
```shell
55-
cd java-server/java-server
56-
mvn clean package
57-
sh run.sh
54+
```bash
55+
cd generic/java-client
56+
mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiTripleConsumer"
5857
```
5958

60-
### Dubbo Java Client
59+
The client uses `reference.setGeneric("true")` to perform generic calls.
60+
61+
## Tested Methods
6162

62-
Using Dubbo Java as consumer:
63+
| Method | Parameters | Return |
64+
|--------|------------|--------|
65+
| GetUser1 | String | User |
66+
| GetUser2 | String, String | User |
67+
| GetUser3 | int | User |
68+
| GetUser4 | int, String | User |
69+
| GetOneUser | - | User |
70+
| GetUsers | String[] | User[] |
71+
| GetUsersMap | String[] | Map<String, User> |
72+
| QueryUser | User | User |
73+
| QueryUsers | User[] | User[] |
74+
| QueryAll | - | Map<String, User> |
75+
76+
## Expected Output
77+
78+
Server log:
79+
80+
```
81+
Generic Go/Java server started on port 50052
82+
Registry: zookeeper://127.0.0.1:2181
83+
```
84+
85+
Client log:
6386

64-
```shell
65-
cd java-client/java-client
66-
mvn clean package
67-
sh run.sh
6887
```
88+
[PASS] GetUser1(String): {id=A003, name=Joe, age=48, ...}
89+
[PASS] GetUser2(String, String): {id=A003, name=lily, age=48, ...}
90+
...
91+
[OK] All tests passed!
92+
```
93+
94+
## Notes
6995

70-
## Testing Interoperability
71-
72-
This example is designed to test interoperability between Dubbo-Go and Dubbo Java:
73-
74-
1. Start the ZooKeeper registry
75-
2. Start either go-server or java-server
76-
3. Run either go-client or java-client to test the generic calls
77-
78-
The client will make various generic calls to the server, including:
79-
- GetUser1(String userId)
80-
- GetUser2(String userId, String name)
81-
- GetUser3(int userCode)
82-
- GetUser4(int userCode, String name)
83-
- GetOneUser()
84-
- GetUsers(String[] userIdList)
85-
- GetUsersMap(String[] userIdList)
86-
- QueryUser(User user)
87-
- QueryUsers(List<User> userObjectList)
88-
- QueryAll()
96+
- Do NOT start Go Server and Java Server at the same time. Both listen on port 50052.
97+
- Make sure ZooKeeper is running before starting the server or client.

generic/README_zh.md

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,97 @@
1-
# 泛化调用
1+
# 泛化调用示例 (Triple 协议)
22

3-
泛化调用是在客户端没有接口信息时保证信息被正确传递的手段,即把 POJO 泛化为通用格式(如字典、字符串),一般被用于集成测试、网关等场景。
3+
[English](README.md) | [中文](README_zh.md)
44

5-
本示例演示了 Dubbo-Go 和 Dubbo Java 服务之间的泛化调用,展示了不同语言实现的服务如何互操作
5+
本示例演示了如何使用 Triple 协议进行泛化调用,实现 Go 和 Java 服务之间的互操作。泛化调用允许在没有服务接口定义的情况下调用远程服务
66

77
## 目录结构
88

9-
- go-server: Dubbo-Go 服务端示例
10-
- go-client: Dubbo-Go 客户端示例(泛化调用)
11-
- java-client: Dubbo Java 客户端示例
12-
- java-server: Dubbo Java 服务端示例
13-
- build: 集成测试需要的脚本
9+
```
10+
generic/
11+
├── go-server/ # Go 服务端,监听 :50052
12+
├── go-client/ # Go 客户端,泛化调用
13+
├── java-server/ # Java 服务端,监听 :50052
14+
└── java-client/ # Java 客户端,泛化调用
15+
```
1416

15-
Dubbo Java 示例可以用来测试与 Dubbo-Go 的互操作性。您可以启动 java 服务端配合 go 客户端,或者启动 go 服务端配合 java 客户端进行测试。
17+
## 前置条件
1618

17-
## 环境准备
19+
启动 ZooKeeper:
1820

19-
- Docker 和 Docker Compose 用于运行 ZooKeeper 注册中心
20-
- Go 1.23+ 用于 Dubbo-Go 示例
21-
- Java 8+ 和 Maven 用于 Dubbo Java 示例
21+
```bash
22+
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.8
23+
```
2224

23-
## 注册中心
25+
## 启动 Go 服务端
2426

25-
本示例使用 ZooKeeper 作为注册中心。以下命令通过 docker 启动 ZooKeeper,因此需要确保已安装 docker 和 docker-compose。
27+
```bash
28+
cd generic/go-server/cmd
29+
go run .
30+
```
2631

27-
```shell
28-
# 启动 ZooKeeper 注册中心
29-
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.4.14
32+
服务端监听 `50052` 端口,并注册到 ZooKeeper。
33+
34+
## 启动 Go 客户端
35+
36+
```bash
37+
cd generic/go-client/cmd
38+
go run .
3039
```
3140

32-
## 运行示例
41+
客户端通过 ZooKeeper 发现服务,使用 `client.WithGenericType("true")` 进行泛化调用。
3342

34-
### Dubbo-Go 服务端
43+
## 启动 Java 服务端
3544

36-
使用 Dubbo-Go 作为服务提供者,可以通过命令行工具启动
45+
在 java-server 目录下构建并运行
3746

38-
```shell
39-
cd go-server/cmd && go run server.go
47+
```bash
48+
cd generic/java-server
49+
mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiProvider"
4050
```
4151

42-
### Dubbo-Go 客户端(泛化调用)
43-
44-
使用 Dubbo-Go 作为服务消费者进行泛化调用:
52+
## 启动 Java 客户端
4553

46-
```shell
47-
cd go-client/cmd && go run client.go
54+
```bash
55+
cd generic/java-client
56+
mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiTripleConsumer"
4857
```
4958

50-
### Dubbo Java 服务端
59+
客户端使用 `reference.setGeneric("true")` 进行泛化调用。
5160

52-
使用 Dubbo Java 作为服务提供者:
61+
## 测试方法
5362

54-
```shell
55-
cd java-server/java-server
56-
mvn clean package
57-
sh run.sh
58-
```
63+
| 方法 | 参数 | 返回值 |
64+
|------|------|--------|
65+
| GetUser1 | String | User |
66+
| GetUser2 | String, String | User |
67+
| GetUser3 | int | User |
68+
| GetUser4 | int, String | User |
69+
| GetOneUser | - | User |
70+
| GetUsers | String[] | User[] |
71+
| GetUsersMap | String[] | Map<String, User> |
72+
| QueryUser | User | User |
73+
| QueryUsers | User[] | User[] |
74+
| QueryAll | - | Map<String, User> |
5975

60-
### Dubbo Java 客户端
76+
## 预期输出
6177

62-
使用 Dubbo Java 作为服务消费者
78+
服务端日志
6379

64-
```shell
65-
cd java-client/java-client
66-
mvn clean package
67-
sh run.sh
6880
```
81+
Generic Go/Java server started on port 50052
82+
Registry: zookeeper://127.0.0.1:2181
83+
```
84+
85+
客户端日志:
86+
87+
```
88+
[PASS] GetUser1(String): {id=A003, name=Joe, age=48, ...}
89+
[PASS] GetUser2(String, String): {id=A003, name=lily, age=48, ...}
90+
...
91+
[OK] All tests passed!
92+
```
93+
94+
## 注意事项
6995

70-
## 测试互操作性
71-
72-
本示例旨在测试 Dubbo-Go 和 Dubbo Java 之间的互操作性:
73-
74-
1. 启动 ZooKeeper 注册中心
75-
2. 启动 go-server 或 java-server 之一
76-
3. 运行 go-client 或 java-client 之一来测试泛化调用
77-
78-
客户端将向服务端发起多种泛化调用,包括:
79-
- GetUser1(String userId)
80-
- GetUser2(String userId, String name)
81-
- GetUser3(int userCode)
82-
- GetUser4(int userCode, String name)
83-
- GetOneUser()
84-
- GetUsers(String[] userIdList)
85-
- GetUsersMap(String[] userIdList)
86-
- QueryUser(User user)
87-
- QueryUsers(List<User> userObjectList)
88-
- QueryAll()
96+
- 不要同时启动 Go 服务端和 Java 服务端,它们都监听 50052 端口。
97+
- 启动服务端或客户端之前,请确保 ZooKeeper 正在运行。

0 commit comments

Comments
 (0)