使用Unix套接字连接器通信

介绍使用 Dubbo Rust Triple 协议使用 Unix 套接字连接器实现通信。

本文重点讲解 Dubbo Rust Triple 协议使用Unix 套接,请先查看 Quick Start 了解 Dubbo Rust 基本使用,在此查看本文的完整示例

1 使用 Unix 套接字连接器 说明

[cfg(any(target_os = “macos”, target_os=“unix”))] 当操作系统符合cfg配置时,unix 模块会被编译使用,否则无法使用

2 使用 client/connection 使用 Unix 套接字连接器编写逻辑

2.1 编写 Client 端

  1. // examples/echo/src/echo/client.rs
  2. // 使用 ClientBuilder 初始化 Client
  3. let builder = ClientBuilder::new().with_connector("unix").with_host("unix://127.0.0.1:8888");
  4. let mut cli = EchoClient::build(builder);

2.2 编写 Server 端

  1. // examples/echo/src/echo/server.rs
  2. // 通过 serverbuilder 来初始化 Server
  3. let builder = ServerBuilder::new()
  4. .with_listener("unix".to_string())
  5. .with_service_names(vec!["grpc.examples.echo.Echo".to_string()])
  6. .with_addr("127.0.0.1:8888");
  7. builder.build().serve().await.unwrap();

3 运行示例

  1. 编译

执行cargo build来编译server和client。

  1. 运行server

执行cargo run --bin echo-server来运行server,如上文dubbo.yaml所配置,server会监听8888端口,并以triple协议提供RPC服务:

  1. $ cargo run --bin echo-server
  2. 2023-01-19T08:36:25.663138Z INFO dubbo::triple::server::builder: server starting. addr: Some(127.0.0.1:8888)
  1. 运行client,可以看到 Unix 通信效果

执行cargo run --bin echo-client来运行client,调用triple://127.0.0.1:8888/org.apache.dubbo.sample.tri.Greeter下的各种方法:

  1. $ cargo run --bin echo-client
  2. 2023-01-19T08:38:52.516792Z INFO dubbo::triple::transport::connector::unix_connector: host is ip address: "127.0.0.1"
  3. Response: EchoResponse { message: "hello, dubbo-rust" }
  4. 2023-01-19T08:38:52.526697Z INFO dubbo::triple::transport::connector::unix_connector: host is ip address: "127.0.0.1"
  5. client streaming, Response: EchoResponse { message: "hello client streaming" }
  6. 2023-01-19T08:38:52.539439Z INFO dubbo::triple::transport::connector::unix_connector: host is ip address: "127.0.0.1"
  7. parts: Metadata { inner: {"content-type": "application/grpc", "date": "Thu, 19 Jan 2023 08:38:52 GMT"} }
  8. reply: EchoResponse { message: "server reply: \"msg1 from client\"" }
  9. reply: EchoResponse { message: "server reply: \"msg2 from client\"" }
  10. reply: EchoResponse { message: "server reply: \"msg3 from client\"" }
  11. trailer: Some(Metadata { inner: {"grpc-accept-encoding": "gzip,identity", "grpc-status": "0", "grpc-message": "poll trailer successfully.", "content-type": "application/grpc"} })
  12. 2023-01-19T08:38:52.645035Z INFO dubbo::triple::transport::connector::unix_connector: host is ip address: "127.0.0.1"
  13. parts: Metadata { inner: {"content-type": "application/grpc", "date": "Thu, 19 Jan 2023 08:38:52 GMT"} }
  14. reply: EchoResponse { message: "msg1 from server" }
  15. reply: EchoResponse { message: "msg2 from server" }
  16. reply: EchoResponse { message: "msg3 from server" }
  17. trailer: Some(Metadata { inner: {"grpc-status": "0", "content-type": "application/grpc", "grpc-message": "poll trailer successfully.", "grpc-accept-encoding": "gzip,identity"} })

最后修改 March 2, 2023: Fix alias forward in docs3-v2 (#2342) (f16c1535592)