快速开始
This content is not available in your language yet.
Feat Core 客户端提供轻量级、高性能的 HTTP/SSE/WebSocket 客户端实现,适合需要从 Java 程序主动连接外部服务、处理流式响应或建立双向实时通信的场景。
客户端类型选择
Section titled “客户端类型选择”| 特性 | HTTP | SSE | WebSocket |
|---|---|---|---|
| 通信模式 | 请求-响应 | 服务端推送 | 双向实时 |
| 连接方式 | 短连接 | 长连接 | 长连接 |
| 客户端发送 | ✅ | ❌ | ✅ |
| 服务端发送 | ✅ | ✅ | ✅ |
| 协议开销 | 高(每次请求头) | 中 | 低(首次握手后) |
| 适用场景 | REST API | 通知推送 | 聊天、协作 |
第一个 HTTP 请求
Section titled “第一个 HTTP 请求”- JDK: 8 或更高版本
- 构建工具: Maven 3.0+ 或 Gradle
- 依赖:
feat-core
<dependency> <groupId>tech.smartboot.feat</groupId> <artifactId>feat-core</artifactId> <version>${feat.version}</version></dependency>dependencies { implementation 'tech.smartboot.feat:feat-core:${feat.version}'}import tech.smartboot.feat.Feat;
public class HttpGetDemo { public static void main(String[] args) { Feat.httpClient("https://smartboot.tech") .get("/feat/") .onSuccess(response -> System.out.println(response.body())) .onFailure(Throwable::printStackTrace) .submit(); }}Feat.httpClient(baseUrl) → 创建客户端 ↓ .get(path) → 定义请求 ↓ .onSuccess(callback) → 成功回调 .onFailure(callback) → 失败回调 ↓ .submit() → 执行请求Feat 客户端的设计哲学是简单高效,采用异步回调模型,适合以下场景:
- 高并发服务端 - 异步非阻塞模型,不会阻塞线程
- 微服务间通信 - 轻量级,启动快,内存占用低
- 嵌入式场景 - 不依赖 Spring 等重量级框架
- 需要精细控制 - 支持流式处理、连接池、代理等高级配置
什么时候考虑其他方案
Section titled “什么时候考虑其他方案”- 需要声明式 HTTP 客户端(如 Spring 的
@FeignClient)→ 考虑 OpenFeign - 需要完整的 Reactive Streams 支持(Mono/Flux)→ 考虑 WebFlux 的 WebClient
- 团队已深度使用 Spring 且需要无缝集成 → Spring 生态的客户端集成更好
连接失败:“localhost:8080” 报错
Section titled “连接失败:“localhost:8080” 报错”原因: HttpClient(String url) 需要完整 URL,包含协议头。
解决:
// ❌ 错误new HttpClient("localhost:8080");
// ✅ 正确new HttpClient("http://localhost:8080");如何开启调试日志
Section titled “如何开启调试日志”httpClient.options().debug(true);异步结果如何转为同步
Section titled “异步结果如何转为同步”HttpResponse response = Feat.httpClient("https://api.example.com") .get("/data") .submit() .get(); // 阻塞等待结果