Tsukikage7 opened a new pull request, #1016:
URL: https://github.com/apache/dubbo-go-samples/pull/1016
## What this PR does
This PR refactors and enhances the **generic call sample** to demonstrate
both Dubbo protocol and Triple protocol generic calls between Go and Java.
### Background
The existing generic sample only demonstrated Dubbo protocol generic call.
With the new Triple protocol generic call support in dubbo-go (see
apache/dubbo-go#3154), this sample needs to be updated to showcase both
protocols.
### Changes Overview
#### 1. Dual Protocol Support
The sample now demonstrates generic call on **both protocols**:
| Protocol | Port | Group | Description |
|----------|------|-------|-------------|
| Dubbo | 20000 | `dubbo` | Traditional Dubbo binary protocol |
| Triple | 50052 | `triple` | HTTP/2 based Triple protocol |
#### 2. Java Server Enhancement
**Before**: Single protocol, ZooKeeper dependency
**After**: Dual protocol, direct connection mode
```java
// ApiProvider.java - Now exposes both protocols
public class ApiProvider {
private static final int DUBBO_PORT = 20000;
private static final int TRIPLE_PORT = 50052;
public static void main(String[] args) {
// Dubbo protocol service (group=dubbo)
ServiceConfig<UserProvider> dubboService = new ServiceConfig<>();
dubboService.setGroup("dubbo");
dubboService.setProtocol(new ProtocolConfig("dubbo", DUBBO_PORT));
// Triple protocol service (group=triple)
ServiceConfig<UserProvider> tripleService = new ServiceConfig<>();
tripleService.setGroup("triple");
tripleService.setProtocol(new ProtocolConfig("tri", TRIPLE_PORT));
// Bootstrap both services...
}
}
```
#### 3. Go Client Enhancement
**Before**: Only Dubbo protocol test
**After**: Tests both Dubbo and Triple protocols
```go
func main() {
// Test Dubbo protocol generic call
logger.Info("=== Testing Dubbo Protocol Generic Call ===")
testDubboProtocol(ins)
// Test Triple protocol generic call
logger.Info("=== Testing Triple Protocol Generic Call ===")
testTripleProtocol(ins)
}
func testDubboProtocol(ins *dubbo.Instance) {
cli, _ := ins.NewClient(
client.WithClientProtocolDubbo(),
client.WithClientSerialization(constant.Hessian2Serialization),
)
conn, _ := cli.Dial(UserProvider,
client.WithURL("dubbo://127.0.0.1:20000"),
client.WithGeneric(),
client.WithGroup("dubbo"),
)
runGenericTests(conn, "Dubbo")
}
func testTripleProtocol(ins *dubbo.Instance) {
cli, _ := ins.NewClient(
client.WithClientProtocolTriple(),
client.WithClientSerialization(constant.Hessian2Serialization),
)
conn, _ := cli.Dial(UserProvider,
client.WithURL("tri://127.0.0.1:50052"),
client.WithGeneric(),
client.WithGroup("triple"),
)
runGenericTests(conn, "Triple")
}
```
#### 4. Comprehensive Test Suite
Added `client_test.go` with 19 test cases:
**Dubbo Protocol Tests (6 cases)**:
- `TestDubboGenericCall_StringArg`
- `TestDubboGenericCall_MultipleArgs`
- `TestDubboGenericCall_IntArg`
- `TestDubboGenericCall_NoArgs`
- `TestDubboGenericCall_ArrayArg`
- `TestDubboGenericCall_POJOArg`
**Triple Protocol Tests (13 cases)**:
- `TestTripleGenericCall_StringArg`
- `TestTripleGenericCall_MultipleArgs`
- `TestTripleGenericCall_IntArg`
- `TestTripleGenericCall_NoArgs`
- `TestTripleGenericCall_ArrayArg`
- `TestTripleGenericCall_POJOArg`
- `TestTripleGenericCall_POJOArrayArg`
- `TestTripleGenericCall_MapResult`
- `TestTripleGenericCall_ListResult`
- `TestTripleGenericCall_EmptyArrayArg`
- `TestTripleGenericCall_NonExistentMethod`
- `TestTripleGenericCall_WithTimeout`
- `TestTripleGenericCall_WithCancelledContext`
**Benchmarks**:
- `BenchmarkDubboGenericCall`
- `BenchmarkTripleGenericCall`
#### 5. Project Structure Simplification
**Before** (nested directories):
```
generic/
├── java-server/
│ └── java-server/ # Redundant nesting
│ ├── pom.xml
│ └── src/
├── java-client/
│ └── java-client/ # Redundant nesting
│ ├── pom.xml
│ └── src/
```
**After** (flat structure):
```
generic/
├── java-server/
│ ├── pom.xml
│ ├── run.sh
│ └── src/
├── java-client/
│ ├── pom.xml
│ ├── run.sh
│ └── src/
├── go-server/
│ └── cmd/
├── go-client/
│ └── cmd/
│ ├── client.go
│ └── client_test.go
```
#### 6. Removed ZooKeeper Dependency
- Changed from registry-based service discovery to direct URL connection
- Simplifies local testing and CI/CD pipelines
- No external dependencies required to run the sample
### Files Changed
| File | Change Type | Description |
|------|-------------|-------------|
| `generic/java-server/src/.../ApiProvider.java` | Modified | Dual protocol
support |
| `generic/java-client/src/.../ApiTripleConsumer.java` | Added | Triple
protocol consumer |
| `generic/go-client/cmd/client.go` | Modified | Both protocol tests |
| `generic/go-client/cmd/client_test.go` | Added | 19 test cases +
benchmarks |
| `generic/go-server/cmd/server.go` | Modified | Enhanced server |
| `generic/go-server/pkg/user_provider.go` | Modified | Cleaner
implementation |
| `generic/java-server/run.sh` | Added | Easy run script |
| `generic/java-client/run.sh` | Added | Easy run script |
| `generic/README.md` | Modified | Updated documentation |
| `generic/README_zh.md` | Modified | Updated Chinese documentation |
| `generic/java-*/java-*/` | Deleted | Removed redundant nesting |
| `generic/build/test.sh` | Deleted | Obsolete test script |
### How to Test
#### 1. Start Java Server
```bash
cd generic/java-server
./run.sh
```
#### 2. Run Go Client Tests
```bash
cd generic/go-client/cmd
go test -v
```
#### 3. Or Run Go Client Manually
```bash
cd generic/go-client/cmd
go run client.go
```
### Test Results
```
=== Testing Dubbo Protocol Generic Call ===
[Dubbo] GetUser1(userId string) res: map[age:48
class:org.apache.dubbo.samples.User ...]
[Dubbo] GetUser2(userId string, name string) res: map[age:48 ...]
[Dubbo] GetUser3(userCode int) res: map[age:48 ...]
[Dubbo] GetUser4(userCode int, name string) res: map[age:48 ...]
[Dubbo] GetOneUser() res: map[age:48 ...]
[Dubbo] GetUsers(userIdList []string) res: [map[age:48 ...] map[age:48 ...]
...]
[Dubbo] GetUsersMap(userIdList []string) res: map[001:map[age:48 ...] ...]
[Dubbo] QueryAll() res: [map[age:48 ...] map[age:48 ...]]
[Dubbo] QueryUser(user *User) res: map[age:25 ...]
[Dubbo] QueryUsers(users []*User) res: [map[age:24 ...] map[age:21 ...]]
=== Testing Triple Protocol Generic Call ===
[Triple] GetUser1(userId string) res: map[age:48 ...]
[Triple] GetUser2(userId string, name string) res: map[age:48 ...]
[Triple] GetUser3(userCode int) res: map[age:48 ...]
[Triple] GetUser4(userCode int, name string) res: map[age:48 ...]
[Triple] GetOneUser() res: map[age:48 ...]
[Triple] GetUsers(userIdList []string) res: [map[age:48 ...] ...]
[Triple] GetUsersMap(userIdList []string) res: map[001:map[age:48 ...] ...]
[Triple] QueryAll() res: [map[age:48 ...] map[age:48 ...]]
[Triple] QueryUser(user *User) res: map[age:25 ...]
[Triple] QueryUsers(users []*User) res: [map[age:24 ...] map[age:21 ...]]
All generic call tests completed
```
### Related PRs
- apache/dubbo-go#3154 - Triple protocol generic call implementation
---
Signed-off-by: TsukiKage <[email protected]>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]