Crates.io | cakerabbit-core |
lib.rs | cakerabbit-core |
version | 0.1.3 |
source | src |
created_at | 2021-09-01 07:13:39.820496 |
updated_at | 2022-03-25 09:07:46.205281 |
description | A rust microservice framework, this is the core kernel for the project. |
homepage | |
repository | https://github.com/halokid/cakerabbit-core |
max_upload_size | |
id | 445474 |
size | 171,415 |
A rust microservice framework, this is the core kernel for the project.
Transport
Service
Server
Cilent
Register
Failmode
Selector
Random
RoundRobin
set in Cargo.toml:
[dependencies]
cakerabbit-core = "0.1.1"
code a EchoRs service, register method say_hello(). if you want to devlop use local project source, check the example in folder ./examples/echo_use_localsource. run command, check the toml:
cargo.exe run --example echoserver
cargo.exe run --example echoclient
if you want to devlop use crate.io source, check the example in folder ./examples/echo_use_crateio run command, check the toml:
cd ./examples/echo_use_crateio
cargo.exe run --example echoserver
# server with http
# cargo.exe run --examplee choserver_httpapi
cargo.exe run --example echoclient
Service with HTTP support, check the code server_use_cake_httpapi.rs
const SVC_NAME: &str = "EchoRs";
#[derive(Default, Serialize, Deserialize, Debug, Clone)]
struct SayHelloReply {
name: String,
}
fn say_hello(params: &[Value]) -> CakeResult<Vec<u8>> {
if let Value::String(ref value) = params[0] {
if let Some(val) = value.as_str() {
let rsp = SayHelloReply {
name: "foo".to_string()
};
let rsp_vec = serde_json::to_vec(&rsp).unwrap();
return Ok(rsp_vec);
}
}
Ok(Vec::from("wrong param".to_string()))
}
#[tokio::main]
async fn main() -> io::Result<()> {
env_logger::from_env(Env::default().default_filter_or("info")).init();
let mut svc_serve = CakeServiceServe::new(SVC_NAME.to_string(),
"cake/".to_string(),
"0.0.0.0:9527".to_string(),
"consul".to_string(),
"localhost:8500".to_string(),
"1m0s".to_string(),
false);
// todo: register svc method
svc_serve.register_fn("say_hello".into(),
say_hello,
&["foo".into()]);
// todo: run
svc_serve.run().await;
Ok(())
}
code a client call EchoRs service
#[tokio::main]
async fn main() -> io::Result<()> {
env_logger::init();
let fm = FailMode::Failtry(Failtry{ retries: 3 });
let mut cake_client = CakeClient::new("cake/".into(),
"EchoRs".into(),
"consul".into(),
"localhost:8500".into(),
SelectorTyp::RoundRobin,
FailMode::FailFast);
// fm);
let res = cake_client.call("say_hello", &["foo".into()]).await;
match res {
Ok(rsp) => {
println!("rsp ------------ {}", rsp);
}
Err(err) => {
println!("err ------------------ {:?}", err);
}
}
}
you can build a service use
Cannot start a runtime from within a runtime
, cakeRabbit is base on tokio runtimeEasy use, keep in simple
Gateway support to external service use RESTFUL api
More program language support, cakeRabbit-code transport codec base on msgpack(grpc etc.), it means if you write a service use rust, it can be call by other language.
UI for service governance, service register center can use consul, etcd, zookeeper.