{"id":8892,"date":"2024-05-23T08:01:01","date_gmt":"2024-05-23T00:01:01","guid":{"rendered":""},"modified":"2024-05-23T08:01:01","modified_gmt":"2024-05-23T00:01:01","slug":"rust torch_Rust\u83dc\u9e1f\u6559\u7a0b\u300c\u5efa\u8bae\u6536\u85cf\u300d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8892.html","title":{"rendered":"rust torch_Rust\u83dc\u9e1f\u6559\u7a0b\u300c\u5efa\u8bae\u6536\u85cf\u300d"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

Rust__\u4f7f\u7528tonic\u5b9e\u73b0\u4e00\u5143\u53ca\u6d41\u5f0f\uff08Unary and Streaming\uff09RPC\u7684\u5b9e\u4f8b<\/strong><\/p>\n

\u2003\u2003\u672c\u6587\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7tonic\uff08gRPC\u7684rust\u5b9e\u73b0\uff09\u5b9e\u73b0\u4e00\u5143RPC\u548c\u6d41\u5f0fRPC\u3002\u5b9e\u4f8b\u4e3a\u901a\u8fc7Unary RPC\u5b9e\u73b0\u5bf9\u670d\u52a1\u5668\u4e2d\u54c8\u5e0c\u8868\u7684\u5355\u4e2a\u8bfb\u5199\uff0c\u4ee5\u53ca\u901a\u8fc7Streaming RPC\u8fdb\u884c\u6279\u91cf\u8bfb\u5199\u3002\u6d41\u5f0fRPC\u53ef\u4ee5\u4f7f\u5f97\u5ba2\u6237\u7aef\u4e00\u8fb9\u53d1\u9001\uff0c\u670d\u52a1\u5668\u4e00\u8fb9\u5904\u7406\uff0c\u4e0d\u9700\u8981\u7b49\u5230\u5ba2\u6237\u7aef\u5168\u90e8\u53d1\u9001\u5b8c\u518d\u5904\u7406\uff0c\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5927\u7684\u6279\u91cf\u5904\u7406\u60c5\u51b5\u3002
\u5b8c\u6574\u4ee3\u7801Github<\/p>\n

1.\u5b9a\u4e49\u534f\u8bae<\/h2>\n
\/\/myproto.proto<\/span>\nsyntax =<\/span> \"proto3\"<\/span>;<\/span>\npackage myproto;<\/span>\n\nmessage KvPair { \n   <\/span>\n    Key key =<\/span> 1<\/span>;<\/span>\n    Value val =<\/span> 2<\/span>;<\/span>\n}<\/span>\n\nmessage Key { \n   <\/span>\n    string key =<\/span> 1<\/span>;<\/span>\n}<\/span>\n\nmessage Value { \n   <\/span>\n    string val =<\/span> 1<\/span>;<\/span>\n}<\/span>\n\nmessage ReplyState { \n   <\/span>\n    string reply_info =<\/span> 1<\/span>;<\/span>\n    KvPair kvpair =<\/span> 2<\/span>;<\/span>\n}<\/span>\n\nmessage RequestState { \n   <\/span>\n    string request_info =<\/span> 1<\/span>;<\/span>\n}<\/span>\n\nservice MyRpc { \n   <\/span>\n    \/\/simple rpc<\/span>\n    rpc SetKv<\/span>(<\/span>KvPair)<\/span> returns<\/span> (<\/span>ReplyState)<\/span> { \n   <\/span>}<\/span>\n    rpc GetKv<\/span>(<\/span>Key)<\/span> returns<\/span> (<\/span>ReplyState)<\/span> { \n   <\/span>}<\/span>\n\n    \/\/ A server-to-client streaming RPC.<\/span>\n    rpc GetKvList<\/span>(<\/span>RequestState)<\/span> returns<\/span> (<\/span>stream KvPair)<\/span> { \n   <\/span>}<\/span>\n\n    \/\/ A client-to-server streaming RPC.<\/span>\n    rpc SetKvList<\/span>(<\/span>stream KvPair)<\/span> returns<\/span> (<\/span>ReplyState)<\/span> { \n   <\/span>}<\/span>\n}<\/span>\n<\/code><\/pre>\n

2.\u5c06Proto\u8f6c\u6362\u4e3aRust\u6570\u636e\u7ed3\u6784<\/h2>\n

\u901a\u8fc7tonic\u81ea\u5e26\u7684tonic_build\u5de5\u5177\uff08\u57fa\u4e8eprost\uff09\u5c06proto\u6570\u636e\u53ca\u65b9\u6cd5\u8f6c\u6362\u4e3aRust\u6570\u636e\u7ed3\u6784\u53ca\u65b9\u6cd5\uff0cbuild.rs\u5728\u6784\u5efa\u6e90\u6587\u4ef6\u524d\u8fdb\u884c\u9884\u6784\u5efa\uff0c\u751f\u6210Rust\u4ee3\u7801\u3002<\/p>\n

fn<\/span> main<\/span>(<\/span>)<\/span> { \n   <\/span>\n    tonic_build::<\/span><\/span>configure<\/span>(<\/span>)<\/span>\n        .<\/span>out_dir<\/span>(<\/span>\"src\/pb\"<\/span>)<\/span>\n        .<\/span>compile<\/span>(<\/span>&<\/span>[<\/span>\"proto\/my_proto.proto\"<\/span>]<\/span>,<\/span> &<\/span>[<\/span>\".\"<\/span>]<\/span>)<\/span>\n        .<\/span>expect<\/span>(<\/span>\"failed to compile protos\"<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u6784\u5efa\u540e\u4f1a\u5728src\/pb<\/code>\u76ee\u5f55\u4e0b\u751f\u6210myproto.rs<\/code>\u6587\u4ef6\uff0c\u6587\u4ef6\u90e8\u5206\u5185\u5bb9\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

(1)\u8be5\u6587\u4ef6\u4e2d\u5305\u542b\u5728rust\u4e0b\u5b9e\u73b0\u7684proto\u4e2d\u7684\u6570\u636e\u7ed3\u6784:<\/strong><\/p>\n

#[derive(Clone, PartialEq, ::prost::Message)]<\/span>\npub<\/span> struct<\/span> KvPair<\/span> { \n   <\/span>\n    #[prost(message, optional, tag=\"1\"<\/span>)]<\/span>\n    pub<\/span> key:<\/span> ::<\/span>core::<\/span>option::<\/span><\/span>Option<\/span><<\/span>Key<\/span>><\/span>,<\/span>\n    #[prost(message, optional, tag=\"2\"<\/span>)]<\/span>\n    pub<\/span> val:<\/span> ::<\/span>core::<\/span>option::<\/span><\/span>Option<\/span><<\/span>Value<\/span>><\/span>,<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u5e76\u521b\u5efa\u4e862\u4e2amod<\/code>\uff1a<\/p>\n

pub<\/span> mod<\/span> my_rpc_client<\/span>\npub<\/span> mod<\/span> my_rpc_server<\/span>\n<\/code><\/pre>\n

(2)\u5728 mod my_rpc_client<\/code>\u4e0b\u521b\u5efa\u4e86\u7ed3\u6784\u4f53\uff1a<\/strong><\/p>\n

pub<\/span> struct<\/span> MyRpcClient<\/span><<\/span>T<\/span>><\/span> { \n   <\/span>\n    inner:<\/span> tonic::<\/span>client::<\/span><\/span>Grpc<\/span><<\/span>T<\/span>><\/span>,<\/span>\n}<\/span>\n<\/code><\/pre>\n

\u5e76\u4e3a\u8be5\u7ed3\u6784\u4f53\u6cdb\u578b\u4e3atonic::transport::Channel<\/code>\u65f6\u5b9e\u73b0\u4e86\u7279\u5316\u65b9\u6cd5\uff1a<\/p>\n

impl<\/span> MyRpcClient<\/span><<\/span>tonic::<\/span>transport::<\/span><\/span>Channel<\/span>><\/span> { \n   <\/span>\n        \/\/\/ Attempt to create a new client by connecting to a given endpoint.<\/span>\n        pub<\/span> async<\/span> fn<\/span> connect<\/span><<\/span>D<\/span>><\/span>(<\/span>dst:<\/span> D<\/span>)<\/span> -><\/span> Result<\/span><<\/span>Self<\/span>,<\/span> tonic::<\/span>transport::<\/span><\/span>Error<\/span>><\/span>\n        where<\/span>\n            D<\/span>:<\/span> std::<\/span>convert::<\/span><\/span>TryInto<\/span><<\/span>tonic::<\/span>transport::<\/span><\/span>Endpoint<\/span>><\/span>,<\/span>\n            D<\/span>::<\/span>Error<\/span>:<\/span> Into<\/span><<\/span>StdError<\/span>><\/span>,<\/span>\n        { \n   <\/span>\n            let<\/span> conn =<\/span> tonic::<\/span>transport::<\/span><\/span>Endpoint<\/span>::<\/span>new<\/span>(<\/span>dst)<\/span>?<\/span>.<\/span>connect<\/span>(<\/span>)<\/span>.<\/span>await<\/span>?<\/span>;<\/span>\n            Ok<\/span>(<\/span>Self<\/span>::<\/span>new<\/span>(<\/span>conn)<\/span>)<\/span>\n        }<\/span>\n    }<\/span>\n<\/code><\/pre>\n

\u4ee5\u53ca\u4e00\u4e9b\u5e38\u89c4\u65b9\u6cd5\u5982\uff1a
pub fn new(inner: T) -> Self<\/code>\uff0c
pub fn send_gzip(mut self) -> Self<\/code>\uff0c
pub fn accept_gzip(mut self) -> Self<\/code>\uff0c
......<\/code><\/p>\n

\u8fd8\u6709\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u65b9\u6cd5\uff1a<\/p>\n

pub<\/span> async<\/span> fn<\/span> set_kv<\/span>(<\/span>\n            &<\/span>mut<\/span> self<\/span>,<\/span>\n            request:<\/span> impl<\/span> tonic::<\/span><\/span>IntoRequest<\/span><<\/span>super<\/span>::<\/span>KvPair<\/span>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>tonic::<\/span><\/span>Response<\/span><<\/span>super<\/span>::<\/span>ReplyState<\/span>><\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span>{ \n   <\/span>...<\/span>}<\/span>\npub<\/span> async<\/span> fn<\/span> get_kv<\/span>(<\/span>\n            &<\/span>mut<\/span> self<\/span>,<\/span>\n            request:<\/span> impl<\/span> tonic::<\/span><\/span>IntoRequest<\/span><<\/span>super<\/span>::<\/span>Key<\/span>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>tonic::<\/span><\/span>Response<\/span><<\/span>super<\/span>::<\/span>ReplyState<\/span>><\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span>{ \n   <\/span>...<\/span>}<\/span>\npub<\/span> async<\/span> fn<\/span> get_kv_list<\/span>(<\/span>\n            &<\/span>mut<\/span> self<\/span>,<\/span>\n            request:<\/span> impl<\/span> tonic::<\/span><\/span>IntoRequest<\/span><<\/span>super<\/span>::<\/span>RequestState<\/span>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>\n            tonic::<\/span><\/span>Response<\/span><<\/span>tonic::<\/span>codec::<\/span><\/span>Streaming<\/span><<\/span>super<\/span>::<\/span>KvPair<\/span>>><\/span>,<\/span>\n            tonic::<\/span><\/span>Status<\/span>,<\/span>\n        ><\/span> { \n   <\/span>...<\/span>}<\/span>\npub<\/span> async<\/span> fn<\/span> set_kv_list<\/span>(<\/span>\n            &<\/span>mut<\/span> self<\/span>,<\/span>\n            request:<\/span> impl<\/span> tonic::<\/span><\/span>IntoStreamingRequest<\/span><<\/span>Message<\/span> =<\/span> super<\/span>::<\/span>KvPair<\/span>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>tonic::<\/span><\/span>Response<\/span><<\/span>super<\/span>::<\/span>ReplyState<\/span>><\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span> { \n   <\/span>...<\/span>}<\/span>\n<\/code><\/pre>\n

(3)\u5728 mod my_rpc_server<\/code>\u4e0b\u521b\u5efa\u4e86trait<\/code>\u53ca\u7ed3\u6784\u4f53\uff1a<\/strong><\/p>\n

#[async_trait]<\/span>\npub<\/span> trait<\/span> MyRpc<\/span>:<\/span> Send<\/span> +<\/span> Sync<\/span> +<\/span> 'static<\/span> { \n   <\/span>\n        \/\/\/simple rpc<\/span>\n        async<\/span> fn<\/span> set_kv<\/span>(<\/span>\n            &<\/span>self<\/span>,<\/span>\n            request:<\/span> tonic::<\/span><\/span>Request<\/span><<\/span>super<\/span>::<\/span>KvPair<\/span>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>tonic::<\/span><\/span>Response<\/span><<\/span>super<\/span>::<\/span>ReplyState<\/span>><\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span>;<\/span>\n        async<\/span> fn<\/span> get_kv<\/span>(<\/span>\n            &<\/span>self<\/span>,<\/span>\n            request:<\/span> tonic::<\/span><\/span>Request<\/span><<\/span>super<\/span>::<\/span>Key<\/span>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>tonic::<\/span><\/span>Response<\/span><<\/span>super<\/span>::<\/span>ReplyState<\/span>><\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span>;<\/span>\n        \/\/\/Server streaming response type for the GetKvList method.<\/span>\n        type<\/span> GetKvListStream<\/span>:<\/span> futures_core::<\/span><\/span>Stream<\/span><<\/span>\n                Item<\/span> =<\/span> Result<\/span><<\/span>super<\/span>::<\/span>KvPair<\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span>,<\/span>\n            ><\/span>\n            +<\/span> Send<\/span>\n            +<\/span> 'static<\/span>;<\/span>\n        \/\/\/ A server-to-client streaming RPC.<\/span>\n        async<\/span> fn<\/span> get_kv_list<\/span>(<\/span>\n            &<\/span>self<\/span>,<\/span>\n            request:<\/span> tonic::<\/span><\/span>Request<\/span><<\/span>super<\/span>::<\/span>RequestState<\/span>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>tonic::<\/span><\/span>Response<\/span><<\/span>Self<\/span>::<\/span>GetKvListStream<\/span>><\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span>;<\/span>\n        \/\/\/ A client-to-server streaming RPC.<\/span>\n        async<\/span> fn<\/span> set_kv_list<\/span>(<\/span>\n            &<\/span>self<\/span>,<\/span>\n            request:<\/span> tonic::<\/span><\/span>Request<\/span><<\/span>tonic::<\/span><\/span>Streaming<\/span><<\/span>super<\/span>::<\/span>KvPair<\/span>>><\/span>,<\/span>\n        )<\/span> -><\/span> Result<\/span><<\/span>tonic::<\/span><\/span>Response<\/span><<\/span>super<\/span>::<\/span>ReplyState<\/span>><\/span>,<\/span> tonic::<\/span><\/span>Status<\/span>><\/span>;<\/span>\n    }<\/span>\n<\/code><\/pre>\n
\t#[derive(Debug)]<\/span>\n    pub<\/span> struct<\/span> MyRpcServer<\/span><<\/span>T<\/span>:<\/span> MyRpc<\/span>><\/span> { \n   <\/span>\n        inner:<\/span> _Inner<<\/span>T<\/span>><\/span>,<\/span>\n        accept_compression_encodings:<\/span> (<\/span>)<\/span>,<\/span>\n        send_compression_encodings:<\/span> (<\/span>)<\/span>,<\/span>\n    }<\/span>\n    struct<\/span> _Inner<\/span><<\/span>T<\/span>><\/span>(<\/span>Arc<\/span><<\/span>T<\/span>><\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

\u4e3a\u8be5\u7ed3\u6784\u5b9e\u73b0\uff1a<\/p>\n

\timpl<\/span><<\/span>T<\/span>,<\/span> B<\/span>><\/span> tonic::<\/span>codegen::<\/span><\/span>Service<\/span><<\/span>http::<\/span><\/span>Request<\/span><<\/span>B<\/span>>><\/span> for<\/span> MyRpcServer<\/span><<\/span>T<\/span>><\/span>\n\twhere<\/span>\n\t    T<\/span>:<\/span> MyRpc<\/span>,<\/span>\n\t    B<\/span>:<\/span> Body<\/span> +<\/span> Send<\/span> +<\/span> 'static<\/span>,<\/span>\n\t    B<\/span>::<\/span>Error<\/span>:<\/span> Into<\/span><<\/span>StdError<\/span>><\/span> +<\/span> Send<\/span> +<\/span> 'static<\/span>,<\/span>\n\t        \n\timpl<\/span><<\/span>T<\/span>:<\/span> MyRpc<\/span>><\/span> Clone<\/span> for<\/span> MyRpcServer<\/span><<\/span>T<\/span>><\/span>\n\timpl<\/span><<\/span>T<\/span>:<\/span> MyRpc<\/span>><\/span> Clone<\/span> for<\/span> _Inner<<\/span>T<\/span>><\/span>\n\timpl<\/span><<\/span>T<\/span>:<\/span> std::<\/span>fmt::<\/span><\/span>Debug<\/span>><\/span> std::<\/span>fmt::<\/span><\/span>Debug<\/span> for<\/span> _Inner<<\/span>T<\/span>><\/span>\n\timpl<\/span><<\/span>T<\/span>:<\/span> MyRpc<\/span>><\/span> tonic::<\/span>transport::<\/span><\/span>NamedService<\/span> for<\/span> MyRpcServer<\/span><<\/span>T<\/span>><\/span>\n<\/code><\/pre>\n

3.\u670d\u52a1\u5668\u4ee3\u7801<\/h2>\n

\uff081\uff09\u9996\u5148\u5f15\u5165myproto.rs<\/code>\u6587\u4ef6<\/strong><\/p>\n

pub<\/span> mod<\/span> myproto<\/span> { \n   <\/span>\n    include!<\/span>(<\/span>\"pb\/myproto.rs\"<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n

\uff082\uff09\u7136\u540e\u5efa\u7acbRPC\u670d\u52a1\u7ed3\u6784\u4f53\u5e76\u5b9e\u73b0MyRpc trait<\/code><\/strong>
\u6ce8\u610f\uff1a\u5728\u5f02\u6b65\u534f\u7a0b\u73af\u5883\u4e0b\u8981\u4f7f\u7528futures_util::lock::Mutex;<\/code>\u800c\u4e0d\u662f\u666e\u901a\u7684Mutex\uff0c\u9632\u6b62\u6b7b\u9501\u3002<\/p>\n

#[derive(Debug)]<\/span>\npub<\/span> struct<\/span> MyRpcService<\/span> { \n   <\/span>\n    table:<\/span> Arc<\/span><<\/span>Mutex<\/span><<\/span>HashMap<\/span><<\/span>Key<\/span>,<\/span> Value<\/span>>><\/span>><\/span>,<\/span>\n}<\/span>\n\n#[tonic::async_trait]<\/span>\nimpl<\/span> MyRpc<\/span> for<\/span> MyRpcService<\/span> { \n   <\/span>\n    async<\/span> fn<\/span> set_kv<\/span>(<\/span>\n        &<\/span>self<\/span>,<\/span>\n        _request:<\/span> Request<\/span><<\/span>KvPair<\/span>><\/span>,<\/span>\n    )<\/span> -><\/span> Result<\/span><<\/span>Response<\/span><<\/span>ReplyState<\/span>><\/span>,<\/span> Status<\/span>><\/span> { \n   <\/span>\n        println!<\/span>(<\/span>\"set_kv = {:?}\"<\/span>,<\/span> _request)<\/span>;<\/span>\n\n        let<\/span> kvpair =<\/span> _request.<\/span>into_inner<\/span>(<\/span>)<\/span>;<\/span>\n        let<\/span> k =<\/span> kvpair.<\/span>key.<\/span>expect<\/span>(<\/span>\"key should not none.\"<\/span>)<\/span>;<\/span>\n        let<\/span> v =<\/span> kvpair.<\/span>val.<\/span>expect<\/span>(<\/span>\"value should not none.\"<\/span>)<\/span>;<\/span>\n        let<\/span> tb =<\/span> self<\/span>.<\/span>table.<\/span>clone<\/span>(<\/span>)<\/span>;<\/span>\n\n        if<\/span> let<\/span> Some<\/span>(<\/span>val)<\/span> =<\/span> \n            tb.<\/span>lock<\/span>(<\/span>)<\/span>.<\/span>await<\/span>.<\/span>insert<\/span>(<\/span>k.<\/span>clone<\/span>(<\/span>)<\/span>,<\/span> v)<\/span> { \n   <\/span>\n            return<\/span> Ok<\/span>(<\/span>Response<\/span>::<\/span>new<\/span>(<\/span>ReplyState<\/span> { \n   <\/span>\n                reply_info:<\/span> \"update value, return old.\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>,<\/span>\n                kvpair:<\/span> Some<\/span>(<\/span>KvPair<\/span> { \n   <\/span>\n                    key:<\/span> Some<\/span>(<\/span>k)<\/span>,<\/span>\n                    val:<\/span> Some<\/span>(<\/span>val.<\/span>clone<\/span>(<\/span>)<\/span>)<\/span>,<\/span>\n                }<\/span>)<\/span>,<\/span>\n            }<\/span>)<\/span>)<\/span>;<\/span>\n        }<\/span>\n\n        Ok<\/span>(<\/span>Response<\/span>::<\/span>new<\/span>(<\/span>\n            ReplyState<\/span>{ \n   <\/span>\n                reply_info:<\/span> \"set new kvpair.\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>,<\/span>\n                kvpair:<\/span> Some<\/span>(<\/span>KvPair<\/span> { \n   <\/span>\n                    key:<\/span> Some<\/span>(<\/span>k)<\/span>,<\/span>\n                    val:<\/span> None<\/span>,<\/span>\n                }<\/span>)<\/span>,<\/span>\n            }<\/span>)<\/span>)<\/span>\n    }<\/span>\n\n    async<\/span> fn<\/span> get_kv<\/span>(<\/span>\n        &<\/span>self<\/span>,<\/span>\n        _request:<\/span> Request<\/span><<\/span>Key<\/span>><\/span>,<\/span>\n    )<\/span> -><\/span> Result<\/span><<\/span>Response<\/span><<\/span>ReplyState<\/span>><\/span>,<\/span> Status<\/span>><\/span> { \n   <\/span>\n        println!<\/span>(<\/span>\"get_kv = {:?}\"<\/span>,<\/span> _request)<\/span>;<\/span>\n\n        let<\/span> k =<\/span> _request.<\/span>into_inner<\/span>(<\/span>)<\/span>;<\/span>\n        let<\/span> tb =<\/span> self<\/span>.<\/span>table.<\/span>clone<\/span>(<\/span>)<\/span>;<\/span>\n        \n        if<\/span> let<\/span> Some<\/span>(<\/span>val)<\/span> =<\/span> \n            tb.<\/span>lock<\/span>(<\/span>)<\/span>.<\/span>await<\/span>.<\/span>get<\/span>(<\/span>&<\/span>k)<\/span> { \n   <\/span>\n            return<\/span> Ok<\/span>(<\/span>Response<\/span>::<\/span>new<\/span>(<\/span>ReplyState<\/span> { \n   <\/span>\n                reply_info:<\/span> \"get success.\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>,<\/span>\n                kvpair:<\/span> Some<\/span>(<\/span>KvPair<\/span> { \n   <\/span>\n                    key:<\/span> Some<\/span>(<\/span>k)<\/span>,<\/span>\n                    val:<\/span> Some<\/span>(<\/span>val.<\/span>clone<\/span>(<\/span>)<\/span>)<\/span>,<\/span>\n                }<\/span>)<\/span>,<\/span>\n            }<\/span>)<\/span>)<\/span>\n        }<\/span>\n\n        Ok<\/span>(<\/span>Response<\/span>::<\/span>new<\/span>(<\/span>ReplyState<\/span> { \n   <\/span>\n            reply_info:<\/span> \"get failed.\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>,<\/span>\n            kvpair:<\/span> Some<\/span>(<\/span>KvPair<\/span> { \n   <\/span>\n                key:<\/span> Some<\/span>(<\/span>k)<\/span>,<\/span>\n                val:<\/span> None<\/span>,<\/span>\n            }<\/span>)<\/span>,<\/span>\n        }<\/span>)<\/span>)<\/span>\n    }<\/span>   \n\n    \/\/\/Server streaming response type for the GetKvList method.<\/span>\n    type<\/span> GetKvListStream<\/span> =<\/span> ReceiverStream<\/span><<\/span>Result<\/span><<\/span>KvPair<\/span>,<\/span> Status<\/span>>><\/span>;<\/span>\n\n    \/\/\/ A server-to-client streaming RPC.<\/span>\n    async<\/span> fn<\/span> get_kv_list<\/span>(<\/span>\n        &<\/span>self<\/span>,<\/span>\n        _request:<\/span> Request<\/span><<\/span>RequestState<\/span>><\/span>,<\/span>\n    )<\/span> -><\/span> Result<\/span><<\/span>Response<\/span><<\/span>Self<\/span>::<\/span>GetKvListStream<\/span>><\/span>,<\/span> Status<\/span>><\/span> { \n   <\/span>\n        println!<\/span>(<\/span>\"get_kv_list = {:?}\"<\/span>,<\/span> _request)<\/span>;<\/span>\n        let<\/span> (<\/span>tx,<\/span> rx)<\/span> =<\/span> mpsc::<\/span><\/span>channel<\/span>(<\/span>10<\/span>)<\/span>;<\/span>\n        let<\/span> tb =<\/span> self<\/span>.<\/span>table.<\/span>clone<\/span>(<\/span>)<\/span>;<\/span>\n\n        tokio::<\/span><\/span>spawn<\/span>(<\/span>async<\/span> move<\/span> { \n   <\/span>\n            for<\/span> (<\/span>k,<\/span> v)<\/span> in<\/span> tb.<\/span>lock<\/span>(<\/span>)<\/span>.<\/span>await<\/span>.<\/span>iter<\/span>(<\/span>)<\/span> { \n   <\/span>\n                println!<\/span>(<\/span>\" => send k = {:?}, v = {:?}\"<\/span>,<\/span> k,<\/span> v)<\/span>;<\/span>\n                tx.<\/span>send<\/span>(<\/span>Ok<\/span>(<\/span>KvPair<\/span> { \n   <\/span>\n                    key:<\/span> Some<\/span>(<\/span>k.<\/span>clone<\/span>(<\/span>)<\/span>)<\/span>,<\/span>\n                    val:<\/span> Some<\/span>(<\/span>v.<\/span>clone<\/span>(<\/span>)<\/span>)<\/span>,<\/span>\n                }<\/span>)<\/span>)<\/span>.<\/span>await<\/span>.<\/span>unwrap<\/span>(<\/span>)<\/span>;<\/span>\n            }<\/span>\n            println!<\/span>(<\/span>\" \/\/\/ done sending\"<\/span>)<\/span>;<\/span>\n        }<\/span>)<\/span>;<\/span>\n\n        Ok<\/span>(<\/span>Response<\/span>::<\/span>new<\/span>(<\/span>ReceiverStream<\/span>::<\/span>new<\/span>(<\/span>rx)<\/span>)<\/span>)<\/span>\n    }<\/span>\n\n    \/\/\/ A client-to-server streaming RPC.<\/span>\n    async<\/span> fn<\/span> set_kv_list<\/span>(<\/span>\n        &<\/span>self<\/span>,<\/span>\n        _request:<\/span> Request<\/span><<\/span>tonic::<\/span><\/span>Streaming<\/span><<\/span>KvPair<\/span>>><\/span>,<\/span>\n    )<\/span> -><\/span> Result<\/span><<\/span>Response<\/span><<\/span>ReplyState<\/span>><\/span>,<\/span> Status<\/span>><\/span> { \n   <\/span>\n        println!<\/span>(<\/span>\"set_kv_list = {:?}\"<\/span>,<\/span> _request)<\/span>;<\/span>\n\n        let<\/span> tb =<\/span> self<\/span>.<\/span>table.<\/span>clone<\/span>(<\/span>)<\/span>;<\/span>\n        let<\/span> mut<\/span> stream =<\/span> _request.<\/span>into_inner<\/span>(<\/span>)<\/span>;<\/span>\n\n        while<\/span> let<\/span> Some<\/span>(<\/span>kvpair)<\/span> =<\/span> stream.<\/span>next<\/span>(<\/span>)<\/span>.<\/span>await<\/span> { \n   <\/span>\n            let<\/span> kvpair =<\/span> kvpair?<\/span>;<\/span>\/\/stream.next().await -> Option<Result<...>><\/span>\n            let<\/span> k =<\/span> kvpair.<\/span>key;<\/span>\n            let<\/span> v =<\/span> kvpair.<\/span>val;<\/span>\n            tb.<\/span>lock<\/span>(<\/span>)<\/span>.<\/span>await<\/span>.<\/span>insert<\/span>(<\/span>k.<\/span>unwrap<\/span>(<\/span>)<\/span>,<\/span> v.<\/span>unwrap<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n        }<\/span>\n\n        Ok<\/span>(<\/span>Response<\/span>::<\/span>new<\/span>(<\/span>ReplyState<\/span> { \n   <\/span>\n            reply_info:<\/span> \"set all kvpair done.\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>,<\/span>\n            kvpair:<\/span> None<\/span>,<\/span>\n        }<\/span>)<\/span>)<\/span>\n    }<\/span>\n}<\/span>\n<\/code><\/pre>\n

\uff083\uff09\u7f16\u5199\u4e3b\u51fd\u6570<\/strong>
\u2003\u2003\u5728\u4e3b\u51fd\u6570\u4e2d\u542f\u52a8RPC\u670d\u52a1<\/p>\n

#[tokio::main]<\/span>\nasync<\/span> fn<\/span> main<\/span>(<\/span>)<\/span> -><\/span> Result<\/span><<\/span>(<\/span>)<\/span>,<\/span> Box<\/span><<\/span>dyn<\/span> std::<\/span>error::<\/span><\/span>Error<\/span>>><\/span>{ \n   <\/span>\n    let<\/span> addr =<\/span> \"[::1]:10000\"<\/span>.<\/span>parse<\/span>(<\/span>)<\/span>.<\/span>unwrap<\/span>(<\/span>)<\/span>;<\/span>\n\n    let<\/span> my_rpc =<\/span> MyRpcService<\/span> { \n   <\/span>\n        table:<\/span> Arc<\/span>::<\/span>new<\/span>(<\/span>Mutex<\/span>::<\/span>new<\/span>(<\/span>HashMap<\/span>::<\/span>new<\/span>(<\/span>)<\/span>)<\/span>)<\/span>,<\/span>\n    }<\/span>;<\/span>\n\n    let<\/span> svc =<\/span> MyRpcServer<\/span>::<\/span>new<\/span>(<\/span>my_rpc)<\/span>;<\/span>\n\n    Server<\/span>::<\/span>builder<\/span>(<\/span>)<\/span>.<\/span>add_service<\/span>(<\/span>svc)<\/span>.<\/span>serve<\/span>(<\/span>addr)<\/span>.<\/span>await<\/span>?<\/span>;<\/span>\n\n    Ok<\/span>(<\/span>(<\/span>)<\/span>)<\/span>\n}<\/span>\n<\/code><\/pre>\n

4.\u5ba2\u6237\u7aef\u4ee3\u7801<\/h2>\n

\uff081\uff09\u5ba2\u6237\u7aef\u4ee3\u7801\u5f15\u5165myproto.rs<\/code>\u6587\u4ef6<\/strong><\/p>\n

pub<\/span> mod<\/span> myproto<\/span> { \n   <\/span>\n    include!<\/span>(<\/span>\"pb\/myproto.rs\"<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n

\uff082\uff09\u5b9e\u73b0\u6279\u91cf\u5199\u5165\u548c\u8bfb\u53d6\u51fd\u6570<\/strong><\/p>\n

async<\/span> fn<\/span> print_kv_list<\/span>(<\/span>client:<\/span> &<\/span>mut<\/span> MyRpcClient<\/span><<\/span>Channel<\/span>><\/span>)<\/span> -><\/span> Result<\/span><<\/span>(<\/span>)<\/span>,<\/span> Box<\/span><<\/span>dyn<\/span> Error<\/span>>><\/span> { \n   <\/span>\n    let<\/span> rqs =<\/span> RequestState<\/span> { \n   <\/span>\n        request_info:<\/span> \"get all.\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>,<\/span>\n    }<\/span>;<\/span>\n\n    let<\/span> mut<\/span> stream =<\/span> client\n        .<\/span>get_kv_list<\/span>(<\/span>Request<\/span>::<\/span>new<\/span>(<\/span>rqs)<\/span>)<\/span>\n        .<\/span>await<\/span>?<\/span>\n        .<\/span>into_inner<\/span>(<\/span>)<\/span>;<\/span>\n\n    while<\/span> let<\/span> Some<\/span>(<\/span>kvpair)<\/span> =<\/span> stream.<\/span>message<\/span>(<\/span>)<\/span>.<\/span>await<\/span>?<\/span> { \n   <\/span>\n        println!<\/span>(<\/span>\"KvPair = {:?}\"<\/span>,<\/span> kvpair)<\/span>;<\/span>\n    }<\/span>\n\n    Ok<\/span>(<\/span>(<\/span>)<\/span>)<\/span>\n}<\/span>\n\nasync<\/span> fn<\/span> run_set_kv_list<\/span>(<\/span>client:<\/span> &<\/span>mut<\/span> MyRpcClient<\/span><<\/span>Channel<\/span>><\/span>)<\/span> -><\/span> Result<\/span><<\/span>(<\/span>)<\/span>,<\/span> Box<\/span><<\/span>dyn<\/span> Error<\/span>>><\/span> { \n   <\/span>\n    let<\/span> mut<\/span> pairs =<\/span> vec!<\/span>[<\/span>]<\/span>;<\/span>\n\n    for<\/span> i in<\/span> 0<\/span>..<\/span>3<\/span> { \n   <\/span>\n        pairs.<\/span>push<\/span>(<\/span>KvPair<\/span> { \n   <\/span>\n            key:<\/span> Some<\/span>(<\/span>Key<\/span>{ \n   <\/span>key:<\/span> i.<\/span>to_string<\/span>(<\/span>)<\/span>}<\/span>)<\/span>,<\/span>\n            val:<\/span> Some<\/span>(<\/span>Value<\/span>{ \n   <\/span>val:<\/span> i.<\/span>to_string<\/span>(<\/span>)<\/span>}<\/span>)<\/span>\n        }<\/span>)<\/span>\n    }<\/span>\n\n    println!<\/span>(<\/span>\"pairs num = {:?}\"<\/span>,<\/span> pairs.<\/span>len<\/span>(<\/span>)<\/span>)<\/span>;<\/span>\n    let<\/span> request =<\/span> Request<\/span>::<\/span>new<\/span>(<\/span>stream::<\/span><\/span>iter<\/span>(<\/span>pairs)<\/span>)<\/span>;<\/span>\n\n    match<\/span> client.<\/span>set_kv_list<\/span>(<\/span>request)<\/span>.<\/span>await<\/span> { \n   <\/span>\n        Ok<\/span>(<\/span>response)<\/span> =><\/span> println!<\/span>(<\/span>\"ReplyState = {:?}\"<\/span>,<\/span> response.<\/span>into_inner<\/span>(<\/span>)<\/span>)<\/span>,<\/span>\n        Err<\/span>(<\/span>e)<\/span> =><\/span> println!<\/span>(<\/span>\"something went wrong: {:?}\"<\/span>,<\/span> e)<\/span>,<\/span>\n    }<\/span>\n\n    Ok<\/span>(<\/span>(<\/span>)<\/span>)<\/span>\n}<\/span>\n<\/code><\/pre>\n

\uff083\uff09\u5ba2\u6237\u7aef\u4e3b\u51fd\u6570<\/strong><\/p>\n

\u2003\u2003\u5728\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528RPC\u670d\u52a1<\/p>\n

#[tokio::main]<\/span>\nasync<\/span> fn<\/span> main<\/span>(<\/span>)<\/span> -><\/span> Result<\/span><<\/span>(<\/span>)<\/span>,<\/span> Box<\/span><<\/span>dyn<\/span> std::<\/span>error::<\/span><\/span>Error<\/span>>><\/span> { \n   <\/span>\n    let<\/span> mut<\/span> client =<\/span> MyRpcClient<\/span>::<\/span>connect<\/span>(<\/span>\"http:\/\/[::1]:10000\"<\/span>)<\/span>.<\/span>await<\/span>?<\/span>;<\/span>\n    \n    println!<\/span>(<\/span>\"*** SIMPLE RPC ***\"<\/span>)<\/span>;<\/span>\n    let<\/span> response0 =<\/span> client\n    .<\/span>set_kv<\/span>(<\/span>Request<\/span>::<\/span>new<\/span>(<\/span>KvPair<\/span> { \n   <\/span>\n        key:<\/span> Some<\/span>(<\/span>Key<\/span>{ \n   <\/span>key:<\/span> \"a\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>}<\/span>)<\/span>,<\/span>\n        val:<\/span> Some<\/span>(<\/span>Value<\/span>{ \n   <\/span>val:<\/span> \"1\"<\/span>.<\/span> into<\/span>(<\/span>)<\/span>}<\/span>)<\/span>,<\/span>\n    }<\/span>)<\/span>)<\/span>\n    .<\/span>await<\/span>?<\/span>;<\/span>\n    println!<\/span>(<\/span>\"RESPONSE0 = {:?}\"<\/span>,<\/span> response0)<\/span>;<\/span>\n\n    let<\/span> response1 =<\/span> client\n    .<\/span>get_kv<\/span>(<\/span>Request<\/span>::<\/span>new<\/span>(<\/span>Key<\/span>{ \n   <\/span>key:<\/span> \"a\"<\/span>.<\/span>into<\/span>(<\/span>)<\/span>}<\/span>)<\/span>)<\/span>\n    .<\/span>await<\/span>?<\/span>;<\/span>\n    println!<\/span>(<\/span>\"RESPONSE1 = {:?}\"<\/span>,<\/span> response1)<\/span>;<\/span>\n\n    println!<\/span>(<\/span>\"\\n*** CLIENT STREAMING ***\"<\/span>)<\/span>;<\/span>\n    run_set_kv_list<\/span>(<\/span>&<\/span>mut<\/span> client)<\/span>.<\/span>await<\/span>?<\/span>;<\/span>\n\n    println!<\/span>(<\/span>\"\\n*** SERVER STREAMING ***\"<\/span>)<\/span>;<\/span>\n    print_kv_list<\/span>(<\/span>&<\/span>mut<\/span> client)<\/span>.<\/span>await<\/span>?<\/span>;<\/span>\n\n    Ok<\/span>(<\/span>(<\/span>)<\/span>)<\/span>\n}<\/span>\n<\/code><\/pre>\n

5.\u7b80\u5355\u6d4b\u8bd5<\/h2>\n

\uff081\uff09\u670d\u52a1\u5668\u6253\u5370\u60c5\u51b5<\/strong>
\"rust
\uff082\uff09\u5ba2\u6237\u7aef\u6253\u5370\u60c5\u51b5<\/strong>
\"rust
\u901a\u8fc7\u6807\u51c6\u8f93\u51fa\u7684\u5185\u5bb9\u53ef\u4ee5\u770b\u5230\uff1a
\uff081\uff09\u5ba2\u6237\u7aef\u901a\u8fc7set_kv(\"a\", 1)<\/code>\u53d1\u9001\u8bf7\u6c42\u5230\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u8fd4\u56de\u4e86\u54cd\u5e94RESPONSE0<\/code>\uff0c\u8868\u793a\u5df2\u7ecf\u6210\u529f\u5199\u5165\u3002
\uff082\uff09\u5ba2\u6237\u7aef\u8c03\u7528get_kv(\"a\")<\/code>\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u6b64\u65f6\u5ba2\u6237\u7aef\u6536\u5230\u4e86RESPONSE1<\/code>,\u5f97\u5230\u4e86\u7ed3\u679c(\"a\", 1)<\/code>\u3002
\uff083\uff09\u5ba2\u6237\u7aef\u8c03\u7528set_kv_list()<\/code>\uff0c\u6536\u5230\u4e86\u6279\u91cf\u5199\u5165\u6210\u529f\u7684\u54cd\u5e94\u3002
\uff084\uff09\u5ba2\u6237\u7aef\u8c03\u7528get_kv_list()<\/code>\uff0c\u6536\u5230\u4e86\u670d\u52a1\u5668\u5b58\u50a8\u7684\u6240\u6709KvPair<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"rust torch_Rust\u83dc\u9e1f\u6559\u7a0b\u300c\u5efa\u8bae\u6536\u85cf\u300d\u672c\u6587\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7tonic\uff08gRPC\u7684rust\u5b9e\u73b0\uff09\u5b9e\u73b0\u4e00\u5143RPC\u548c\u6d41\u5f0fRPC","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8892"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8892"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8892\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}