Skip to content

Commit f3bceec

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 f3bceec

File tree

12 files changed

+292
-1530
lines changed

12 files changed

+292
-1530
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ 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"
3333

34-
"github.com/apache/dubbo-go-samples/generic/go-client/pkg"
35-
3634
"github.com/dubbogo/gost/log/logger"
3735
)
3836

37+
import (
38+
"github.com/apache/dubbo-go-samples/generic/go-client/pkg"
39+
)
40+
3941
const (
4042
DubboServerURL = "dubbo://127.0.0.1:20000"
4143
TripleServerURL = "tri://127.0.0.1:50052"

generic/go-client/cmd/client_test.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ 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"
34+
)
3435

36+
import (
3537
"github.com/apache/dubbo-go-samples/generic/go-client/pkg"
3638
)
3739

@@ -103,7 +105,7 @@ func setupTripleGenericService(t *testing.T) *generic.GenericService {
103105
// Dubbo Protocol Tests
104106
// =============================================================================
105107

106-
func TestDubbo_GenericCall_StringArg(t *testing.T) {
108+
func TestDubboGenericCallStringArg(t *testing.T) {
107109
if testing.Short() {
108110
t.Skip("Skipping integration test in short mode")
109111
}
@@ -125,7 +127,7 @@ func TestDubbo_GenericCall_StringArg(t *testing.T) {
125127
}
126128
}
127129

128-
func TestDubbo_GenericCall_MultipleArgs(t *testing.T) {
130+
func TestDubboGenericCallMultipleArgs(t *testing.T) {
129131
if testing.Short() {
130132
t.Skip("Skipping integration test in short mode")
131133
}
@@ -147,7 +149,7 @@ func TestDubbo_GenericCall_MultipleArgs(t *testing.T) {
147149
}
148150
}
149151

150-
func TestDubbo_GenericCall_IntArg(t *testing.T) {
152+
func TestDubboGenericCallIntArg(t *testing.T) {
151153
if testing.Short() {
152154
t.Skip("Skipping integration test in short mode")
153155
}
@@ -169,7 +171,7 @@ func TestDubbo_GenericCall_IntArg(t *testing.T) {
169171
}
170172
}
171173

172-
func TestDubbo_GenericCall_NoArgs(t *testing.T) {
174+
func TestDubboGenericCallNoArgs(t *testing.T) {
173175
if testing.Short() {
174176
t.Skip("Skipping integration test in short mode")
175177
}
@@ -191,7 +193,7 @@ func TestDubbo_GenericCall_NoArgs(t *testing.T) {
191193
}
192194
}
193195

194-
func TestDubbo_GenericCall_ArrayArg(t *testing.T) {
196+
func TestDubboGenericCallArrayArg(t *testing.T) {
195197
if testing.Short() {
196198
t.Skip("Skipping integration test in short mode")
197199
}
@@ -213,7 +215,7 @@ func TestDubbo_GenericCall_ArrayArg(t *testing.T) {
213215
}
214216
}
215217

216-
func TestDubbo_GenericCall_POJOArg(t *testing.T) {
218+
func TestDubboGenericCallPOJOArg(t *testing.T) {
217219
if testing.Short() {
218220
t.Skip("Skipping integration test in short mode")
219221
}
@@ -246,7 +248,7 @@ func TestDubbo_GenericCall_POJOArg(t *testing.T) {
246248
// Triple Protocol Tests
247249
// =============================================================================
248250

249-
func TestTriple_GenericCall_StringArg(t *testing.T) {
251+
func TestTripleGenericCallStringArg(t *testing.T) {
250252
if testing.Short() {
251253
t.Skip("Skipping integration test in short mode")
252254
}
@@ -268,7 +270,7 @@ func TestTriple_GenericCall_StringArg(t *testing.T) {
268270
}
269271
}
270272

271-
func TestTriple_GenericCall_MultipleArgs(t *testing.T) {
273+
func TestTripleGenericCallMultipleArgs(t *testing.T) {
272274
if testing.Short() {
273275
t.Skip("Skipping integration test in short mode")
274276
}
@@ -290,7 +292,7 @@ func TestTriple_GenericCall_MultipleArgs(t *testing.T) {
290292
}
291293
}
292294

293-
func TestTriple_GenericCall_IntArg(t *testing.T) {
295+
func TestTripleGenericCallIntArg(t *testing.T) {
294296
if testing.Short() {
295297
t.Skip("Skipping integration test in short mode")
296298
}
@@ -312,7 +314,7 @@ func TestTriple_GenericCall_IntArg(t *testing.T) {
312314
}
313315
}
314316

315-
func TestTriple_GenericCall_NoArgs(t *testing.T) {
317+
func TestTripleGenericCallNoArgs(t *testing.T) {
316318
if testing.Short() {
317319
t.Skip("Skipping integration test in short mode")
318320
}
@@ -334,7 +336,7 @@ func TestTriple_GenericCall_NoArgs(t *testing.T) {
334336
}
335337
}
336338

337-
func TestTriple_GenericCall_ArrayArg(t *testing.T) {
339+
func TestTripleGenericCallArrayArg(t *testing.T) {
338340
if testing.Short() {
339341
t.Skip("Skipping integration test in short mode")
340342
}
@@ -356,7 +358,7 @@ func TestTriple_GenericCall_ArrayArg(t *testing.T) {
356358
}
357359
}
358360

359-
func TestTriple_GenericCall_POJOArg(t *testing.T) {
361+
func TestTripleGenericCallPOJOArg(t *testing.T) {
360362
if testing.Short() {
361363
t.Skip("Skipping integration test in short mode")
362364
}
@@ -385,7 +387,7 @@ func TestTriple_GenericCall_POJOArg(t *testing.T) {
385387
}
386388
}
387389

388-
func TestTriple_GenericCall_POJOArrayArg(t *testing.T) {
390+
func TestTripleGenericCallPOJOArrayArg(t *testing.T) {
389391
if testing.Short() {
390392
t.Skip("Skipping integration test in short mode")
391393
}
@@ -398,7 +400,7 @@ func TestTriple_GenericCall_POJOArrayArg(t *testing.T) {
398400
&pkg.User{ID: "003", Name: "User3", Age: 30, Time: time.Now()},
399401
}
400402

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

426-
func TestTriple_GenericCall_EmptyStringArg(t *testing.T) {
428+
func TestTripleGenericCallEmptyStringArg(t *testing.T) {
427429
if testing.Short() {
428430
t.Skip("Skipping integration test in short mode")
429431
}
@@ -445,7 +447,7 @@ func TestTriple_GenericCall_EmptyStringArg(t *testing.T) {
445447
}
446448
}
447449

448-
func TestTriple_GenericCall_ZeroIntArg(t *testing.T) {
450+
func TestTripleGenericCallZeroIntArg(t *testing.T) {
449451
if testing.Short() {
450452
t.Skip("Skipping integration test in short mode")
451453
}
@@ -467,7 +469,7 @@ func TestTriple_GenericCall_ZeroIntArg(t *testing.T) {
467469
}
468470
}
469471

470-
func TestTriple_GenericCall_EmptyArrayArg(t *testing.T) {
472+
func TestTripleGenericCallEmptyArrayArg(t *testing.T) {
471473
if testing.Short() {
472474
t.Skip("Skipping integration test in short mode")
473475
}
@@ -493,7 +495,7 @@ func TestTriple_GenericCall_EmptyArrayArg(t *testing.T) {
493495
// Error Case Tests
494496
// =============================================================================
495497

496-
func TestTriple_GenericCall_NonExistentMethod(t *testing.T) {
498+
func TestTripleGenericCallNonExistentMethod(t *testing.T) {
497499
if testing.Short() {
498500
t.Skip("Skipping integration test in short mode")
499501
}
@@ -510,7 +512,7 @@ func TestTriple_GenericCall_NonExistentMethod(t *testing.T) {
510512
// Context Tests
511513
// =============================================================================
512514

513-
func TestTriple_GenericCall_WithTimeout(t *testing.T) {
515+
func TestTripleGenericCallWithTimeout(t *testing.T) {
514516
if testing.Short() {
515517
t.Skip("Skipping integration test in short mode")
516518
}
@@ -526,7 +528,7 @@ func TestTriple_GenericCall_WithTimeout(t *testing.T) {
526528
}
527529
}
528530

529-
func TestTriple_GenericCall_CancelledContext(t *testing.T) {
531+
func TestTripleGenericCallCancelledContext(t *testing.T) {
530532
if testing.Short() {
531533
t.Skip("Skipping integration test in short mode")
532534
}
@@ -546,7 +548,7 @@ func TestTriple_GenericCall_CancelledContext(t *testing.T) {
546548
// Benchmark Tests
547549
// =============================================================================
548550

549-
func BenchmarkDubbo_GenericCall(b *testing.B) {
551+
func BenchmarkDubboGenericCall(b *testing.B) {
550552
ins, _ := dubbo.NewInstance(dubbo.WithName("benchmark-dubbo"))
551553
cli, _ := ins.NewClient(
552554
client.WithClientProtocolDubbo(),
@@ -568,7 +570,7 @@ func BenchmarkDubbo_GenericCall(b *testing.B) {
568570
}
569571
}
570572

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

generic/go-server/cmd/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ import (
2727

2828
hessian "github.com/apache/dubbo-go-hessian2"
2929

30-
"github.com/apache/dubbo-go-samples/generic/go-server/pkg"
31-
3230
"github.com/dubbogo/gost/log/logger"
3331
)
3432

33+
import (
34+
"github.com/apache/dubbo-go-samples/generic/go-server/pkg"
35+
)
36+
3537
func main() {
3638
hessian.RegisterPOJO(&pkg.User{})
3739

0 commit comments

Comments
 (0)