Skip to content

Commit 5c643ce

Browse files
committed
feat(generic): add generic call sample for both Dubbo and Triple protocols
This PR refactors and enhances the generic call sample to demonstrate both Dubbo protocol and Triple protocol generic calls between Go and Java. ## Changes Overview ### 1. Dual Protocol Support | Protocol | Port | Group | Description | |----------|-------|----------|----------------------------------| | Dubbo | 20000 | `dubbo` | Traditional Dubbo binary protocol| | Triple | 50052 | `triple` | HTTP/2 based Triple protocol | ### 2. Direct Connection Mode - Removed ZooKeeper dependency in favor of direct URL connection - Simplifies local testing and CI/CD pipelines - No external dependencies required to run the sample ### 3. Project Structure Simplification - Removed redundant nested directories (java-server/java-server → java-server) - Added run.sh scripts for easy execution ### 4. Comprehensive Test Suite Added client_test.go with test cases covering: - Dubbo Protocol Tests (6 cases) - Triple Protocol Tests (13 cases) - Benchmarks for both protocols ### 5. Code Improvements - Go server uses direct connection mode (no registry) - user_provider.go Invoke method returns proper error for unknown methods - Fixed QueryUsers parameter type to match Java array signature ## Dependencies This PR depends on apache/dubbo-go#3154 which adds `NewGenericService` API. CI will pass after that PR is merged to main branch. Signed-off-by: TsukiKage <chongyanx@163.com>
1 parent 4d00f88 commit 5c643ce

37 files changed

Lines changed: 2717 additions & 1142 deletions

File tree

generic/README.md

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,99 @@
1-
# Generic Call
1+
# Generic Sample (Dubbo & Triple Protocol)
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 both Dubbo and Triple protocols 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 (Triple protocol on :50052)
12+
├── go-client/ # Go consumer with generic invocation
13+
├── java-server/ # Java provider (Dubbo on :20000, Triple on :50052)
14+
└── java-client/ # Java consumer with generic invocation
15+
```
1616

17-
## Prerequisites
17+
## Protocol Support
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+
| Protocol | Port | Group | Description |
20+
|----------|------|-------|-------------|
21+
| Dubbo | 20000 | `dubbo` | Traditional Dubbo binary protocol |
22+
| Triple | 50052 | `triple` | HTTP/2 based Triple protocol |
2223

23-
## Registry
24+
## Run the Java Server (Recommended)
2425

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.
26+
The Java server exposes both Dubbo and Triple protocols using direct connection mode (no registry required).
2627

27-
```shell
28-
# Start ZooKeeper registry
29-
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.4.14
28+
```bash
29+
cd generic/java-server
30+
./run.sh
31+
# Or use Maven directly:
32+
# mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiProvider"
3033
```
3134

32-
## Running the Examples
35+
## Run the Go Client
3336

34-
### Dubbo-Go Server
37+
The client connects directly to the server using URLs via `client.WithURL()` to perform generic calls.
3538

36-
Using Dubbo-Go as provider, you can start it from command line tool:
39+
```bash
40+
cd generic/go-client/cmd
41+
go run .
42+
```
43+
44+
## Run the Go Server
3745

38-
```shell
39-
cd go-server/cmd && go run server.go
46+
```bash
47+
cd generic/go-server/cmd
48+
go run .
4049
```
4150

42-
### Dubbo-Go Client (Generic Call)
51+
The Go server listens on port `50052` with Triple protocol.
4352

44-
Using Dubbo-Go as consumer with generic calls:
53+
## Run the Java Client
4554

46-
```shell
47-
cd go-client/cmd && go run client.go
55+
```bash
56+
cd generic/java-client
57+
./run.sh
58+
# Or use Maven directly:
59+
# mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiTripleConsumer"
4860
```
4961

50-
### Dubbo Java Server
62+
## Tested Methods
5163

52-
Using Dubbo Java as provider:
64+
| Method | Parameters | Return |
65+
|--------|------------|--------|
66+
| GetUser1 | String | User |
67+
| GetUser2 | String, String | User |
68+
| GetUser3 | int | User |
69+
| GetUser4 | int, String | User |
70+
| GetOneUser | - | User |
71+
| GetUsers | String[] | User[] |
72+
| GetUsersMap | String[] | Map<String, User> |
73+
| QueryUser | User | User |
74+
| QueryUsers | User[] | User[] |
75+
| QueryAll | - | Map<String, User> |
5376

54-
```shell
55-
cd java-server/java-server
56-
mvn clean package
57-
sh run.sh
58-
```
77+
## Expected Output
5978

60-
### Dubbo Java Client
79+
Server log:
6180

62-
Using Dubbo Java as consumer:
81+
```
82+
Generic Go/Java server started
83+
Dubbo protocol on port 20000 (group=dubbo)
84+
Triple protocol on port 50052 (group=triple)
85+
```
86+
87+
Client log:
6388

