How to use
Demo server
A server has any restful interface (like this : find_user_by_id, new_user)
curl 127.1:3000/user/find_by_id/1
# -> {"id":1,"name":"hello"}
curl -X POST 127.1:3000/user/new_user \
-H 'Content-Type: application/json' \
-d '{"id":1,"name":"Link"}'
# -> "Link" ➜ ~
Dependencies
- Add feign dependency to Cargo.toml
- Add reqwest dependency to Cargo.toml and enable feature json
- Add serde's dependencies to Cargo.toml, because inputs or outputs entities must be Serialize / Deserialize
[]
= "1"
= { = "0.11", = ["json"] }
= "1.0"
= "1.0"
# runtime
= { = "1.15", = ["macros", "rt-multi-thread"] }
Entites
Add a user entity add derives serde_derive::Deserialize and serde_derive::Serialize
use Deserialize;
use Serialize;
Feign client
- Use feign::client macro and trait make a feign client, host is server address, path is controller context. (The host can be dynamically replaced and can be ignored)
- In the trait, use the method macro and path args make a request, the member method must async and first arg is recover(&self)
- Use #[json] / #[form] post body, use #[path] for replace <arg_name> in request path
use ;
Demo
Use
async
user : hello
result : name
Dynamic modify host with configure_host
async