9 releases
0.1.9 | Aug 29, 2024 |
---|---|
0.1.8 | Aug 29, 2024 |
#8 in #service-mesh
77 downloads per month
Used in 2 crates
(via logimesh)
39KB
712 lines
logimesh
logimesh
is a Rust RPC Microservice 2.0 framework inspired by the Towards Modern Development of Cloud Applications paper.
(This is one of my amateur idea and is only developed in leisure-time.)
Some features of logimesh:
- The client supports both local calls and remote calls simultaneously, meaning that users can dynamically switch the calling method according to the context.
Usage
Add to your Cargo.toml
dependencies:
logimesh = "0.1"
The logimesh::component
attribute expands to a collection of items that form an component component.
These generated types make it easy and ergonomic to write servers with less boilerplate.
Simply implement the generated component trait, and you're off to the races!
Example
This example uses tokio, so add the following dependencies to
your Cargo.toml
:
[lib]
name = "service"
path = "src/lib.rs"
...
[dependencies]
logimesh = { version = "0.1" }
anyhow = "1.0"
tokio = { version = "1.0", features = ["macros"] }
For a more real-world example, see logimesh-example.
First, let's set up the dependencies and component definition.
lib.rs
file
extern crate logimesh;
// This is the component definition. It looks a lot like a trait definition.
// It defines one RPC, hello, which takes one arg, name, and returns a String.
#[logimesh::component]
trait World {
/// Returns a greeting for name.
async fn hello(name: String) -> String;
}
This component definition generates a trait called World
. Next we need to
implement it for our Server struct.
# extern crate logimesh;
#
# // This is the component definition. It looks a lot like a trait definition.
# // It defines one RPC, hello, which takes one arg, name, and returns a String.
# #[logimesh::component]
# trait World {
# /// Returns a greeting for name.
# async fn hello(name: String) -> String;
# }
use logimesh::context;
use logimesh::transport::codec::Codec;
/// This is the type that implements the generated World trait. It is the business logic
/// and is used to start the server.
#[derive(Clone)]
pub struct CompHello;
impl World for CompHello {
const TRANSPORT_CODEC: Codec = Codec::Json;
async fn hello(self, ctx: context::Context, name: String) -> String {
format!("Hello, {name}! context: {:?}", ctx)
}
}
server.rs
file
extern crate logimesh;
extern crate tokio;
extern crate anyhow;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
logimesh::tokio_tcp_listen!(CompHello, logimesh::server::TcpConfig::new("[::1]:8888".parse::<std::net::SocketAddrV6>().unwrap()));
Ok(())
}
client.rs
file
extern crate logimesh;
extern crate tokio;
extern crate anyhow;
use logimesh::client::balance::RandomBalance;
use logimesh::client::discover::FixedDiscover;
use logimesh::client::lrcall::ConfigExt;
use logimesh::component::Endpoint;
use logimesh::context;
use service::{CompHello, World};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = CompHello
.logimesh_lrclient(
Endpoint::new("p.s.m"),
FixedDiscover::from_address(vec!["[::1]:8888".parse::<std::net::SocketAddrV6>().unwrap()]),
RandomBalance::new(),
ConfigExt::default(),
)
.await?;
// Send the request twice, just to be safe! ;)
let hello = tokio::select! {
hello1 = client.hello(context::current(), format!("{}1", flags.name)) => { hello1 }
hello2 = client.hello(context::current(), format!("{}2", flags.name)) => { hello2 }
};
match hello {
Ok(hello) => println!("{hello:?}"),
Err(e) => println!("{:?}", anyhow::Error::from(e)),
}
Ok(())
}
Dependencies
~250–700KB
~17K SLoC