conreg-client 0.2.0-beta.2版本发布啦,支持创建声明式的 HTTP 客户端,类似于 Java Feign。通过为 trait 添加#[feign_client],可自动生成 HTTP 请求的实现代码,实现微服务之间的 HTTP 通信。
文档地址:https://docs.rs/conreg-client/0.2.0-beta.2/conreg_client/
使用方式:
toml
conreg-client = { version = "=0.2.0-beta.2", features = ["tracing", "feign"]}
示例:
rust
#[feign_client(service_id = "test-server")]
trait ExampleClient {
/// Request `GET` and return string.
#[get("/hello")]
async fn hello(&self) -> Result<String, FeignError>;
}
#[tokio::main]
async fn main() {
conreg_client::init().await;
let client = ExampleClientImpl::default();
let response = client.hello().await.unwrap();
println!("hello -> {:?}", response);
}
feign_client 宏
feign_client 宏作用于一个trait,它接收以下参数:
service_id:必填,服务的唯一标识符,用于服务发现和负载均衡。base_path:可选,基础路径前缀,将添加到所有请求路径之前。url:可选,直接指定请求的 URL,如果设置了此项,将忽略service_id和base_path。
feign_client 将自动实现其标记的trait,生成 <trait_name>Impl 的默认实现。
参数绑定
路径参数
在路径中使用 {param_name} 占位符。当方法参数名与占位符名称匹配时,会自动绑定。
示例:#[get("/api/users/{id}")]
查询参数
使用 query = "{param}" 指定查询参数模板。
示例:#[get(path = "/api/users", query = "id={id}")]
表单参数
form 虽然在服务间调用时并不常用,但是也支持,该参数类型必须为reqwest::multipart::Form。
使用 form = "{param}" 指定表单数据,支持 application/x-www-form-urlencoded 和 multipart/form-data。
示例:#[post(path = "/api/login", form = "{loginForm}")]
请求体参数
使用 body = "{param}" 指定原始请求体,请求体类型必须实现了Into<reqwest::Body>。
示例:#[post(path = "/api/post", body = "{data}")]
JSON 参数
使用 json = "{param}" 指定 JSON 数据;会自动序列化并设置 Content-Type: application/json。
示例:#[post(path = "/api/post", json = "{data}")]
JSON的序列化和反序列化依赖于 serde_json。
请求头
使用 headers("Key=Value", ...) 或 headers("Key={param}", ...) 指定请求头,支持静态值和动态参数。
示例:#[get(path = "/api/users", headers("Authorization=Bearer {token}", "Accept=application/json"))]
示例
以下是一个较为完整的示例:
rust
#[feign_client(service_id = "httpbin", url = "https://httpbin.org")]
trait ExampleClient {
/// Request `GET` and return string.
#[get("/ip")]
async fn ip(&self) -> Result<String, FeignError>;
/// Request `GET` and return json.
#[get(path = "/json")]
async fn json(&self) -> Result<serde_json::Value, FeignError>;
/// Request `GET` and return bytes.
#[get(path = "/image", headers("Accept=image/png"))]
async fn image(&self) -> Result<Bytes, FeignError>;
/// Post a form
#[post(path = "/post", form = "{form}")]
async fn form(&self, form: Form) -> Result<String, FeignError>;
/// 动态Header
#[get(path = "/headers", headers("My-Header={my_header}"))]
async fn headers(&self, my_header: &str) -> Result<String, FeignError>;
}
#[tokio::main]
async fn main() {
let client = ExampleClientImpl::default();
let response = client.ip().await.unwrap();
println!("ip -> {:?}", response);
let response = client.json().await.unwrap();
println!("json -> {:#?}", response);
let response = client.image().await.unwrap();
let path = Path::new("image.png");
std::fs::write(&path, response).unwrap();
println!("image saved -> {:?}", path.canonicalize().unwrap());
let form = Form::new().text("custname", "Hello, this is form form!");
let response = client.form(form).await.unwrap();
println!("form -> {:?}", response);
let response = client.headers("Hello, this is headers!").await.unwrap();
println!("headers -> {:?}", response);
}
使用限制
目前 conreg-client 仍处于早期阶段,未来API可能会有较大变化,目前还不建议用于生产环境。欢迎对此项目感兴趣的同学来一起改进。
仓库地址:conreg