框架仓库
https://github.com/paritytech/jsonrpc#readme
使用
1.依赖
[dependencies]
jsonrpc-core = "18.0.0"
jsonrpc-core-client = "18.0.0"
jsonrpc-http-server = "18.0.0"
jsonrpc-ipc-server = "18.0.0"
jsonrpc-tcp-server= "18.0.0"
jsonrpc-ws-server = "18.0.0"
jsonrpc-stdio-server = "18.0.0"
jsonrpc-derive = "18.0.0"
jsonrpc-server-utils = "18.0.0"
jsonrpc-pubsub = "18.0.0"
2.hello wold
use jsonrpc_http_server::jsonrpc_core::{IoHandler, Params, Value};
use jsonrpc_http_server::ServerBuilder;
pub fn start_rpc_server() {
let mut io = IoHandler::default();
// 注册了一个say_hello方法,忽略参数,直接返回hello
io.add_method("say_hello", |_params: Params| async {
Ok(Value::String("hello".to_owned()))
});
let server = ServerBuilder::new(io)
.threads(3)
.start_http(&"127.0.0.1:3030".parse().unwrap())
.unwrap();
server.wait();
}
3.测试
>curl --location --request POST '127.0.0.1:3030' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc":"2.0",
"method":"say_hello",
"id":"say my name"
}'
{"jsonrpc":"2.0","result":"hello","id":"say my name"}