Skip to content

Commit 6a8f438

Browse files
committed
STDIN
1 feat(generic): add generic call sample for both Dubbo and Triple protocols 2 3 Signed-off-by: TsukiKage <chongyanx@163.com>
1 parent a6246b2 commit 6a8f438

File tree

11 files changed

+282
-1526
lines changed

11 files changed

+282
-1526
lines changed

generic/README.md

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

33
[English](README.md) | [中文](README_zh.md)
44

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.
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

77
## Layout
88

99
```
1010
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
11+
├── go-server/ # Go provider (Triple protocol, port 50052)
12+
├── go-client/ # Go consumer with generic invocation (direct connection)
13+
├── java-server/ # Java provider (Triple protocol, port 50052)
1414
└── java-client/ # Java consumer with generic invocation
1515
```
1616

1717
## Prerequisites
1818

19-
Start ZooKeeper:
19+
Start ZooKeeper (required by the server for service registration):
2020

2121
```bash
2222
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.8
@@ -29,7 +29,7 @@ cd generic/go-server/cmd
2929
go run .
3030
```
3131

32-
The server listens on port `50052` and registers to ZooKeeper.
32+
The server exposes the Triple protocol on port `50052`, registers to ZooKeeper, and serves `UserProvider` with version `1.0.0` and group `triple`.
3333

3434
## Run the Go Client
3535

@@ -38,7 +38,7 @@ cd generic/go-client/cmd
3838
go run .
3939
```
4040

41-
The client discovers the service via ZooKeeper and uses `client.WithGenericType("true")` to perform generic calls.
41+
The client uses direct URL connection (`client.WithURL`) to connect to the server and performs generic calls via `cli.NewGenericService`. It tests both Dubbo protocol (port 20000) and Triple protocol (port 50052).
4242

4343
## Run the Java Server
4444

@@ -78,20 +78,21 @@ The client uses `reference.setGeneric("true")` to perform generic calls.
7878
Server log:
7979

8080
```
81-
Generic Go/Java server started on port 50052
81+
Generic Go server started on port 50052
8282
Registry: zookeeper://127.0.0.1:2181
8383
```
8484

8585
Client log:
8686

8787
```
88-
[PASS] GetUser1(String): {id=A003, name=Joe, age=48, ...}
89-
[PASS] GetUser2(String, String): {id=A003, name=lily, age=48, ...}
88+
[Triple] GetUser1(userId string) res: {id=A003, name=Joe, age=48, ...}
89+
[Triple] GetUser2(userId string, name string) res: {id=A003, name=lily, age=48, ...}
9090
...
91-
[OK] All tests passed!
91+
All generic call tests completed
9292
```
9393

9494
## Notes
9595

9696
- 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.
97+
- The Go server requires ZooKeeper for service registration.
98+
- The Go client uses direct URL connection and does not require ZooKeeper.

generic/README_zh.md

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

33
[English](README.md) | [中文](README_zh.md)
44

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

77
## 目录结构
88

99
```
1010
generic/
11-
├── go-server/ # Go 服务端,监听 :50052
12-
├── go-client/ # Go 客户端,泛化调用
13-
├── java-server/ # Java 服务端,监听 :50052
11+
├── go-server/ # Go 服务端(Triple 协议,端口 50052
12+
├── go-client/ # Go 客户端,泛化调用(直连模式)
13+
├── java-server/ # Java 服务端(Triple 协议,端口 50052
1414
└── java-client/ # Java 客户端,泛化调用
1515
```
1616

1717
## 前置条件
1818

19-
启动 ZooKeeper:
19+
启动 ZooKeeper(服务端注册服务时需要)
2020

2121
```bash
2222
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.8
@@ -29,7 +29,7 @@ cd generic/go-server/cmd
2929
go run .
3030
```
3131

32-
服务端监听 `50052` 端口,并注册到 ZooKeeper。
32+
服务端通过 Triple 协议监听 `50052` 端口,注册到 ZooKeeper,提供 `UserProvider` 服务(version=1.0.0,group=triple)
3333

3434
## 启动 Go 客户端
3535

@@ -38,7 +38,7 @@ cd generic/go-client/cmd
3838
go run .
3939
```
4040

41-
客户端通过 ZooKeeper 发现服务,使用 `client.WithGenericType("true")` 进行泛化调用。
41+
客户端使用直连模式(`client.WithURL`)连接服务端,通过 `cli.NewGenericService` 进行泛化调用。同时测试 Dubbo 协议(端口 20000)和 Triple 协议(端口 50052)
4242

