Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

rpcx microservice framework in Rust

License

NotificationsYou must be signed in to change notification settings

smallnest/rpcx-rs

Repository files navigation

Build StatusCrateAPI

Rust library forrpcx rpc/microservice framework.

Use thesimplest style to explore Rust function as cross-platform rpc services.

If you can write Rust functions, you can write rpc services. It is so easy.

see all exampes:rpcx-rs-examples.

Roadmap

0.1.x

protocol and client/server lib.

  • Protocol
  • Client (call synchronous/asynchronous)
  • support JSON, MessagePack and Protobuf
  • Service implementation

0.2.x

  • Service discovery
    • static multiple peers
    • etcd
    • consul
  • service governance
  • Select Mode
    • RandomSelect,
    • RoundRobin
    • WeightedRoundRobin
    • WeightedICMP
    • ConsistentHash
    • Closest
    • Custiomized
  • Faile Mode
    • Failover
    • Failfast
    • Failtry

0.3.x

  • plugins
  • document
  • unit tests and integration tests
  • other features like implementation in Go

Usage

Add this to your Cargo.toml:

[dependencies]rpcx ="0.2.0"

Example

Write the Argument and the Reply

First you should write the argument and the reply. They are used by rpc services and clients.

use std::error::ErrorasStdError;use rmp_serdeas rmps;use serde::{Deserialize,Serialize};use rpcx_derive::*;use rpcx_protocol::{Error,ErrorKind,Result,RpcxParam,SerializeType};#[derive(RpcxParam,Default,Debug,Copy,Clone,Serialize,Deserialize)]pubstructArithAddArgs{#[serde(rename ="A")]puba:u64,#[serde(rename ="B")]pubb:u64,}#[derive(RpcxParam,Default,Debug,Copy,Clone,Serialize,Deserialize)]pubstructArithAddReply{#[serde(rename ="C")]pubc:u64,}

You must addRpcxParamSerializeDeserialize andDefault traits inderive. Rpcx can add hepler methods for serialization.

If not, you need to implementRpcxParam andDefault mannually.

Here we definedArithAddArgs as the argument type andArithAddReply as the reply type.

Implement the server

use mul_model::{ArithAddArgs,ArithAddReply};use rpcx::*;fnadd(args:ArithAddArgs) ->ArithAddReply{ArithAddReply{c: args.a + args.b}}fnmul(args:ArithAddArgs) ->ArithAddReply{ArithAddReply{c: args.a* args.b}}fnmain(){letmut rpc_server =Server::new("127.0.0.1:8972".to_owned());register_func!(        rpc_server,"Arith","Add",        add,ArithAddArgs,ArithAddReply);register_func!(        rpc_server,"Arith","Mul",        mul,ArithAddArgs,ArithAddReply);    rpc_server.start().unwrap();}

Here we implement two services:add andmul. And we useregister_func! macro to register them with their expored names(service_path andservice_method). Clients can use the name to access them.

Implement client

Here we use one client to accessArith.Mul service in a loop.

use std::collections::hash_map::HashMap;use mul_model::*;use rpcx::Client;use rpcx::{Result,SerializeType};pubfnmain(){letmut c:Client =Client::new("127.0.0.1:8972");    c.start().map_err(|err|println!("{}", err)).unwrap();    c.opt.serialize_type =SerializeType::JSON;letmut a =1;loop{let service_path =String::from("Arith");let service_method =String::from("Mul");let metadata =HashMap::new();let args =ArithAddArgs{a: a,b:10};        a +=1;let reply:Option<Result<ArithAddReply>> =            c.call(service_path, service_method,false, metadata,&args);match reply{Some(Ok(r)) =>println!("received: {:?}", r),Some(Err(err)) =>println!("received err:{}", err),None =>{}}}}

Actually you can use this client to access rpcx services implemented by other program languages such asservice in go.

As you see, only after three steps you have expored Rust functions (add andmul) as rpc services.

You can find more examples atrpcx-rs/examples

License

rpcx-rs is distributed under the terms of both the MIT license.

SeeLICENSE-APACHE andLICENSE-MIT, andCOPYRIGHT for details.

About

rpcx microservice framework in Rust

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp