Dubbo-go 示例
为了方便 Dubbogo 框架用户的使用,我们提供了 Samples 仓库供用户参考
1. Samples 仓库中包含的示例
- config-api:使用 API 进行配置初始化
- configcenter:使用不同的配置中心,目前支持三种类型:zookeeper、apollo 和 nacos
- context:如何使用上下文传递附件
- direct:直连模式
- game:游戏服务示例
- generic:泛化调用
- rpc:RPC 调用示例,包括 Triple、Dubbo 和其他协议以及跨语言/gRPC 互操作性示例
- helloworld:RPC 调用入门示例
- logger:日志示例
- registry:展示与不同注册中心的连接,包括 zk、nacos、etcd
- metrics:数据报告
- filter:使用提供的过滤器和自定义过滤器的示例
- registry/servicediscovery:应用级服务发现示例
- router:路由示例
- tracing:链路追踪示例
2. 如何运行
目前有三种方式可以运行 dubbo-go 示例
- 通过 bash 命令快速启动:通过简单的命令行启动示例和单元测试
- 在 IDE 中快速启动,这也是推荐的方式:在项目“.run”子目录下,提供了所有示例 GoLand 运行配置文件,因此用户只需在 IDE 中点击即可运行所有示例。
- 在 IDE 中手动配置和运行:为了完整性,以及如果您不使用 GoLand 而使用其他 IDE,这里也提供了分步配置指南,以帮助用户了解如何在 IDE 中进行配置,以运行或调试 dubbo-go 示例。
2.1 通过命令行快速启动
前提条件:需要准备好 docker 环境
下面我们将以“helloworld”为例
启动注册中心(例如 zookeeper)
make -f build/Makefile docker-up
当您看到类似于以下内容的输出时,表示 zookeeper 服务器已准备好启动。
> Starting dependency services with ./integrate_test/dockercompose/docker-compose.yml Docker Compose is now in the Docker CLI, try `docker compose up` Creating network "dockercompose_default" with the default driver Creating dockercompose_zookeeper_1... done Creating etcd... done Creating nacos-standalone... done
如果要停止注册中心,可以通过运行以下命令来完成
make -f build/Makefile docker-down
启动服务提供者
cd helloworld/go-server/cmd export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml" go run .
当您看到类似于以下内容的输出时,表示服务提供者已准备好启动。
2021/10/27 00:33:10 Connected to 127.0.0.1:2181 2021/10/27 00:33:10 Authenticated: id=72057926938066944, timeout=10000 2021/10/27 00:33:10 Re-submitting `0` credentials after reconnect
运行服务调用方
cd helloworld/go-client/cmd export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml" go run .
当输出以下信息时,表示
go-client
成功调用了go-server
。2021-10-27T00:40:44.879+0800 DEBUG triple/dubbo3_client.go:106 TripleClient.Invoke: get reply = name:"Hello laurence" id:"12345" age:21 2021-10-27T00:40:44.879+0800 DEBUG proxy/proxy.go:218 [makeDubboCallProxy] result: name:"Hello laurence" id:"12345" age:21 , err: <nil> 2021-10-27T00:40:44.879+0800 INFO cmd/client.go:51 client response result: name:"Hello laurence" id:"12345" age:21
集成测试 除了展示如何在 dubbo-go 中使用功能和特性外,这个 dubbo-go-samples 项目还用于 apache/dubbo-go 的集成测试。为
go-server
设计的集成测试可以按如下方式运行先启动服务器
cd helloworld/go-server/cmd export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml" go run .
然后切换到单个测试目录,设置环境变量,然后执行单个测试
cd integrate_test/helloworld/tests/integration export DUBBO_GO_CONFIG_PATH="../../../../helloworld/go-client/conf/dubbogo.yml" go test -v
当输出以下信息时,表示集成测试已通过。
> Running integration test for application go-server ... --- PASS: TestSayHello (0.01s) PASS ok github.com/apache/dubbo-go-samples/integrate_test/helloworld/tests/integration0.119s
关闭和清理
make -f build/Makefile clean docker-down
以下两种操作模式与 IDE 相关。这里我们以 Intellij GoLand 为例进行讨论。
2.2 在 IDE 中快速启动
在 GoLand 中打开此项目后,您会发现在“运行配置”弹出菜单中,已经有一系列用于运行相关服务提供者和调用者的预配置选项,例如:“helloworld-go-server”和“helloworld-go-client”。
您可以选择任何这些与快速入门相关的示例。当然,在运行之前,假设所需的注册中心已经提前启动,否则用例将失败。您可以选择手动启动它,或者使用项目中提供的“docker-compose.yml”来启动注册中心的 docker 实例。
2.3. 在 IDE 中手动运行
这里以 *Intellij GoLand* 为例。在 GoLand 中打开 dubbo-go-samples 项目后,请按照以下步骤运行/调试此示例
启动 zookeeper 服务器
打开“integrate_test/dockercompose/docker-compose.yml”文件,然后单击编辑器左侧列中的 ▶︎▶︎ 图标运行,“服务”选项卡应弹出并输出类似于以下内容的文本消息
Deploying 'Compose: docker'... /usr/local/bin/docker-compose -f ...integrate_test/dockercompose/docker-compose.yml up -d Creating network "docker_default" with the default driver Creating docker_zookeeper_1... 'Compose: docker' has been deployed successfully.
启动服务提供者
打开“helloworld/go-server/cmd/server.go”文件,然后单击左侧列中“main”函数旁边的 ▶︎ 图标,然后从弹出菜单中选择“修改运行配置...”,并确保以下配置准确无误
- 工作目录:“helloworld/go-server”目录的绝对路径,例如:*/home/dubbo-go-samples/helloworld/go-server*
- 环境:DUBBO_GO_CONFIG_PATH=”../conf/dubbogo.yml”
这样,示例中的服务器就准备就绪,可以运行了。
运行服务消费者
打开“helloworld/go-client/cmd/client.go”文件,然后从左侧列中单击“main”函数旁边的 ▶︎ 图标,然后选择“修改运行配置...”,并确保以下配置准确无误
- 工作目录:“helloworld/go-client”目录的绝对路径,例如:*/home/dubbo-go-samples/helloworld/go-client*
- 环境:DUBBO_GO_CONFIG_PATH=”../conf/dubbogo.yml”
然后您可以运行并调用远程服务。如果调用成功,将有以下输出
[2021-02-03/16:19:30 main.main: client.go: 66] response result: &{A001 Alex Stocks 18 2020-02-04 16:19:30.422 +0800 CST}
如果您需要调试此示例或 dubbo-go 框架,可以在 IDE 中从“运行”切换到“调试”。如果要结束,只需直接单击 ◼︎。