4343
## 启动 Java 服务端
4444

@@ -78,20 +78,21 @@ mvn clean compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.ApiTriple
7878
服务端日志:
7979

8080
```
81-
Generic Go/Java server started on port 50052
81+
Generic Go server started on port 50052
8282
Registry: zookeeper://127.0.0.1:2181
8383
```
8484

8585
客户端日志:
8686

8787
```
88-
[PASS] GetUser1(String): {id=A003, name=Joe, age=48, ...}
89-
[PASS] GetUser2(String, String): {id=A003, name=lily, age=48, ...}
88+
[Triple] GetUser1(userId string) res: {id=A003, name=Joe, age=48, ...}
89+
[Triple] GetUser2(userId string, name string) res: {id=A003, name=lily, age=48, ...}
9090
...
91-
[OK] All tests passed!
91+
All generic call tests completed
9292
```
9393

9494
## 注意事项
9595

9696
- 不要同时启动 Go 服务端和 Java 服务端,它们都监听 50052 端口。
97-
- 启动服务端或客户端之前,请确保 ZooKeeper 正在运行。
97+
- Go 服务端需要 ZooKeeper 进行服务注册。
98+
- Go 客户端使用直连模式,不依赖 ZooKeeper。

generic/go-client/cmd/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"dubbo.apache.org/dubbo-go/v3"
2727
"dubbo.apache.org/dubbo-go/v3/client"
2828
"dubbo.apache.org/dubbo-go/v3/common/constant"
29-
"dubbo.apache.org/dubbo-go/v3/config/generic"
29+
"dubbo.apache.org/dubbo-go/v3/filter/generic"
3030
_ "dubbo.apache.org/dubbo-go/v3/imports"
3131

3232
hessian "github.com/apache/dubbo-go-hessian2"

generic/go-client/cmd/client_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"dubbo.apache.org/dubbo-go/v3"
2828
"dubbo.apache.org/dubbo-go/v3/client"
2929
"dubbo.apache.org/dubbo-go/v3/common/constant"
30-
"dubbo.apache.org/dubbo-go/v3/config/generic"
30+
"dubbo.apache.org/dubbo-go/v3/filter/generic"
3131
_ "dubbo.apache.org/dubbo-go/v3/imports"
3232

