【conreg-client】在Rust中使用向Feign一样的远程调用

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_idbase_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-urlencodedmultipart/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

相关推荐
double2li2 小时前
linux/unix 段错误捕获【续】
linux·运维·服务器·网络·unix
qq_260241232 小时前
将盾 CDN:网络网站安全防护体系与实践
网络·安全
dashizhi20152 小时前
禁止复制电脑文件、电脑机密数据禁止拷贝、禁止电脑文件复制到U盘和移动硬盘的方法
运维·网络·stm32·安全·电脑
Ralph_Y2 小时前
C++网络:一
开发语言·网络·c++
猪猪侠|ZZXia3 小时前
# Openssl关键知识
linux·网络
程序猿编码3 小时前
探秘 SSL/TLS 服务密码套件检测:原理、实现与核心设计(C/C++代码实现)
c语言·网络·c++·ssl·密码套件
江南西肥肥3 小时前
养虾日记[特殊字符]:多Agent在飞书群辩论--踩坑篇
网络·飞书·openclaw
网云工程师手记3 小时前
企业多出口负载与故障切换实战:4 种调度模式 + 主备线路高可用
运维·服务器·网络·安全·网络安全
上海云盾-高防顾问4 小时前
扫段攻击防御指南:简单几步,守住网络安全防线
网络·安全·web安全