1、smart-http 簡介
smart-http 是一款可編程式 HTTP 應用微內核。它是目前市面上為數不多即能嚴格準守 RFC2616 規范,又兼顧卓越性能的 Http 服務器。
smart-http 建立在自研的通信框架 smart-socket 之上,使其有著純正的國產化血統。該項目對標的是 nginx,高性能、輕量化是其追求和堅持的目標。
模塊介紹
模塊 | 說明 | 代碼量 |
---|---|---|
smart-http-common | 基礎通用模塊,包括枚舉、日志、工具類 | 2300+ |
smart-http-client | HTTP Client 編解碼和響應式編程設計 | 1400+ |
smart-http-server | HTTP Server 和 WebSocket 的編解碼和應用層接口設計 | 2800+ |
smart-http-restful | 基于 smart-http-server 的簡易版 MVC 模塊(實驗性) | 680 |
smart-http-restful-mybatis | 類似 mybatis-spring(實驗性) | 25 |
smart-http-test | 單測模塊 | 1600+ |
2、 版本更新
這是一個特別的版本,還是一個新的起點。
smart-http 一直以來都以「極簡、易用、高性能」為設計理念,但凡跟這三點不相干的內容,我們從來不會去 care。
但是,隨著近期觀察到我部署在互聯網上的服務頻繁受到騷擾,讓我意識到「安全」,也是 smart-http 應該去關注的方向。
以下是檢測到來自世界各地的非正常訪問:
-
來自美國的流量,訪問不存在資源。
-
來自美國的流量,非正常 HTTP 報文
-
來自越南的流量。
-
來自荷蘭的流量
除了非法請求,還有一些會長期占用 TCP 連接資源的情況。目前我的站點提供的只是一些簡單的服務,此類流量還沒有造成負面影響。但對于這種不禮貌的行徑,我覺得作為一款專業的 http 服務,還是能夠做一些事情的。
所以,我們決定在 smart-http 搭建一套 waf 的能力。主動防御一切非法請求,并給予以下反饋:
來自東方的神秘力量正在守護這片區域
這個版本主要搭建了 waf 的基礎骨架,并實現了對于 Method、URI 的檢測,后續我們再不斷完善 waf 的檢測規則和覆蓋范圍。
Maven
<dependency>
<groupId>org.smartboot.http</groupId>
<artifactId>smart-http-server</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>org.smartboot.http</groupId>
<artifactId>smart-http-client</artifactId>
<version>1.3.4</version>
</dependency>
本次更新內容:
-
restful 模塊新增 commons-fileupload 的適配,提供文件上傳的處理能力。
-
新增 waf 模塊,提升 HTTP 服務安全性。
3、快速上手
3.1 HTTP 服務端
public class SimpleSmartHttp {
public static void main(String[] args) {
HttpBootstrap bootstrap = new HttpBootstrap();
bootstrap.httpHandler(new HttpServerHandler() {
@Override
public void handle(HttpRequest request, HttpResponse response) throws IOException {
response.write("hello smart-http<br/>".getBytes());
}
}).setPort(8080).start();
}
}
3.2 WebSocket 服務端
public class WebSocketDemo {
public static void main(String[] args) {
//1. 實例化路由Handle
WebSocketRouteHandler routeHandle = new WebSocketRouteHandler();
//2. 指定路由規則以及請求的處理實現
routeHandle.route("/", new WebSocketDefaultHandler() {
@Override
public void handleTextMessage(WebSocketRequest request, WebSocketResponse response, String data) {
response.sendTextMessage("接受到客戶端消息:" + data);
}
});
// 3. 啟動服務
HttpBootstrap bootstrap = new HttpBootstrap();
bootstrap.webSocketHandler(routeHandle);
bootstrap.start();
}
}
3.3 Http 客戶端
public class HttpGetDemo {
public static void main(String[] args) {
HttpClient httpClient = new HttpClient("www.baidu.com", 80);
httpClient.get("/").header().keepalive(false).done()
.onSuccess(response -> System.out.println(response.body()))
.onFailure(Throwable::printStackTrace)
.done();
}
}
3.4 Restful
<dependency>
<groupId>org.smartboot.http</groupId>
<artifactId>smart-http-restful</artifactId>
<version>${smarthttp.version}</version>
</dependency>
@Controller
public class RestfulDemo {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String helloworld() {
return "hello world";
}
public static void main(String[] args) throws Exception {
RestfulBootstrap bootstrap = RestfulBootstrap.getInstance().controller(RestfulDemo.class);
bootstrap.bootstrap().setPort(8080).start();
}
}
smartboot 開源組織,一個容易被誤認為是在 “重復造輪子” 的低調組織。曾獲得 2020 年度 OSC 中國開源項目「優秀 Gitee 組織 」榮譽。
該組織內的明星項目包括:
smart-socket
歷時 5 年精煉出 2 千多行代碼,輕松實現百萬級長連接的 AIO 通信框架。smart-http
基于 smart-socket 實現的 HTTP/1.1 web 服務。smart-servlet
基于 smart-http 實現的 Servlet 3.1 容器服務。smart-mqtt
基于 smart-socket 實現的 MQTT 3.1.1/5.0 Broker&Client 服務。smart-flow
一款具備可觀測性的輕量級業務編排框架。組織地址:https://smartboot.tech/
代碼倉庫:https://gitee.com/smartboot