电子说
Dubbo Rust 近期发布了首个正式版本,Dubbo Rust 目标是对齐 Dubbo 3 的所有核心功能设计,包括基于 HTTP/2 的高性能通信、用户友好的微服务开发编程模式、通过接入Dubbo Mesh提供丰富的服务治理能力等。相比于其他语言实现,Dubbo Rust 将很好的利用 Rust 语言极致性能、安全和指令级掌控能力的特点。
1►
前置条件
安装 Rust 开发环境
安装 protoc 工具
2►
使用 IDL 定义 Dubbo 服务
Greeter 服务定义如下,包含一个 Unary(Request - Response)模型的 Dubbo 服务。
// ./proto/greeter.proto syntax = "proto3"; option java_multiple_files = true; package org.apache.dubbo.sample.tri; // The request message containing the user's name. message GreeterRequest{ string name = 1; } // The response message containing the greetings message GreeterReply { string message = 1; } service Greeter{ // unary rpc greet(GreeterRequest) returns (GreeterReply); }
3►
添加 Dubbo-Rust 及相关依赖到项目
# ./Cargo.toml [package] name = "example-greeter" version = "0.1.0" edition = "2021" [[bin]] name = "greeter-server" path = "src/greeter/server.rs" [[bin]] name = "greeter-client" path = "src/greeter/client.rs" [dependencies] http = "0.2" http-body = "0.4.4" futures-util = {version = "0.3", default-features = false} tokio = { version = "1.0", features = [ "rt-multi-thread", "time", "fs", "macros", "net", "signal"] } prost-derive = {version = "0.10", optional = true} prost = "0.10.4" async-trait = "0.1.56" tokio-stream = "0.1" dubbo = "0.2.0" dubbo-config = "0.2.0" [build-dependencies] dubbo-build = "0.2.0"
4►
配置 Dubbo - Build 编译 IDL
在项目根目录创建(注意不是 src 目录),创建 build.rs 文件并添加以下内容:
// ./build.rs fn main() { dubbo_build::configure() .compile(&["proto/greeter.proto"], &["proto/"]) .unwrap(); }
这样配置之后,编译项目就可以生成 Dubbo Stub 相关代码。
路径一般在:
./target/debug/build/example-greeter/out/org.apache.dubbo.sample.tri.rs
5►
编写 Dubbo 业务代码
5.1
编写 Dubbo Server
// ./src/greeter/server.rs use ... #[tokio::main] async fn main() { register_server(GreeterServerImpl { name: "greeter".to_string(), }); // Dubbo::new().start().await; Dubbo::new() .with_config({ let r = RootConfig::new(); match r.load() { Ok(config) => config, Err(_err) => panic!("err: {:?}", _err), // response was droped } }) .start() .await; } #[allow(dead_code)] #[derive(Default, Clone)] struct GreeterServerImpl { name: String, } // #[async_trait] #[async_trait] impl Greeter for GreeterServerImpl { async fn greet( &self, request: Request, ) -> Result , dubbo::Status> { println!("GreeterServer::greet {:?}", request.metadata); Ok(Response::new(GreeterReply { message: "hello, dubbo-rust".to_string(), })) } }
5.2
配置 dubbo.yaml
dubbo.yaml 指示 Server 端的配置,包括暴露的服务列表、协议配置、监听配置等。
# ./dubbo.yaml name: dubbo service: org.apache.dubbo.sample.tri.Greeter: version: 1.0.0 group: test protocol: triple registry: '' serializer: json protocol_configs: triple: ip: 0.0.0.0 port: '8888' name: triple protocols: triple: ip: 0.0.0.0 port: '8888' name: triple
5.3
编写 Dubbo Client
// ./src/greeter/client.rs use ... #[tokio::main] async fn main() { let mut cli = GreeterClient::new().with_uri("http://127.0.0.1:8888".to_string()); println!("# unary call"); let resp = cli .greet(Request::new(GreeterRequest { name: "message from client".to_string(), })) .await; let resp = match resp { Ok(resp) => resp, Err(err) => return println!("{:?}", err), }; let (_parts, body) = resp.into_parts(); println!("Response: {:?}", body); }
6►
运行并总结
6.1
编译
执行 cargo build 来编译 Server 和 Client。
6.2
运行 Server
执行 ./target/debug/greeter-server 来运行 Server,如上文 dubbo.yaml 所配置,Server 会监听 8888 端口,并以 triple 协议提供 RPC 服务。
$ ./target/debug/greeter-server 2022-09-28T2328.104577Z INFO dubbo: url: Some(Url { uri: "triple://0.0.0.0:8888/org.apache.dubbo.sample.tri.Greeter", protocol: "triple", location: "0.0.0.0:8888", ip: "0.0.0.0", port: "8888", service_key: ["org.apache.dubbo.sample.tri.Greeter"], params: {} })
6.3
运行 Client,验证调用是否成功
执行 ./target/debug/greeter-client 来运行 Client,调用triple://127.0.0.1:8888/org.apache.dubbo.sample.tri.Greeter 下的各种方法。
$ ./target/debug/greeter-client Response: GreeterReply { message: "hello, dubbo-rust" }
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !