Skip to content

Commit 516fc8e

Browse files
author
litongjava
committed
add TioBootServer
1 parent e22b71b commit 516fc8e

File tree

5 files changed

+321
-262
lines changed

5 files changed

+321
-262
lines changed

docs/.vuepress/config/sidebar-zh.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
{
153153
"title": "20_http-server",
154154
"collapsable": false,
155-
"children": ["20_http-server/01.md", "20_http-server/02.md"]
155+
"children": ["20_http-server/01.md", "20_http-server/02.md", "20_http-server/03.md"]
156156
},
157157
{
158158
"title": "25_性能测试",

docs/zh/06_内置组件/01.md

+38-48
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,59 @@
1-
# 路由
1+
# TioBootServer 文档
22

3-
### Tio-boot 手动添加路由
3+
## 概述
44

5-
Tio-boot 框架提供了一种简单高效的方式来处理 HTTP 路由。虽然框架默认会自动扫描并添加带有 `@RequestPath` 注解的 Controller 到路由中,但在某些情况下,你可能需要手动添加路由。这可以通过实例化 `SimpleHttpRoutes` 类并向其中添加路由来实现。值得注意的是,
5+
`TioBootServer` 是 Tio 框架的核心启动类,负责初始化和启动 Tio 服务器。它提供了一个结构化的方式来配置和启动你的服务器应用程序。在启动过程中,`TioBootServer` 将大量的类以静态变量的形式存储,使得开发者可以在框架启动前后进行灵活的配置和操作。
66

7-
- 手动添加的路由将拥有比默认 `TioBootHttpRoutes` 更高的优先级。
8-
- 手动添加路由不支持参数封装
7+
## 主要功能
98

10-
#### 步骤 1:创建 Controller 类
9+
- **初始化和启动**: 通过 `init` 方法初始化服务器配置,并通过 `start` 方法启动服务器。
10+
- **动态配置**: 开发者可以在框架启动前后,通过获取和设置 `TioBootServer` 中的静态变量来改变框架的行为。
11+
- **资源清理**: 提供 `stop` 方法来关闭服务器,并执行注册的销毁方法。
1112

12-
首先,创建一个名为 `HelloController` 的类,它包含了两个处理 HTTP 请求的方法。每个方法接收一个 `HttpRequest` 对象,并返回一个 `HttpResponse` 对象。例如:
13+
## 核心方法
1314

14-
```java
15-
package com.litongjava.ai.chat.AController;
15+
### init(ServerTioConfig, WsServerConfig, HttpConfig)
1616

17-
import com.litongjava.tio.http.common.HttpRequest;
18-
import com.litongjava.tio.http.common.HttpResponse;
19-
import com.litongjava.tio.http.server.util.Resps;
17+
初始化 `TioBootServer`。接收 `ServerTioConfig``WsServerConfig``HttpConfig` 作为参数,用于配置服务器的 TCP,WebSocket 和 HTTP 设置。
2018

21-
public class HelloController {
19+
### start(String bindIp, Integer bindPort)
2220

23-
public HttpResponse hello(HttpRequest httpRequest) {
24-
return Resps.txt(httpRequest, "hello");
25-
}
21+
启动服务器。绑定 IP 和端口,并开始接收客户端连接。
2622

27-
public HttpResponse hi(HttpRequest httpRequest) {
28-
return Resps.txt(httpRequest, "hi");
29-
}
30-
}
31-
```
23+
### stop()
3224

33-
在这个例子中,`hello` 方法返回文本 "hello",而 `hi` 方法返回文本 "hi"
25+
关闭服务器。停止接收新的客户端连接,关闭当前连接,并执行注册的销毁方法
3426

35-
#### 步骤 2:定义并配置 HttpRoutes
27+
### isRunning()
3628

37-
接下来,定义一个配置类 `DefineHttpRoutesConfig`。在这个类中,你将实例化 `SimpleHttpRoutes` 并添加自定义路由
29+
检查服务器是否正在运行
3830

39-
使用 `@BeforeStartConfiguration` 注解标记这个配置类,这样框架会在启动前加载它。通过 `@ABean` 注解定义一个 `HttpRoutes` 类型的方法 `httpRoutes`。在这个方法中,首先通过 `Aop.get` 方法获取 `HelloController` 的实例。然后,创建一个 `SimpleHttpRoutes` 实例,并使用 `add` 方法添加路由。
31+
### addDestroyMethod(Runnable runnable)
4032

41-
```java
42-
import com.litongjava.jfinal.aop.Aop;
43-
import com.litongjava.jfinal.aop.annotation.AInitialization;
44-
import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration;
45-
import com.litongjava.tio.boot.server.TioBootServer;
46-
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes;
33+
添加一个在服务器关闭时执行的方法。这允许开发者注册自定义的资源清理逻辑。
4734

48-
@BeforeStartConfiguration
49-
public class DefineHttpRoutesConfig {
35+
## 获取和设置配置
5036

51-
@AInitialization
52-
public void httpRoutes() {
53-
// 创建simpleHttpRoutes
54-
SimpleHttpRoutes simpleHttpRoutes = new SimpleHttpRoutes();
55-
// 创建controller
56-
HelloController helloController = Aop.get(HelloController.class);
37+
- `getTioServer()` / `getWsServerConfig()` / `getHttpConfig()`: 获取当前的服务器,WebSocket,HTTP 配置。
5738

58-
// 添加action
59-
simpleHttpRoutes.add("/hi", helloController::hi);
60-
simpleHttpRoutes.add("/hello", helloController::hello);
39+
- `setServerInteceptorConfigure(ServerInteceptorConfigure)`: 设置服务器拦截器配置。
40+
- `setTioBootHttpRoutes(TioBootHttpRoutes)`: 设置 Tio Boot 的 HTTP 路由。
41+
- `setHttpRoutes(HttpRoutes)`: 设置 HTTP 路由。
42+
- `setServerTcpHandler(ServerTcpHandler)`: 设置 TCP 处理器。
43+
- `setServerAioListener(ServerAioListener)`: 设置服务器监听器。
6144

62-
// 将simpleHttpRoutes添加到TioBootServer
63-
TioBootServer.setHttpRoutes(simpleHttpRoutes);
64-
}
65-
}
45+
- `getServerInteceptorConfigure`
46+
- `getTioBootHttpRoutes`
47+
- `getHttpRoutes`
48+
- `getServerTcpHandler`
49+
- `getServerAioListener`
50+
- `getServerListener`
6651

67-
```
52+
## 常用类解释
6853

69-
在上述配置中,`/hi` 路由映射到 `HelloController``hi` 方法,而 `/hello` 路由映射到 `hello` 方法。
54+
- `ServerInteceptorConfigure` 管理拦截器
55+
- `TioBootHttpRoutes` 管理 tio-boot controller
56+
- `HttpRoutes` 管理 tio-http-server handler
57+
- `ServerTcpHandler` 管理 tcpHnalder
58+
- `ServerAioListener` tcp 请求监听器
59+
- `ServerListener` tio-boot server 监听器

docs/zh/06_内置组件/09.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### 18.4.Tio
1+
# Tio
22

33
`Tio`是一个用于管理网络通信的核心类,特别是在处理客户端和服务器之间的连接、消息发送、连接绑定和关闭等功能。类包含了大量的用于管理网络连接、发送和接收数据包、处理连接状态、以及管理连接的黑名单等操作的方法。这些方法使得开发者可以在客户端和服务器之间有效地进行通信,并对连接进行精细化管理.
44

docs/zh/20_http-server/02.md

+22-212
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
# 在 Android 上使用 tio-boot 运行 HTTP 服务
1+
# tio-boot 添加 http-server handler
22

3-
本文档提供了在 Android 设备上通过`tio-boot`运行 HTTP 服务的详细步骤。`tio-boot`是一个轻量级的 HTTP 服务框架,可用于处理 HTTP 请求。
3+
tio-boot 支持的两种组件最 http 请求进行处理
44

5-
## 步骤 1: 添加依赖项
5+
- http-server handler
6+
- tio-boot controller
67

7-
在你的`build.gradle`文件中添加以下依赖项,以引入必要的库。
8+
1.http-server handler 的优先级高于 tio-boot controller
9+
2.http-server handler 不支持参数封装,只能接受形参 HttpRequest,返回 HttpResponse
10+
3.http-server handler 在性能上优于 tio-boot controller
811

9-
```xml
10-
// 在build.gradle中添加以下依赖项以支持tio-boot和相关功能
11-
implementation 'com.litongjava:android-view-inject:1.0'
12-
implementation 'com.litongjava:litongjava-android-utils:1.0.0'
13-
implementation 'com.litongjava:tio-boot:1.3.7'
14-
```
15-
16-
## 步骤 2: 创建 Controller
12+
#### 步骤 1:创建 Controller 类
1713

18-
创建一个 Controller 类(例如`HelloController`),该类包含处理 HTTP 请求的方法。
14+
首先,创建一个名为 `HelloController` 的类,它包含了两个处理 HTTP 请求的方法。每个方法接收一个 `HttpRequest` 对象,并返回一个 `HttpResponse` 对象。例如:
1915

2016
```java
21-
// 在com.litongjava.android.tio.boot.controller包下创建HelloController类
22-
package com.litongjava.android.tio.boot.controller;
23-
2417
import com.litongjava.tio.http.common.HttpRequest;
2518
import com.litongjava.tio.http.common.HttpResponse;
2619
import com.litongjava.tio.http.server.util.Resps;
@@ -37,31 +30,26 @@ public class HelloController {
3730
}
3831
```
3932

40-
## 步骤 3: 启动服务
33+
在这个例子中,`hello` 方法返回文本 "hello",而 `hi` 方法返回文本 "hi"。
4134

42-
使用`TioBootServerApp`类来配置和启动 HTTP 服务。
35+
#### 步骤 2:定义并配置 HttpRoutes
4336

44-
```java
45-
package com.litongjava.android.tio.boot.controller;
37+
接下来,定义一个配置类 `DefineHttpRoutesConfig`。在这个类中,你将实例化 `SimpleHttpRoutes` 并添加自定义路由。
4638

47-
import com.blankj.utilcode.util.NetworkUtils;
48-
import com.litongjava.android.tio.boot.MainActivity;
39+
使用 `@BeforeStartConfiguration` 注解标记这个配置类,这样框架会在启动前加载它。通过 `@ABean` 注解定义一个 `HttpRoutes` 类型的方法 `httpRoutes`。在这个方法中,首先通过 `Aop.get` 方法获取 `HelloController` 的实例。然后,创建一个 `SimpleHttpRoutes` 实例,并使用 `add` 方法添加路由。
40+
41+
```java
4942
import com.litongjava.jfinal.aop.Aop;
50-
import com.litongjava.jfinal.aop.annotation.AImport;
51-
import com.litongjava.tio.boot.TioApplication;
43+
import com.litongjava.jfinal.aop.annotation.AInitialization;
44+
import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration;
5245
import com.litongjava.tio.boot.server.TioBootServer;
5346
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes;
5447

55-
import org.slf4j.Logger;
56-
import org.slf4j.LoggerFactory;
57-
58-
//不支持
59-
//@AImport(IndexController.class)
60-
public class TioBootServerApp {
61-
private static Logger log = LoggerFactory.getLogger(TioBootServerApp.class);
48+
@BeforeStartConfiguration
49+
public class DefineHttpRoutesConfig {
6250

63-
public static void run() {
64-
long start = System.currentTimeMillis();
51+
@AInitialization
52+
public void httpRoutes() {
6553
// 创建simpleHttpRoutes
6654
SimpleHttpRoutes simpleHttpRoutes = new SimpleHttpRoutes();
6755
// 创建controller
@@ -73,187 +61,9 @@ public class TioBootServerApp {
7361

7462
// 将simpleHttpRoutes添加到TioBootServer
7563
TioBootServer.setHttpRoutes(simpleHttpRoutes);
76-
77-
String ipAddressByWifi = NetworkUtils.getIpAddressByWifi();
78-
log.info("ipAddressByWifi:{}", ipAddressByWifi);
79-
String[] args = new String[]{"--server.port=10051", "--tio.mvc.route.printMapping=true"};
80-
TioApplication.run(TioBootServerApp.class, args);
81-
long end = System.currentTimeMillis();
82-
System.out.println((end - start) + "(ms)");
83-
}
84-
}
85-
```
86-
87-
在这个类中,我们创建了一个`SimpleHttpRoutes`实例来定义路由,并将其添加到`TioBootServer`中。我们还通过 WiFi 获取 IP 地址,并启动了 TioApplication。
88-
89-
## 步骤 4: 配置 AndroidManifest.xml
90-
91-
在你的`AndroidManifest.xml`文件中添加必要的权限。
92-
93-
```xml
94-
<?xml version="1.0" encoding="utf-8"?>
95-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
96-
package="com.litongjava.android.tio.boot">
97-
98-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
99-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
100-
101-
<uses-permission android:name="android.permission.INTERNET" />
102-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
103-
104-
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
105-
106-
107-
<application
108-
android:allowBackup="true"
109-
android:icon="@mipmap/ic_launcher"
110-
android:label="@string/app_name"
111-
android:roundIcon="@mipmap/ic_launcher_round"
112-
android:supportsRtl="true"
113-
android:theme="@style/Theme.Litongjavatiobootforandroidstudy">
114-
<activity android:name=".MainActivity">
115-
<intent-filter>
116-
<action android:name="android.intent.action.MAIN" />
117-
118-
<category android:name="android.intent.category.LAUNCHER" />
119-
</intent-filter>
120-
</activity>
121-
</application>
122-
123-
</manifest>
124-
```
125-
126-
## 步骤 5: 设计 UI 界面
127-
128-
`activity_main.xml`中添加一个按钮,用户可以通过点击该按钮来启动 HTTP 服务。
129-
130-
```xml
131-
<?xml version="1.0" encoding="utf-8"?>
132-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
133-
xmlns:tools="http://schemas.android.com/tools"
134-
android:layout_width="match_parent"
135-
android:layout_height="match_parent"
136-
tools:context=".MainActivity">
137-
138-
<Button
139-
android:id="@+id/startBtn"
140-
android:layout_width="wrap_content"
141-
android:layout_height="wrap_content"
142-
android:background="@color/purple_200"
143-
android:text="启动"></Button>
144-
145-
</LinearLayout>
146-
```
147-
148-
## 步骤 6: MainActivity 获取权限并启动服务
149-
150-
`MainActivity`类中,请求必要的权限并在授权后启动服务。
151-
152-
```java
153-
package com.litongjava.android.tio.boot;
154-
155-
import androidx.appcompat.app.AppCompatActivity;
156-
157-
import android.Manifest;
158-
import android.os.Bundle;
159-
import android.view.View;
160-
161-
import com.blankj.utilcode.util.NetworkUtils;
162-
import com.litongjava.android.tio.boot.controller.HelloController;
163-
import com.litongjava.android.tio.boot.controller.IndexController;
164-
import com.litongjava.android.tio.boot.controller.TioBootServerApp;
165-
import com.litongjava.android.utils.acp.AcpUtils;
166-
import com.litongjava.android.utils.toast.ToastUtils;
167-
import com.litongjava.android.view.inject.annotation.FindViewByIdLayout;
168-
import com.litongjava.android.view.inject.annotation.OnClick;
169-
import com.litongjava.android.view.inject.utils.ViewInjectUtils;
170-
import com.litongjava.jfinal.aop.Aop;
171-
import com.litongjava.tio.boot.TioApplication;
172-
import com.litongjava.tio.boot.http.routes.TioBootHttpRoutes;
173-
import com.litongjava.tio.boot.server.TioBootServer;
174-
import com.litongjava.tio.http.server.handler.HttpRoutes;
175-
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes;
176-
import com.mylhyl.acp.AcpListener;
177-
178-
import org.slf4j.Logger;
179-
import org.slf4j.LoggerFactory;
180-
181-
import java.util.Arrays;
182-
import java.util.List;
183-
184-
@FindViewByIdLayout(R.layout.activity_main)
185-
public class MainActivity extends AppCompatActivity {
186-
private Logger log = LoggerFactory.getLogger(this.getClass());
187-
188-
@Override
189-
protected void onCreate(Bundle savedInstanceState) {
190-
super.onCreate(savedInstanceState);
191-
//setContentView(R.layout.activity_main);
192-
ViewInjectUtils.injectActivity(this, this);
193-
}
194-
195-
@OnClick(R.id.startBtn)
196-
public void startBtnOnClick(View view) {
197-
String[] permissions = {
198-
//写入外部设备权限
199-
Manifest.permission.ACCESS_NETWORK_STATE,
200-
Manifest.permission.ACCESS_WIFI_STATE,
201-
Manifest.permission.INTERNET,
202-
Manifest.permission.READ_EXTERNAL_STORAGE,
203-
Manifest.permission.WRITE_EXTERNAL_STORAGE
204-
205-
};
206-
//创建acpListener
207-
AcpListener acpListener = new AcpListener() {
208-
@Override
209-
public void onGranted() {
210-
TioBootServerApp.run();
211-
}
212-
213-
@Override
214-
public void onDenied(List<String> permissions) {
215-
ToastUtils.defaultToast(getApplicationContext(), permissions.toString() + "权限拒绝,无法写入日志");
216-
}
217-
};
218-
219-
AcpUtils.requestPermissions(this, permissions, acpListener);
220-
22164
}
22265
}
223-
```
224-
225-
在这个类中,我们使用`AcpUtils`来请求权限,并在权限被授予后启动`TioBootServerApp`
226-
启动日志
22766

228-
```xml
229-
2024-01-28 22:58:28.280 [main] INFO TioBootServerApp.run:34 - ipAddressByWifi:10.0.1.64
230-
2024-01-28 22:58:28.347 [main] INFO TioServer.start:167 -
231-
|----------------------------------------------------------------------------------------|
232-
| t-io site | https://www.litongjava.com/t-io |
233-
| t-io on gitee | https://gitee.com/ppnt/t-io |
234-
| t-io on github | https://github.com/litongjava/t-io |
235-
| t-io version | 3.7.3.v20240113-RELEASE |
236-
| ---------------------------------------------------------------------------------------|
237-
| TioConfig name | tio-boot |
238-
| Started at | 2024-01-28 22:58:28 |
239-
| Listen on | 0.0.0.0:10051 |
240-
| Main Class | com.android.internal.os.ZygoteInit |
241-
| Jvm start time | Not available in Android |
242-
| Tio start time | 37ms |
243-
| Pid | Not available in Android |
244-
|----------------------------------------------------------------------------------------|
245-
2024-01-28 22:58:28.351 [main] INFO TioApplicationContext.run:128 - scan class and init:8(ms),server:58(ms),config:1(ms),http reoute:0(ms)
246-
2024-01-28 22:58:28.353 [main] INFO TioApplicationContext.printUrl:146 - port:10051
247-
http://localhost:10051
248-
108(ms)
24967
```
25068

251-
## 测试访问
252-
253-
- http://10.0.1.64:10051/hi
254-
- http://10.0.1.64:10051/hello
255-
256-
## 注意事项
257-
258-
1. `tio-boot``controller`不支持在 Android 系统中运行。
259-
2. `tio-http-server``handler`支持在 Android 系统中运行。
69+
在上述配置中,`/hi` 路由映射到 `HelloController``hi` 方法,而 `/hello` 路由映射到 `hello` 方法。

0 commit comments

Comments
 (0)