Dubbo Rust 近期發(fā)布了首個(gè)正式版本,Dubbo Rust 目標(biāo)是對(duì)齊 Dubbo 3 的所有核心功能設(shè)計(jì),包括基于 HTTP/2 的高性能通信、用戶(hù)友好的微服務(wù)開(kāi)發(fā)編程模式、通過(guò)接入Dubbo Mesh提供豐富的服務(wù)治理能力等。相比于其他語(yǔ)言實(shí)現(xiàn),Dubbo Rust 將很好的利用 Rust 語(yǔ)言極致性能、安全和指令級(jí)掌控能力的特點(diǎn)。
1?
前置條件
安裝 Rust 開(kāi)發(fā)環(huán)境
安裝 protoc 工具
2?
使用 IDL 定義 Dubbo 服務(wù)
Greeter 服務(wù)定義如下,包含一個(gè) Unary(Request - Response)模型的 Dubbo 服務(wù)。
// ./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 及相關(guān)依賴(lài)到項(xiàng)目
# ./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
在項(xiàng)目根目錄創(chuàng)建(注意不是 src 目錄),創(chuàng)建 build.rs 文件并添加以下內(nèi)容:
// ./build.rs fn main() { dubbo_build::configure() .compile(&["proto/greeter.proto"], &["proto/"]) .unwrap(); }
這樣配置之后,編譯項(xiàng)目就可以生成 Dubbo Stub 相關(guān)代碼。
路徑一般在:
./target/debug/build/example-greeter/out/org.apache.dubbo.sample.tri.rs
5?
編寫(xiě) Dubbo 業(yè)務(wù)代碼
5.1
編寫(xiě) 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 端的配置,包括暴露的服務(wù)列表、協(xié)議配置、監(jiān)聽(tīng)配置等。
# ./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
編寫(xiě) 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?
運(yùn)行并總結(jié)
6.1
編譯
執(zhí)行 cargo build 來(lái)編譯 Server 和 Client。
6.2
運(yùn)行 Server
執(zhí)行 ./target/debug/greeter-server 來(lái)運(yùn)行 Server,如上文 dubbo.yaml 所配置,Server 會(huì)監(jiān)聽(tīng) 8888 端口,并以 triple 協(xié)議提供 RPC 服務(wù)。
$ ./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
運(yùn)行 Client,驗(yàn)證調(diào)用是否成功
執(zhí)行 ./target/debug/greeter-client 來(lái)運(yùn)行 Client,調(diào)用triple://127.0.0.1:8888/org.apache.dubbo.sample.tri.Greeter 下的各種方法。
$ ./target/debug/greeter-client Response: GreeterReply { message: "hello, dubbo-rust" }
審核編輯:劉清
-
HTTP
+關(guān)注
關(guān)注
0文章
504瀏覽量
31194 -
IDL
+關(guān)注
關(guān)注
0文章
19瀏覽量
7643 -
rust語(yǔ)言
+關(guān)注
關(guān)注
0文章
57瀏覽量
3009
原文標(biāo)題:用 Rust 語(yǔ)言開(kāi)發(fā)微服務(wù):Dubbo Rust 首個(gè)正式版本初體驗(yàn)
文章出處:【微信號(hào):Rust語(yǔ)言中文社區(qū),微信公眾號(hào):Rust語(yǔ)言中文社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論