Rust library for rpcx rpc/microservice framework.
Use the simplest 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.
protocol and client lib.
server lib. You can register services bu Rust and they can be invoked by other languages.
Add this to your Cargo.toml:
toml
[dependencies]
rpcx = "0.1.3"
First you should write the argument and the reply. They are used by rpc services and clients.
```rust use std::error::Error as StdError;
use rmp_serde as rmps; use serde::{Deserialize, Serialize};
use rpcxderive::*; use rpcxprotocol::{Error, ErrorKind, Result, RpcxParam, SerializeType};
pub struct ArithAddArgs { #[serde(rename = "A")] pub a: u64, #[serde(rename = "B")] pub b: u64, }
pub struct ArithAddReply { #[serde(rename = "C")] pub c: u64, } ```
You must add RpcxParam
、Serialize
、Deserialize
and Default
traits in derive
. Rpcx can add hepler methods for serialization.
If not, you need to implement RpcxParam
and Default
mannually.
Here we defined ArithAddArgs
as the argument type and ArithAddReply
as the reply type.
```rust use mul_model::{ArithAddArgs, ArithAddReply}; use rpcx::*;
fn add(args: ArithAddArgs) -> ArithAddReply { ArithAddReply { c: args.a + args.b } }
fn mul(args: ArithAddArgs) -> ArithAddReply { ArithAddReply { c: args.a * args.b } }
fn main() { let mut rpcserver = Server::new("127.0.0.1:8972".toowned()); registerfunc!( rpcserver, "Arith", "Add", add, ArithAddArgs, ArithAddReply );
register_func!(
rpc_server,
"Arith",
"Mul",
mul,
ArithAddArgs,
ArithAddReply
);
rpc_server.start().unwrap();
}
``
Here we implement two services:
addand
mul. And we use
registerfunc!macro to register them with their expored names(
servicepathand
service_method`). Clients can use the name to access them.
Here we use one client to access Arith.Mul
service in a loop.
```rust use std::collections::hash_map::HashMap;
use mul_model::*; use rpcx::Client; use rpcx::{Result, SerializeType};
pub fn main() { let mut c: Client = Client::new("127.0.0.1:8972"); c.start().maperr(|err| println!("{}", err)).unwrap(); c.opt.serializetype = SerializeType::JSON;
let mut 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 = a + 1;
let reply: Option<Result<ArithAddReply>> =
c.call(service_path, service_method, false, metadata, &args);
if reply.is_none() {
continue;
}
let result_reply = reply.unwrap();
match result_reply {
Ok(r) => println!("received: {:?}", r),
Err(err) => println!("received err:{}", err),
}
}
} ```
Actually you can use this client to access rpcx services implemented by other program languages such as service in go.
As you see, only after three steps you have expored Rust functions (add
and mul
) as rpc services.
You can find more examples at rpcx-rs/examples
rpcx-rs is distributed under the terms of both the MIT license.
See LICENSE-APACHE and LICENSE-MIT, and COPYRIGHT for details.