关于后端:smarthttp-v1120版本发布

10次阅读

共计 2648 个字符,预计需要花费 7 分钟才能阅读完成。

1、smart-http 简介

smart-http 是一款 可编程式 HTTP 利用微内核。它是目前市面上为数不多的做到严格准守 RFC2616 标准,又兼顾卓越性能的 Http 服务器。

smart-http 齐全建设在自研的通信框架 smart-socket 之上,放弃了其纯正的国产化血统。我的项目定位对标的是 nginx,高性能、轻量化是其谋求和保持的指标。

严格意义上 smart-http 算不上是一个框架,它更像一个工具类库。开发人员能够基于它封装出符合特定场景的利用框架或解决方案。

模块介绍

模块 阐明 代码量
smart-http-common 根底通用模块,包含枚举、日志、工具类 2300+
smart-http-client HTTP Client 编解码和响应式编程设计 1200+
smart-http-server HTTP Server 和 WebSocket 的编解码和应用层接口设计 2600+
smart-http-restful 基于 smart-http-server 的简易版 MVC 模块(实验性 380+
smart-http-test 单测模块 1200+

2、版本更新

Maven

<dependency>
    <groupId>org.smartboot.http</groupId>
    <artifactId>smart-http-server</artifactId>
    <version>1.1.20</version>
</dependency>

本次更新内容

  1. smart-socket 降级至 1.6.1
  2. HttpRouteHandler 反对自定义默认处理器。
  3. restful 模块新个性:

    • 反对 classpath 门路下的前端动态资源文件加载。
    • 新增 inspect 个性用于 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.connect();
        httpClient.get("/")
                .onSuccess(response -> System.out.println(response.body()))
                .onFailure(Throwable::printStackTrace)
                .send();}
}

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-broker
    基于 smart-socket 实现的 MQTT 3.1.1/5.0 Broker 服务。
  • smart-flow
    一款具备可观测性的轻量级业务编排框架。

组织地址:https://smartboot.tech/
代码仓库:https://gitee.com/smartboot


关注「三刀」公众号

开源不易,且行且珍惜。

本文由 mdnice 多平台公布

正文完
 0