3333
hessian "github.com/apache/dubbo-go-hessian2"
@@ -103,7 +103,7 @@ func setupTripleGenericService(t *testing.T) *generic.GenericService {
103103
// Dubbo Protocol Tests
104104
// =============================================================================
105105

106-
func TestDubbo_GenericCall_StringArg(t *testing.T) {
106+
func TestDubboGenericCallStringArg(t *testing.T) {
107107
if testing.Short() {
108108
t.Skip("Skipping integration test in short mode")
109109
}
@@ -125,7 +125,7 @@ func TestDubbo_GenericCall_StringArg(t *testing.T) {
125125
}
126126
}
127127

128-
func TestDubbo_GenericCall_MultipleArgs(t *testing.T) {
128+
func TestDubboGenericCallMultipleArgs(t *testing.T) {
129129
if testing.Short() {
130130
t.Skip("Skipping integration test in short mode")
131131
}
@@ -147,7 +147,7 @@ func TestDubbo_GenericCall_MultipleArgs(t *testing.T) {
147147
}
148148
}
149149

150-
func TestDubbo_GenericCall_IntArg(t *testing.T) {
150+
func TestDubboGenericCallIntArg(t *testing.T) {
151151
if testing.Short() {
152152
t.Skip("Skipping integration test in short mode")
153153
}
@@ -169,7 +169,7 @@ func TestDubbo_GenericCall_IntArg(t *testing.T) {
169169
}
170170
}
171171

172-
func TestDubbo_GenericCall_NoArgs(t *testing.T) {
172+
func TestDubboGenericCallNoArgs(t *testing.T) {
173173
if testing.Short() {
174174
t.Skip("Skipping integration test in short mode")
175175
}
@@ -191,7 +191,7 @@ func TestDubbo_GenericCall_NoArgs(t *testing.T) {
191191
}
192192
}
193193

194-
func TestDubbo_GenericCall_ArrayArg(t *testing.T) {
194+
func TestDubboGenericCallArrayArg(t *testing.T) {
195195
if testing.Short() {
196196
t.Skip("Skipping integration test in short mode")
197197
}
@@ -213,7 +213,7 @@ func TestDubbo_GenericCall_ArrayArg(t *testing.T) {
213213
}
214214
}
215215

216-
func TestDubbo_GenericCall_POJOArg(t *testing.T) {
216+
func TestDubboGenericCallPOJOArg(t *testing.T) {
217217
if testing.Short() {
218218
t.Skip("Skipping integration test in short mode")
219219
}
@@ -246,7 +246,7 @@ func TestDubbo_GenericCall_POJOArg(t *testing.T) {
246246
// Triple Protocol Tests
247247
// =============================================================================
248248

249-
func TestTriple_GenericCall_StringArg(t *testing.T) {
249+
func TestTripleGenericCallStringArg(t *testing.T) {
250250
if testing.Short() {
251251
t.Skip("Skipping integration test in short mode")
252252
}
@@ -268,7 +268,7 @@ func TestTriple_GenericCall_StringArg(t *testing.T) {
268268
}
269269
}
270270

271-
func TestTriple_GenericCall_MultipleArgs(t *testing.T) {
271+
func TestTripleGenericCallMultipleArgs(t *testing.T) {
272272
if testing.Short() {
273273
t.Skip("Skipping integration test in short mode")
274274
}
@@ -290,7 +290,7 @@ func TestTriple_GenericCall_MultipleArgs(t *testing.T) {
290290
}
291291
}
292292

293-
func TestTriple_GenericCall_IntArg(t *testing.T) {
293+
func TestTripleGenericCallIntArg(t *testing.T) {
294294
if testing.Short() {
295295
t.Skip("Skipping integration test in short mode")
296296
}
@@ -312,7 +312,7 @@ func TestTriple_GenericCall_IntArg(t *testing.T) {
312312
}
313313
}
314314

315-
func TestTriple_GenericCall_NoArgs(t *testing.T) {
315+
func TestTripleGenericCallNoArgs(t *testing.T) {
316316
if testing.Short() {
317317
t.Skip("Skipping integration test in short mode")
318318
}
@@ -334,7 +334,7 @@ func TestTriple_GenericCall_NoArgs(t *testing.T) {
334334
}
335335
}
336336

337-
func TestTriple_GenericCall_ArrayArg(t *testing.T) {
337+
func TestTripleGenericCallArrayArg(t *testing.T) {
338338
if testing.Short() {
339339
t.Skip("Skipping integration test in short mode")
340340
}
@@ -356,7 +356,7 @@ func TestTriple_GenericCall_ArrayArg(t *testing.T) {
356356
}
357357
}
358358

359-
func TestTriple_GenericCall_POJOArg(t *testing.T) {
359+
func TestTripleGenericCallPOJOArg(t *testing.T) {
360360
if testing.Short() {
361361
t.Skip("Skipping integration test in short mode")
362362
}
@@ -385,7 +385,7 @@ func TestTriple_GenericCall_POJOArg(t *testing.T) {
385385
}
386386
}
387387

388-
func TestTriple_GenericCall_POJOArrayArg(t *testing.T) {
388+
func TestTripleGenericCallPOJOArrayArg(t *testing.T) {
389389
if testing.Short() {
390390
t.Skip("Skipping integration test in short mode")
391391
}
@@ -398,7 +398,7 @@ func TestTriple_GenericCall_POJOArrayArg(t *testing.T) {
398398
&pkg.User{ID: "003", Name: "User3", Age: 30, Time: time.Now()},
399399
}
400400

401-
result, err := svc.Invoke(context.Background(), "QueryUsers", []string{"java.util.List"}, []hessian.Object{users})
401+
result, err := svc.Invoke(context.Background(), "QueryUsers", []string{"[Lorg.apache.dubbo.samples.User;"}, []hessian.Object{users})
402402
if err != nil {
403403
t.Fatalf("QueryUsers failed: %v", err)
404404
}
@@ -423,7 +423,7 @@ func TestTriple_GenericCall_POJOArrayArg(t *testing.T) {
423423
// Edge Case Tests (Triple Protocol)
424424
// =============================================================================
425425

426-
func TestTriple_GenericCall_EmptyStringArg(t *testing.T) {
426+
func TestTripleGenericCallEmptyStringArg(t *testing.T) {
427427
if testing.Short() {
428428
t.Skip("Skipping integration test in short mode")
429429
}
@@ -445,7 +445,7 @@ func TestTriple_GenericCall_EmptyStringArg(t *testing.T) {
445445
}
446446
}
447447

448-
func TestTriple_GenericCall_ZeroIntArg(t *testing.T) {
448+
func TestTripleGenericCallZeroIntArg(t *testing.T) {
449449
if testing.Short() {
450450
t.Skip("Skipping integration test in short mode")
451451
}
@@ -467,7 +467,7 @@ func TestTriple_GenericCall_ZeroIntArg(t *testing.T) {
467467
}
468468
}
469469

470-
func TestTriple_GenericCall_EmptyArrayArg(t *testing.T) {
470+
func TestTripleGenericCallEmptyArrayArg(t *testing.T) {
471471
if testing.Short() {
472472
t.Skip("Skipping integration test in short mode")
473473
}
@@ -493,7 +493,7 @@ func TestTriple_GenericCall_EmptyArrayArg(t *testing.T) {
493493
// Error Case Tests
494494
// =============================================================================
495495

496-
func TestTriple_GenericCall_NonExistentMethod(t *testing.T) {
496+
func TestTripleGenericCallNonExistentMethod(t *testing.T) {
497497
if testing.Short() {
498498
t.Skip("Skipping integration test in short mode")
499499
}
@@ -510,7 +510,7 @@ func TestTriple_GenericCall_NonExistentMethod(t *testing.T) {
510510
// Context Tests
511511
// =============================================================================
512512

513-
func TestTriple_GenericCall_WithTimeout(t *testing.T) {
513+
func TestTripleGenericCallWithTimeout(t *testing.T) {
514514
if testing.Short() {
515515
t.Skip("Skipping integration test in short mode")
516516
}
@@ -526,7 +526,7 @@ func TestTriple_GenericCall_WithTimeout(t *testing.T) {
526526
}
527527
}
528528

529-
func TestTriple_GenericCall_CancelledContext(t *testing.T) {
529+
func TestTripleGenericCallCancelledContext(t *testing.T) {
530530
if testing.Short() {
531531
t.Skip("Skipping integration test in short mode")
532532
}
@@ -546,7 +546,7 @@ func TestTriple_GenericCall_CancelledContext(t *testing.T) {
546546
// Benchmark Tests
547547
// =============================================================================
548548

549-
func BenchmarkDubbo_GenericCall(b *testing.B) {
549+
func BenchmarkDubboGenericCall(b *testing.B) {
550550
ins, _ := dubbo.NewInstance(dubbo.WithName("benchmark-dubbo"))
551551
cli, _ := ins.NewClient(
552552
client.WithClientProtocolDubbo(),
@@ -568,7 +568,7 @@ func BenchmarkDubbo_GenericCall(b *testing.B) {
568568
}
569569
}
570570

571-
func BenchmarkTriple_GenericCall(b *testing.B) {
571+
func BenchmarkTripleGenericCall(b *testing.B) {
572572
ins, _ := dubbo.NewInstance(dubbo.WithName("benchmark-triple"))
573573
cli, _ := ins.NewClient(
574574
client.WithClientProtocolTriple(),

generic/go-server/pkg/user_provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package pkg
1919

2020
import (
2121
"context"
22+
"fmt"
2223
"strconv"
2324
"time"
2425
)
@@ -147,6 +148,6 @@ func (u *UserProvider) Invoke(ctx context.Context, methodName string, types []st
147148
return u.QueryAll(ctx)
148149
default:
149150
logger.Errorf("Unknown method: %s", methodName)
150-
return nil, nil
151+
return nil, fmt.Errorf("unknown method: %s", methodName)
151152
}
152153
}

generic/java-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<dependency>
7676
<groupId>org.apache.zookeeper</groupId>
7777
<artifactId>zookeeper</artifactId>
78-
<version>3.4.14</version>
78+
<version>3.8.4</version>
7979
</dependency>
8080
<dependency>
8181
<groupId>junit</groupId>

generic/java-server/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<dependency>
6464
<groupId>org.apache.zookeeper</groupId>
6565
<artifactId>zookeeper</artifactId>
66-
<version>3.4.14</version>
66+
<version>3.8.4</version>
6767
</dependency>
6868
<dependency>
6969
<groupId>junit</groupId>

0 commit comments

Comments
 (0)