64-
```shell
65-
cd java-client/java-client
66-
mvn clean package
67-
sh run.sh
6889
```
90+
[Dubbo] GetUser1(String): {id=A003, name=Joe, age=48, ...}
91+
[Triple] GetUser1(String): {id=A003, name=Joe, age=48, ...}
92+
...
93+
All generic call tests completed
94+
```
95+
96+
## Notes
6997

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()
98+
- Do NOT start the Go Server and the Java Server's Triple protocol endpoint on the same host at the same time, because both use port 50052 for Triple.
99+
- This sample uses direct connection mode (no ZooKeeper or other registry required).

generic/README_zh.md

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

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

5-
本示例演示了 Dubbo-GoDubbo Java 服务之间的泛化调用,展示了不同语言实现的服务如何互操作
5+
本示例演示了如何使用 Dubbo 和 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: 集成测试需要的脚本
14-
15-
Dubbo Java 示例可以用来测试与 Dubbo-Go 的互操作性。您可以启动 java 服务端配合 go 客户端,或者启动 go 服务端配合 java 客户端进行测试。
9+
```
10+
generic/
11+
├── go-server/ # Go 服务端 (Triple 协议,端口 :50052)
12+
├── go-client/ # Go 客户端,泛化调用
13+
├── java-server/ # Java 服务端 (Dubbo 端口 :20000, Triple 端口 :50052)
14+
└── java-client/ # Java 客户端,泛化调用
15+
```
1616

17-
## 环境准备
17+
## 协议支持
1818

19-
- Docker 和 Docker Compose 用于运行 ZooKeeper 注册中心
20-
- Go 1.23+ 用于 Dubbo-Go 示例
21-
- Java 8+ 和 Maven 用于 Dubbo Java 示例
19+
| 协议 | 端口 | 分组 | 描述 |
20+
|------|------|------|------|
21+
| Dubbo | 20000 | `dubbo` | 传统 Dubbo 二进制协议 |
22+
| Triple | 50052 | `triple` | 基于 HTTP/2 的 Triple 协议 |
2223

23-
## 注册中心
24+
## 启动 Java 服务端(推荐)
2425

25-
本示例使用 ZooKeeper 作为注册中心。以下命令通过 docker 启动 ZooKeeper,因此需要确保已安装 docker 和 docker-compose
26+
Java 服务端同时暴露 Dubbo 和 Triple 协议,使用直连模式(无需注册中心)
2627

27-
```shell
28-
# 启动 ZooKeeper 注册中心
29-
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.4.14
28+
```bash
29+
cd generic/java-server
30+
./run.sh
31+
# 或直接使用 Maven:
32+
# mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiProvider"
3033
```
3134

32-
## 运行示例
35+
## 启动 Go 客户端
3336

34-
### Dubbo-Go 服务端
37+
客户端通过 `client.WithURL()` 直连服务地址进行泛化调用。
3538

36-
使用 Dubbo-Go 作为服务提供者,可以通过命令行工具启动:
39+
```bash
40+
cd generic/go-client/cmd
41+
go run .
42+
```
43+
44+
## 启动 Go 服务端
3745

38-
```shell
39-
cd go-server/cmd && go run server.go
46+
```bash
47+
cd generic/go-server/cmd
48+
go run .
4049
```
4150

42-
### Dubbo-Go 客户端(泛化调用)
51+
Go 服务端监听 `50052` 端口,使用 Triple 协议。
4352

44-
使用 Dubbo-Go 作为服务消费者进行泛化调用:
53+
## 启动 Java 客户端
4554

46-
```shell
47-
cd go-client/cmd && go run client.go
55+
```bash
56+
cd generic/java-client
57+
./run.sh
58+
# 或直接使用 Maven:
59+
# mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiTripleConsumer"
4860
```
4961

50-
### Dubbo Java 服务端
62+
## 测试方法
5163

52-
使用 Dubbo Java 作为服务提供者:
64+
| 方法 | 参数 | 返回值 |
65+
|------|------|--------|
66+
| GetUser1 | String | User |
67+
| GetUser2 | String, String | User |
68+
| GetUser3 | int | User |
69+
| GetUser4 | int, String | User |
70+
| GetOneUser | - | User |
71+
| GetUsers | String[] | User[] |
72+
| GetUsersMap | String[] | Map<String, User> |
73+
| QueryUser | User | User |
74+
| QueryUsers | User[] | User[] |
75+
| QueryAll | - | Map<String, User> |
5376

54-
```shell
55-
cd java-server/java-server
56-
mvn clean package
57-
sh run.sh
58-
```
77+
## 预期输出
5978

60-
### Dubbo Java 客户端
79+
服务端日志:
6180

62-
使用 Dubbo Java 作为服务消费者:
81+
```
82+
Generic Go/Java server started
83+
Dubbo protocol on port 20000 (group=dubbo)
84+
Triple protocol on port 50052 (group=triple)
85+
```
86+
87+
客户端日志:
6388

64-
```shell
65-
cd java-client/java-client
66-
mvn clean package
67-
sh run.sh
6889
```
90+
[Dubbo] GetUser1(String): {id=A003, name=Joe, age=48, ...}
91+
[Triple] GetUser1(String): {id=A003, name=Joe, age=48, ...}
92+
...
93+
All generic call tests completed
94+
```
95+
96+
## 注意事项
6997

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()
98+
- 不要同时在同一主机上启动 Go 服务端和 Java 服务端的 Triple 协议,因为两者都使用 50052 端口。
99+
- 本示例使用直连模式(无需 ZooKeeper 或其他注册中心)。

0 commit comments

Comments
 (0)