Skip to content

Commit 481587b

Browse files
author
litongjava
committed
add tio-boot-web-flux
1 parent ed636f1 commit 481587b

File tree

2 files changed

+174
-1
lines changed

2 files changed

+174
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
{
147147
"title": "18_spring",
148148
"collapsable": false,
149-
"children": ["18_spring/01.md", "18_spring/02.md"]
149+
"children": ["18_spring/01.md", "18_spring/02.md", "18_spring/03.md"]
150150
},
151151
{
152152
"title": "19_mongodb",

docs/zh/18_spring/03.md

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# tio-boot 和 spring-boot-starter-webflux 整合
2+
3+
本文档提供了如何将 tio-boot-server 集成到使用 spring-boot-starter-webflux 的项目中。默认情况下,Spring WebFlux 使用 Netty 作为网络处理层。本文档将引导你如何将 tio-boot-server 作为替代品整合到你的 Spring WebFlux 应用中。
4+
5+
### pom.xml 配置:
6+
7+
1. **Java 和 Spring Boot 版本:**
8+
- 设定了 Java 1.8 版本。
9+
- 设定了 Spring Boot 的版本为 2.5.6。
10+
2. **依赖项配置:**
11+
12+
- 移除了默认的 `spring-boot-starter-json``spring-boot-starter-reactor-netty` 依赖,因为我们打算使用 tio-boot。
13+
- 添加了 `tio-boot` 作为项目依赖,以及其他相关依赖。
14+
15+
3. **插件配置:**
16+
- 配置了 `spring-boot-maven-plugin` 以支持项目构建和运行。
17+
18+
```
19+
<properties>
20+
<java.version>1.8</java.version>
21+
<maven.compiler.source>${java.version}</maven.compiler.source>
22+
<maven.compiler.target>${java.version}</maven.compiler.target>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<spring-boot.version>2.5.6</spring-boot.version>
26+
<main.class>demo.SpringWebFluxTioBootApp</main.class>
27+
<tio-boot.version>1.3.9</tio-boot.version>
28+
</properties>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-webflux</artifactId>
34+
<exclusions>
35+
<exclusion>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-json</artifactId>
38+
</exclusion>
39+
<exclusion>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-reactor-netty</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.litongjava</groupId>
48+
<artifactId>tio-boot</artifactId>
49+
<version>${tio-boot.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.projectlombok</groupId>
53+
<artifactId>lombok</artifactId>
54+
<scope>provided</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.litongjava</groupId>
58+
<artifactId>hotswap-classloader</artifactId>
59+
<version>1.2.1</version>
60+
</dependency>
61+
62+
<!-- SpringBoot集成Test -->
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
70+
<dependency>
71+
<groupId>junit</groupId>
72+
<artifactId>junit</artifactId>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
77+
<build>
78+
<plugins>
79+
<!-- Spring Boot -->
80+
<plugin>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-maven-plugin</artifactId>
83+
<version>${spring-boot.version}</version>
84+
<configuration>
85+
<includeSystemScope>true</includeSystemScope>
86+
<!--使该插件支持热启动 -->
87+
<fork>true</fork>
88+
<mainClass>${main.class}</mainClass>
89+
</configuration>
90+
</plugin>
91+
</plugins>
92+
</build>
93+
94+
<dependencyManagement>
95+
<dependencies>
96+
<dependency>
97+
<groupId>org.springframework.boot</groupId>
98+
<artifactId>spring-boot-dependencies</artifactId>
99+
<version>${spring-boot.version}</version>
100+
<type>pom</type>
101+
<scope>import</scope>
102+
</dependency>
103+
</dependencies>
104+
</dependencyManagement>
105+
```
106+
107+
### 启动类配置:
108+
109+
接下来是 Spring Boot 应用的启动类 `SpringWebFluxTioBootApp`
110+
111+
1. **注解配置:**
112+
- `@SpringBootApplication` 标识这是一个 Spring Boot 应用。
113+
- `@Import({TioBootServerAutoConfiguration.class})` 导入了 tio-boot 的自动配置,这是整合 tio-boot 的关键。
114+
- `@AComponentScan` 用于 tio-boot 组件扫描。
115+
116+
```
117+
package demo;
118+
import org.springframework.boot.autoconfigure.SpringBootApplication;
119+
import org.springframework.context.annotation.Import;
120+
121+
import com.litongjava.hotswap.wrapper.spring.boot.SpringApplicationWrapper;
122+
import com.litongjava.jfinal.aop.annotation.AComponentScan;
123+
import com.litongjava.tio.boot.spring.SpringBootArgs;
124+
import com.litongjava.tio.boot.spring.TioBootServerAutoConfiguration;
125+
126+
@SpringBootApplication
127+
@Import({TioBootServerAutoConfiguration.class})
128+
@AComponentScan
129+
public class SpringWebFluxTioBootApp {
130+
131+
public static void main(String[] args) {
132+
long start = System.currentTimeMillis();
133+
SpringBootArgs.set(SpringWebFluxTioBootApp.class, args);
134+
SpringApplicationWrapper.run(SpringWebFluxTioBootApp.class, args);
135+
long end = System.currentTimeMillis();
136+
System.out.println(end - start + "(ms)");
137+
138+
}
139+
}
140+
```
141+
142+
### Controller 编写:
143+
144+
定义了一个基本的 `HelloWordWebFluxController` 控制器,用于处理根路径(`"/"`)上的请求。
145+
146+
1. **@RequestPath 注解:**
147+
148+
- 类级别的 `@RequestPath("/")` 指定这个控制器处理的基本路径。
149+
- 方法级别的 `@RequestPath`(没有指定路径)默认处理根路径上的请求。
150+
151+
2. **业务方法:**
152+
- `index` 方法简单地返回字符串 "index"。
153+
154+
```
155+
package demo.controller;
156+
157+
import com.litongjava.tio.http.server.annotation.RequestPath;
158+
159+
@RequestPath("/")
160+
public class HelloWordWebFluxController {
161+
162+
@RequestPath
163+
public String index() {
164+
return "index";
165+
}
166+
}
167+
```
168+
169+
### 测试应用:
170+
171+
完成上述配置后,可以启动应用并访问 `http://localhost/` 来测试是否成功集成了 tio-boot-server。
172+
173+
通过这种方式,你可以将 tio-boot-server 与 Spring WebFlux 结合使用,利用 tio-boot 提供的高性能网络处理能力,同时享受 Spring WebFlux 的响应式编程模型和丰富的开发特性。

0 commit comments

Comments
 (0)