Skip to content

feat(router): add router examples#1032

Merged
Alanxtl merged 18 commits intoapache:mainfrom
yangpixi:add-tag-router-examples
Feb 25, 2026
Merged

feat(router): add router examples#1032
Alanxtl merged 18 commits intoapache:mainfrom
yangpixi:add-tag-router-examples

Conversation

@yangpixi
Copy link
Contributor

@yangpixi yangpixi commented Feb 13, 2026

About

apache/dubbo-go#3208

This PR adds tag router and condition router examples. It uses Triple protocol to demonstrate router function.

detail

  • add a common client to call greet function.
  • add two server with one with tag and another without.
  • add this example's description in README.md file.

Copy link
Contributor

@Alanxtl Alanxtl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. pls provied README_CN
  2. 这个新的示例可以作为一个集成测试吗

@yangpixi
Copy link
Contributor Author

  1. pls provied README_CN
  2. 这个新的示例可以作为一个集成测试吗

好的,我看看

@yangpixi yangpixi changed the title feat(router): add tag router examples feat(router): add router examples Feb 15, 2026
@Alanxtl
Copy link
Contributor

Alanxtl commented Feb 16, 2026

集成测试的逻辑我完全重写了一遍,请参考 https://github.com/apache/dubbo-go-samples/blob/main/HOWTO_CN.md#13-%E5%A6%82%E4%BD%95%E6%96%B0%E5%A2%9E%E4%B8%80%E4%B8%AA%E9%9B%86%E6%88%90%E6%B5%8B%E8%AF%95-sample 看下是否能加到集成测试里面

@yangpixi
Copy link
Contributor Author

集成测试的逻辑我完全重写了一遍,请参考 https://github.com/apache/dubbo-go-samples/blob/main/HOWTO_CN.md#13-%E5%A6%82%E4%BD%95%E6%96%B0%E5%A2%9E%E4%B8%80%E4%B8%AA%E9%9B%86%E6%88%90%E6%B5%8B%E8%AF%95-sample 看下是否能加到集成测试里面

Alanxtl and others added 5 commits February 17, 2026 00:24
* complete update logic

* import format

* add docker compose

* docker-health-check

* warp

* fix all samples

* import format

* delete use less files

