1、smart-http 简介
smart-http 是一款基于国产 AIO 通信框架 smart-socket
开发的 HTTP 服务器。
除了极致轻量化和卓越的性能表现,smart-http 还拥有完整的 Web 服务能力:
-
HTTP Server -
WebSocket Server -
HTTP Client -
WebSocket Client -
轻量级 MVC
2、 版本更新
本次更新内容:
-
修复 vert.x websocket client 无法连接的问题(ISSUE:I93SX9) -
优化日志模块,支持占位符 {}
。
Maven
<dependency>
<groupId>org.smartboot.http</groupId>
<artifactId>smart-http-server</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.smartboot.http</groupId>
<artifactId>smart-http-client</artifactId>
<version>1.4.0</version>
</dependency>
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 WebSocket 客户端
public class HttpGetDemo {
public static void main(String[] args) throws IOException {
WebSocketClient client = new WebSocketClient("ws://localhost:8080");
client.connect(new WebSocketListener() {
@Override
public void onOpen(WebSocketClient client, WebSocketResponse session) {
try {
client.sendMessage("hello smart-http");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void onMessage(WebSocketClient client, String message) {
System.out.println(message);
}
});
}
}
3.5 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 4.0 容器服务。smart-mqtt
基于 smart-socket 实现的 MQTT 3.1.1/5.0 Broker&Client 服务。smart-flow
一款具备可观测性的轻量级业务编排框架。组织地址:https://smartboot.tech/
代码仓库:https://gitee.com/smartboot
原文始发于微信公众号(三刀):smart-http v1.4.0发布,日常更新
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/231863.html