This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Restlight Server

Restlight Server

esa.restlight.server.RestliteRestlight架构中的Restlight Server模块的入口类, 在ESA HttpServer 基础上丰富了更多的功能

  • 引入业务线程池
  • 基于CompletableFuture的响应式编程支持
  • 线程调度
  • Filter
  • 请求路由(根据url, method, header等条件将请求路由到对应的Handler)

1 - Quick Start

引入依赖

<dependency>
	<groupId>io.esastack</groupId>
	<artifactId>restlight-server</artifactId>
	<version>${restlight.version}</version>
</dependency>

一行代码启动一个Http Server

Restlite.forServer()
        .daemon(false)
        .deployments()
        .addRoute(route(get("/hello"))
                .handle((request, response) ->
                        response.sendResult("Hello Restlight!".getBytes(StandardCharsets.UTF_8))))
        .server()
        .start();

运行并访问: http://localhost:8080/hello 即可看到输出:

Hello Restlight!

2 - 术语

Mapping

表示一个请求匹配的条件, 用于确定一个请求是否能够路由到某个目标对对象。

eg.

curl -X GET http://localhost:8080/hello

Mapping.get("/hello");

curl -X POST http://localhost:8080/foo?a=1

Mapping.post("/foo")
    .hasParam("a", "1");

curl -X GET -H "a:1" -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:8080/foo?a=1

Mapping.get("/foo")
        .hasParam("a", "1")
        .noneParam("b", "1")
        .hasHeader("a", "1")
        .noneHeader("b", "1")
        .consumes(MediaType.APPLICATION_JSON)
        .produces(MediaType.APPLICATION_JSON);

Route

Route中包含了一个Mapping用于路由匹配, 一个请求都将期望路由到具体的一个Route, 如果找不到任何一个Route则响应一个404, 同时一个Route还负责请求本身的业务处理。

eg.

Mapping mapping = Mapping.get("/foo");
Route route = Route.route(mapping)
        .handle((request, response) -> {
            response.sendResult("Hello Restlight!".getBytes(StandardCharsets.UTF_8));
        })
        .onError(((request, response, error) -> {
            // error occurred
        }))
        .onComplete(((request, response, t) -> {
            // request completed
        }));

3 - 请求处理

业务处理

Mapping mapping = Mapping.get("/foo");
Route route = Route.route(mapping)
        .handle((request, response) -> {
            // biz logic here
            response.sendResult("Hello Restlight!".getBytes(StandardCharsets.UTF_8));
        });

异常处理

Mapping mapping = Mapping.get("/foo");
Route route = Route.route(mapping)
        .handle((request, response) -> {
            // biz logic here
            response.sendResult("Hello Restlight!".getBytes(StandardCharsets.UTF_8));
        })
        .onError(((request, response, error) -> {
            // error occurred
        }));

Complete事件

Mapping mapping = Mapping.get("/foo");
Route route = Route.route(mapping)
        .handle((request, response) -> {
            // biz logic here
            response.sendResult("Hello Restlight!".getBytes(StandardCharsets.UTF_8));
        })
        .onComplete(((request, response, t) -> {
            // request completed
        }));

异步

Route请求处理生命周期均支持基于Completablefuture的异步使用

Route route = route(get("/foo"))
        .handleAsync((request, response) ->
                CompletableFuture.runAsync(() -> {
                    // biz logic
                }))
        .onErrorAsync((request, response, throwable) ->
                CompletableFuture.runAsync(() -> {
                    // error
                }))
        .onCompleteAsync((request, response, t) ->
                CompletableFuture.runAsync(() -> {
                    // complete
                }));