Rust和Java互相调用

使用 Rust 调用 Java 开发的 Dubbo 服务。

1 前置条件

  • 安装 Rust 开发环境
  • 安装 protoc 工具
  • 安装 Java 开发环境

2 运行示例 Java 版本的 Dubbo provider

Java 版本的 Dubbo provider 示例源码见https://github.com/apache/dubbo-samples/tree/master/3-extensions/protocol/dubbo-samples-triple

Clone 源代码、编译构建,并运行 provider:

  1. $ # clone 源代码
  2. $ git clone https://github.com/apache/dubbo-samples.git
  3. $ cd dubbo-samples/dubbo-samples-triple/
  4. $ # 构建
  5. $ mvn clean compile package -DskipTests
  6. $ # 运行 provider
  7. $ java -Dprovider.port=8888 -jar ./target/dubbo-samples-triple-1.0-SNAPSHOT.jar
  8. # ……省略部分日志
  9. Dubbo triple stub server started, port=8888

Java 侧的接口定义

3 运行 Rust 版本的 Dubbo consumer

Rust 版本的 Dubbo consumer 示例源码见https://github.com/apache/dubbo-rust/tree/main/examples/greeter

Clone 源代码、编译构建,并运行 consumer:

  1. $ # clone 源代码
  2. $ git clone https://github.com/apache/dubbo-rust.git
  3. $ cd dubbo-rust/examples/greeter/
  4. $ # 构建
  5. $ cargo build
  6. $ # 运行 consumer,调用provider
  7. $ ../../target/debug/greeter-client
  8. # unary call
  9. Response: GreeterReply { message: "hello, dubbo-rust" }
  10. # client stream
  11. client streaming, Response: GreeterReply { message: "hello client streaming" }
  12. # bi stream
  13. parts: Metadata { inner: {"content-type": "application/grpc", "date": "Wed, 28 Sep 2022 23:54:56 GMT"} }
  14. reply: GreeterReply { message: "server reply: \"msg1 from client\"" }
  15. reply: GreeterReply { message: "server reply: \"msg2 from client\"" }
  16. reply: GreeterReply { message: "server reply: \"msg3 from client\"" }
  17. trailer: Some(Metadata { inner: {"grpc-message": "poll trailer successfully.", "grpc-accept-encoding": "gzip,identity", "content-type": "application/grpc", "grpc-status": "0"} })
  18. # server stream
  19. parts: Metadata { inner: {"content-type": "application/grpc", "date": "Wed, 28 Sep 2022 23:54:56 GMT"} }
  20. reply: GreeterReply { message: "msg1 from server" }
  21. reply: GreeterReply { message: "msg2 from server" }
  22. reply: GreeterReply { message: "msg3 from server" }
  23. trailer: Some(Metadata { inner: {"content-type": "application/grpc", "grpc-message": "poll trailer successfully.", "grpc-accept-encoding": "gzip,identity", "grpc-status": "0"} })

Rust侧的接口定义

最后修改 December 16, 2022: Fix check (#1736) (97972c1)