* Update integrate_test.sh

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Comment on lines 74 to 78
for {
time.Sleep(5 * time.Second) // sleep 5 seconds
rep, err := srv.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
printRes(rep, err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

别弄成死循环 死循环的话集成测试怎么退出啊
另一个示例的死循环也改一下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果不使用死循环,有什么更佳的方法吗?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 1. 准备信号监听
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

// 2. 初始化 Ticker
ticker := time.NewTicker(5 * time.Second)

// 【关键】使用 defer 确保函数退出时,计时器资源被释放
// 无论是由于 return 还是 panic,这行都会执行
defer ticker.Stop() 

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

fmt.Println("服务运行中,按 Ctrl+C 安全退出...")

// 3. 主循环
done := false
for !done {
    select {
    case <-sigs:
        fmt.Println("\n[信号] 接收到退出请求,清理资源中...")
        // 停止向通道发送心跳,虽然 defer 也会做,但这里显式停止是好习惯
        ticker.Stop() 
        done = true // 跳出循环

    case <-ticker.C:
        // 正常执行业务逻辑
        rep, err := srv.Greet(ctx, &greet.GreetRequest{Name: "hello world"})
        printRes(rep, err)
    }
}

问下 gpt 或者 doubao,上面这种优雅退出代码很多

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okok

array+=("java_interop/service_discovery/service")

# router
array+=("router/tag")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外两个也加进来

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦哦 另外两个需要在配置中心手动操作是吧
那不用加到集成测试里面了
如果这样的话 死循环确实也没啥问题

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对的,我是这样想的


```shell
$ go run ./go-server/cmd/server.go # 20000端口
$ go run ./go-copy-server/cmd/server_copy.go # 20001端口
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go-copy-server这个名字总感觉怪怪的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那我今天再改改

Copy link
Contributor

@Alanxtl Alanxtl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@AlexStocks AlexStocks requested a review from Copilot February 20, 2026 07:14

}

func printRes(rep *greet.GreetResponse, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种函数封装毫无意义,去掉吧

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds new Dubbo-go router sample projects (tag / condition / script) using the Triple protocol, and wires the tag router sample into the repository’s integration test run list. It also updates the top-level README(s) and router-specific README(s) to document the new samples.

Changes:

  • Add router/tag sample (proto + Go client + two Go providers: tagged and non-tagged).
  • Add router/condition and router/script samples (two providers + client + Nacos config instructions).
  • Update sample indexes/READMEs and add router/tag to start_integrate_test.sh.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 19 comments.

Show a summary per file
File Description
start_integrate_test.sh Adds router/tag to the integration test sample list.
router/tag/proto/greet.triple.go Generated Triple stubs for the tag-router example.
router/tag/proto/greet.proto Proto contract for the tag-router greet service.
router/tag/proto/greet.pb.go Generated protobuf Go types for the tag-router example.
router/tag/go-tag-server/cmd/server_tag.go Tagged provider implementation for tag routing demo.
router/tag/go-server/cmd/server.go Non-tagged provider implementation for tag routing demo.
router/tag/go-client/cmd/client.go Consumer that invokes with tag/force attachments for routing.
router/tag/README.md English runbook for the tag-router sample.
router/tag/README_CN.md Chinese runbook for the tag-router sample.
router/script/go-server/cmd/server.go Script-router provider (node 1).
router/script/go-node2-server/cmd/server_node2.go Script-router provider (node 2).
router/script/go-client/cmd/client.go Script-router client that loops and observes routing behavior.
router/script/README.md English runbook + Nacos YAML example for script router.
router/script/README_CN.md Chinese runbook + Nacos YAML example for script router.
router/condition/go-server/cmd/server.go Condition-router provider (node 1).
router/condition/go-node2-server/cmd/server_node2.go Condition-router provider (node 2).
router/condition/go-client/cmd/client.go Condition-router client that loops and observes routing behavior.
router/condition/README.md English runbook + Nacos YAML example for condition router.
router/condition/README_CN.md Chinese runbook + Nacos YAML example for condition router.
router/README.md Router samples landing page (English).
router/README_CN.md Router samples landing page (Chinese).
README.md Adds router section to the repository sample index.
README_CN.md Adds router section to the repository sample index (Chinese).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

router/README.md Outdated

## Description

This directory includes four types of dubbo-go router function. No newline at end of file
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This README says there are "four types" of router examples, but the directory currently contains tag/condition/script (3). Please update the count or rephrase (e.g., "multiple types") to match the actual contents.

Suggested change
This directory includes four types of dubbo-go router function.
This directory includes multiple types of dubbo-go router function.

Copilot uses AI. Check for mistakes.
Comment on lines 79 to 83
// temporarily cancel checking for result, for PR # 3208 (https://github.com/apache/dubbo-go/pull/3208)
// hasn't been merged to main branch yet, thus tag router still not works properly.

//checkRes(exp, resp.GetGreeting(), err)
printRes(resp, err)
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkRes is commented out with a note about an upstream PR, so the example no longer verifies the routing behavior and the exp argument is effectively dead. Consider either restoring the assertion (once the dependency is available) or removing the exp parameter / checkRes helper to keep the sample deterministic and easier to maintain.

Copilot uses AI. Check for mistakes.
README.md Outdated
* `retry`: Demonstrates retry mechanisms in Dubbo-go RPC calls.
* `router`: Various Dubbo-go router examples.
* `router/tag`: Dubbo-go tag router examples.
* `router/condition`: Dubbo-to condition router examples.
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The router list has a typo: "Dubbo-to" should be "Dubbo-go".

Suggested change
* `router/condition`: Dubbo-to condition router examples.
* `router/condition`: Dubbo-go condition router examples.

Copilot uses AI. Check for mistakes.
* `retry`:Dubbo-go RPC 调用重试机制示例。
* `router`: Dubbo-go 各种 Router 调用示例。
* `router/tag`: Dubbo-go 的 tag router 使用示例。
* `router/condition`: Dubbo-go 的 condition router 使用示例。
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Chinese sample index under router lists tag/condition, but this PR also adds a router/script example directory. Consider adding a router/script entry here to keep the index complete.

Suggested change
* `router/condition`: Dubbo-go 的 condition router 使用示例。
* `router/condition`: Dubbo-go 的 condition router 使用示例。
* `router/script`: Dubbo-go 的 script router 使用示例。

Copilot uses AI. Check for mistakes.
Comment on lines 18 to 19
$ go run ./go-server/cmd/server.go # 无标签server
$ go run ./go-tag-server/cmdserver_tag.go # 有标签server
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command path for the tagged server looks incorrect (./go-tag-server/cmdserver_tag.go). It should point to the actual file under go-tag-server/cmd/ so users can run the example as documented.

Suggested change
$ go run ./go-server/cmd/server.go # 无标签server
$ go run ./go-tag-server/cmdserver_tag.go # 有标签server
$ go run ./go-server/cmd/server.go # 无标签server
$ go run ./go-tag-server/cmd/server_tag.go # 有标签server

Copilot uses AI. Check for mistakes.
if err := srv.Serve(); err != nil {
logger.Errorf("server serve failed: %v", err)
if strings.Contains(err.Error(), "client not connected") {
logger.Errorf("hint: Nacos client not connected (gRPC). Check %s is reachable and gRPC port %d is open (Nacos 2.x default).", RegistryAddress, TriPort)
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hint message uses TriPort (the service port) as the "Nacos gRPC port". Nacos 2.x gRPC is typically on 9848 (when the HTTP port is 8848), so this hint is misleading. Consider using a dedicated nacosGrpcPort value instead of TriPort.

Copilot uses AI. Check for mistakes.
if err := srv.Serve(); err != nil {
logger.Errorf("server serve failed: %v", err)
if strings.Contains(err.Error(), "client not connected") {
logger.Errorf("hint: Nacos client not connected (gRPC). Check %s is reachable and gRPC port %d is open (Nacos 2.x default).", RegistryAddress, TriPort)
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hint message uses TriPort (the service port) as the "Nacos gRPC port". Nacos 2.x gRPC is typically on 9848 (when the HTTP port is 8848), so this hint is misleading. Consider using a dedicated nacosGrpcPort value instead of TriPort.

Copilot uses AI. Check for mistakes.
if err := srv.Serve(); err != nil {
logger.Errorf("server serve failed: %v", err)
if strings.Contains(err.Error(), "client not connected") {
logger.Errorf("hint: Nacos client not connected (gRPC). Check %s is reachable and gRPC port %d is open (Nacos 2.x default).", RegistryAddress, TriPort)
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hint message uses TriPort (the service port) as the "Nacos gRPC port". Nacos 2.x gRPC is typically on 9848 (when the HTTP port is 8848), so this hint is misleading. Consider using a dedicated nacosGrpcPort value instead of TriPort.

Copilot uses AI. Check for mistakes.
if err := srv.Serve(); err != nil {
logger.Errorf("server serve failed: %v", err)
if strings.Contains(err.Error(), "client not connected") {
logger.Errorf("hint: Nacos client not connected (gRPC). Check %s is reachable and gRPC port %d is open (Nacos 2.x default).", RegistryAddress, TriPort)
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hint message uses TriPort (the service port) as the "Nacos gRPC port". Nacos 2.x gRPC is typically on 9848 (when the HTTP port is 8848), so this hint is misleading. Consider using a dedicated nacosGrpcPort value instead of TriPort.

Copilot uses AI. Check for mistakes.
registry.WithNacos(),
registry.WithAddress(RegistryAddress),
),
dubbo.WithConfigCenter( // configure config center to enable condition router
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says this config-center setup is to enable the "condition router", but this example is for the script router. Please adjust the comment to avoid misleading readers.

Suggested change
dubbo.WithConfigCenter( // configure config center to enable condition router
dubbo.WithConfigCenter( // configure config center for script router

Copilot uses AI. Check for mistakes.
@yangpixi yangpixi requested a review from AlexStocks February 23, 2026 14:15
@Alanxtl Alanxtl merged commit fc02ccc into apache:main Feb 25, 2026
3 checks passed
@yangpixi yangpixi deleted the add-tag-router-examples branch February 25, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants