smart-http 1.6.1 实例
所属分类 smartboot
浏览量 81
smart-http v1.6.1 发布
https://mp.weixin.qq.com/s/Ov4aPXb9xZs5xcHBp-l1Zw
smart-http 是一款基于国产 AIO 通信框架 smart-socket 开发的 HTTP 服务器。
除了极致轻量化和卓越的性能表现,smart-http 还拥有完整的 Web 服务能力:
HTTP Server
WebSocket Server
HTTP Client
WebSocket Client
轻量级 MVC(实验性)
io.github.smartboot.http:smart-http-server:1.6.1
io.github.smartboot.http:smart-http-client:1.6.1
io.github.smartboot.http:smart-http-restful:1.6.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".getBytes());
}
}).setPort(8070).start();
}
}
WebSocket 服务端
public class WebSocketServerDemo {
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.setPort(8071);
bootstrap.webSocketHandler(routeHandle);
bootstrap.start();
}
}
Http客户端
public class HttpClientDemo {
public static void main(String[] args) {
HttpClient httpClient = new HttpClient("127.0.0.1", 8070);
httpClient.get("/").header().keepalive(false).done()
.onSuccess(response -> System.out.println(response.body()))
.onFailure(Throwable::printStackTrace)
.done();
}
}
public class WebSocketClientDemo {
public static void main(String[] args) throws IOException {
WebSocketClient client = new WebSocketClient("ws://localhost:8071");
client.connect(new WebSocketListener() {
@Override
public void onOpen(WebSocketClient client, WebSocketResponse session) {
try {
client.sendMessage("hello smart-http websocket");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void onMessage(WebSocketClient client, String message) {
System.out.println(message);
}
});
}
}
Restful
@Controller
public class RestfulServerDemo {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return"hello smart restful";
}
public static void main(String[] args) throws Exception {
RestfulBootstrap bootstrap = RestfulBootstrap.getInstance().controller(RestfulDemo.class);
bootstrap.bootstrap().setPort(8072).start();
}
}
项目依赖
mvn dependency:tree -Dverbose
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ smartbootdemo ---
[WARNING] The artifact xml-apis:xml-apis:jar:2.0.2 has been relocated to xml-apis:xml-apis:jar:1.0.b2
[INFO] dyyx:smartbootdemo:jar:1.0.0
[INFO] +- io.github.smartboot.http:smart-http-server:jar:1.6.1:compile
[INFO] | \- io.github.smartboot.http:smart-http-common:jar:1.6.1:compile
[INFO] | \- io.github.smartboot.socket:aio-pro:jar:1.5.54:compile
[INFO] | +- io.github.smartboot.socket:aio-core:jar:1.5.54:compile
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.36:compile
[INFO] +- io.github.smartboot.http:smart-http-client:jar:1.6.1:compile
[INFO] | \- (io.github.smartboot.http:smart-http-common:jar:1.6.1:compile - omitted for duplicate)
[INFO] \- io.github.smartboot.http:smart-http-restful:jar:1.6.1:compile
[INFO] +- (io.github.smartboot.http:smart-http-server:jar:1.6.1:compile - omitted for duplicate)
[INFO] \- com.alibaba.fastjson2:fastjson2:jar:2.0.8:compile
[INFO] ------------------------------------------------------------------------
io.github.smartboot.http
smart-http-server
smart-http-common
aio-pro
aio-core
slf4j-api
smart-http-client
mart-http-server
fastjson2
http://127.0.0.1:8070
hello smart-http
HttpClientDemo
hello smart-http
websocket
http://127.0.0.1:8071
Hello smart-http
org.smartboot.http.server.HttpServerConfiguration.httpServerHandler
private HttpServerHandler httpServerHandler = new HttpServerHandler() {
@Override
public void handle(HttpRequest request, HttpResponse response) throws IOException {
response.write("Hello smart-http".getBytes(StandardCharsets.UTF_8));
}
};
WebSocketClientDemo
ws://localhost:8071
接受到客户端消息:hello smart-http websocket
restful
http://127.0.0.1:8072/
hello smart-http-rest
org.smartboot.http.restful.RestfulBootstrap
private static final HttpServerHandler DEFAULT_HANDLER = new HttpServerHandler() {
private final byte[] BYTES = "hello smart-http-rest".getBytes();
@Override
public void handle(HttpRequest request, HttpResponse response) throws IOException {
response.getOutputStream().write(BYTES);
}
};
http://127.0.0.1:8072/hello
hello smart restful
上一篇
下一篇
java AIO 使用注意点
JAVA AIO 例子 客户端发送与服务端接收消息
java AIO echo server
java NIO ByteBuffer 读写整数
java NIO ByteBuffer 使用技巧
smartsocket 整数读写实例