rpcx-rs 0.2.2 版本,使用Rust访问rpcx服务,支持 JSON 和 MessagePack 两种序列化方式。
protobuf序列化的支持、服务治理各种功能(路由、失败处理、重试、熔断器、限流)、监控(metrics、trace)、注册中心(etcd、consul)等众多的功能
cat ../Cargo.toml
[package]
name = "rust_demo4"
version = "0.1.0"
edition = "2021"
[dependencies]
rpcx = "0.2.2"
serde = {
version = "1.0", features = ["derive"] }
serde_json = "1.0"
rmp-serde = "1.1.2"
// lib.rs
use std::error::Error as StdError;
use rmp_serde as rmps;
use serde::{
Deserialize, Serialize};
use rpcx::*;
#[derive(RpcxParam, Default, Debug, Copy, Clone, Serialize, Deserialize)]
pub struct ArithAddArgs {
#[serde(rename = "A")]
pub a: u64,
#[serde(rename = "B")]
pub b: u64,
}
#[derive(RpcxParam, Default, Debug, Copy, Clone, Serialize, Deserialize)]
pub struct ArithAddReply {
#[serde(rename = "C")]
pub c: u64,
}
// main.rs
use rust_demo4::{
ArithAddArgs, ArithAddReply};
use rpcx::*;
fn test(args: ArithAddArgs) -> ArithAddReply {
ArithAddReply {
c: args.a + args.b }
}
fn main() {
// 第二个参数为 0,表示绑定所需的服务器线程数,如果设置为 0,则使用默认值(通常为 CPU 数量)
let mut rpc_server = Server::new("0.0.0.0:8972".to_owned(), 0);
// macro_rules! register_func {
// (
// $rpc_server:expr,
// $service_path:expr,
// $service_method:expr,
// $service_fn:expr,
// $meta:expr,
// $arg_type:ty,
// $reply_type:ty
// ) => { ... };
// }
register_func!(
rpc_server,
"Arith", // service_path 服务名
"Add", // service_method 服务的方法
test, // service_fn 服务的函数名
"".to_owned(),// meta
ArithAddArgs, // 参数类型
ArithAddReply // 响应类型
);
// pub fn get_fn(
// &self,
// service_path: String,
// service_method: String
// ) -> Option Result, Error>>
let f = rpc_server
.get_fn(String::from("Arith"), String::from("Add")) // service_path、service_method
.unwrap(); // 返回 fn(_: &[u8], _: SerializeType) -> Result 或者 Error
let s = String::from(r#"{"A":1,"B":2}"#);
// 第一个参数是一个u8数组引用,即要传递的内容
// 第二个参数是序列化类型
let reply = f(s.as_ref(), SerializeType::JSON).unwrap();
println!("reply:{}", String::from_utf8(reply).unwrap());
}
服务端 —> service_path service_method service_fn 客户端 —> service_path service_method 请求参数
tree -L 1 -C -F -I ".*"
./
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
├── build.rs
├── clippy.toml
├── examples/
├── rpcx/
├── rpcx_client/
├── rpcx_derive/
├── rpcx_protocol/
├── rpcx_server/
├── rustfmt.toml
├── target/
└── test_suite/
cat src/lib.rs
use std::{
boxed::Box,
collections::HashMap,
sync::{
Arc, RwLock},
};
use std::net::SocketAddr;
use rpcx_protocol::*;
use std::{
io::{
BufReader, BufWriter, Write},
net::{
Shutdown,