Skip to content

Commit f0fe198

Browse files
committed
fixes typo
1 parent 14a5895 commit f0fe198

File tree

11 files changed

+212
-0
lines changed

11 files changed

+212
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

boot.md

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
# Build a Reactive application with Spring Boot 2.0
2+
3+
[TOC]
4+
5+
Nowadays, reactive programming becomes more and more popular. Spring 5 and Spring Boot 2.0 also bring built-in reactive programming support.
6+
7+
According to the definition in [Wikipedia](https://en.wikipedia.org/wiki/Reactive_programming).
8+
9+
>Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change. This means that it becomes possible to express static (e.g. arrays) or dynamic (e.g. event emitters) data streams with ease via the employed programming language(s)
10+
11+
[Reactor](http://projectreactor.io) is a [Reactive Streams](http://www.reactive-streams.org/) implementation, and provide fully non-blocking reactive programming model with back-pressure capability.
12+
13+
In this post, we will create a simple Spring Boot application with the newest Reactive APIs provided in `spring-webflux`, and use Reactor-Netty as runtime instead of traditional Servlet based container.
14+
15+
## Prerequisites
16+
17+
Make sure you have already installed the following software.
18+
19+
* [Oracle Java 8](https://java.oracle.com)
20+
* [Apache Maven](https://maven.apache.org)
21+
* [Gradle](http://www.gradle.org)
22+
* Your favorite IDE, including :
23+
* [NetBeans IDE](http://www.netbeans.org)
24+
* [Eclipse IDE](http://www.eclipse.org) (or Eclipse based IDE, Spring ToolSuite is highly recommended)
25+
* [Intellij IDEA](http://www.jetbrains.com)
26+
27+
## Generate project skeleton
28+
29+
To kick start a Spring Boot application quickly, the simplest approach is utilizing [Spring Initializer](http://start.spring.io) to generate the project template.
30+
31+
Open your favoriate browser, and go to [Spring Initializer](http://start.spring.io) page.
32+
33+
![Spring Boot initializer](https://github.com/hantsy/gs-boot-reactive/blob/master/start.png)
34+
35+
Fill the form field as you need.
36+
* You can select Maven or Gradle as build tools.
37+
* And select Kotlin, Groovy, Java 8 as programming language.
38+
* Then choose the Spring Boot version, be care about this field, only Spring Boot 2.0 support Reactive features, you can select the latest 2.0 milestone 4 or snapshot.
39+
* In the group and artifact, input your desired values. Here we use the default value for demonstration purpose.
40+
* In the **Selected Dependencies** field, type **reactive** in the input box, in the autocomplete dropdown menus, choose **Reactive Web**, you can also add other features as you need.
41+
42+
Then hint **Generate Project** button or use **ALT+ENTER** keyboard shortcuts to get the generated codes. It will popup a download dialog for you, save it into your local disk.
43+
44+
## Import codes into IDE
45+
46+
Extract the download files, let's try to import the codes into the popular IDEs, you can use your favorite one, including NetBeans IDE, Eclipse, Intellij IDEA etc, all of them have excellent Maven support.
47+
48+
### NetBeans IDE
49+
50+
NetBeans IDE can recognize Maven project automatically.
51+
52+
1. Start up NetBeans IDE.
53+
2. Click **Open project** icon from toolbar or choose **Open Project** from *File* menu.
54+
3. In the **Open Project** dialog, find and select source codes folder, it should be marked as Mavenzied NetBeans project.
55+
4. When it is selected, click the **Open Project** button.
56+
57+
### Eclipse
58+
59+
Eclipse Java EE bundle or Spring ToolSuite is highly recommended for building a Spring application.
60+
61+
1. Starts up Eclipse.
62+
2. Select **Import...** from *File* menu.
63+
3. In the **Import...** dialog, select *Maven/Existing Maven Project*, click **Next** button.
64+
4. In the **Import Maven Projects...** dialog, click *Browse* button to choose the location of the source codes as **Root Directory**.
65+
5. Select the projects to be imported in the **Projects** list, then click **Finish** button.
66+
67+
### Intellij IDEA
68+
69+
For Java developers, IntelliJ IDEA is the most productive IDEs, it includes a free and open source community version and a commercial version, both have good Java and Maven support.
70+
71+
1. Starts up IDEA.
72+
2. Select *New* / *Project from Existing Sources...* from *File* menu.
73+
3. Choose the source folder in the **Select the File or Directory to import** dialog.
74+
4. Then follow the steps in the wizard to import the codes.
75+
76+
## Project structure
77+
78+
Let's have a look at the project structure.
79+
80+
```
81+
| .gitignore
82+
| mvnw
83+
| mvnw.cmd
84+
| pom.xml
85+
|
86+
+---.mvn
87+
| \---wrapper
88+
| maven-wrapper.jar
89+
| maven-wrapper.properties
90+
|
91+
\---src
92+
+---main
93+
| +---java
94+
| | \---com
95+
| | \---example
96+
| | \---demo
97+
| | DemoApplication.java
98+
| |
99+
| \---resources
100+
| application.properties
101+
|
102+
\---test
103+
\---java
104+
\---com
105+
\---example
106+
\---demo
107+
DemoApplicationTests.java
108+
```
109+
110+
It is a standard Maven project, but includes optional Maven Wrapper files.
111+
112+
`src/main/java/com/example/demo/DemoApplication.java` is the boostrap class of this application.
113+
114+
`src/main/resources/application.properties` is the application configuration file. Spring Boot also support `YAML` format.
115+
116+
`src/test/java/com/example/demo/DemoApplicationTests.java` is a JUnit test class for this application.
117+
118+
Open the `pom.xml` file.
119+
120+
There is `spring-boot-stater-webflux`, like the traditional `spring-boot-stater-web`, the new starter will add `spring-webflux` support in this project, and enable `webflux` configuration for this application .
121+
122+
```xml
123+
<dependency>
124+
<groupId>org.springframework.boot</groupId>
125+
<artifactId>spring-boot-starter-webflux</artifactId>
126+
</dependency>
127+
```
128+
By default, it will use `reactor-netty` as runtime.
129+
130+
## Create a Reactive Controller
131+
132+
Similar with traditional `Controller`, `spring-webfux` also support the same annotation when you declare controllers, but it can use the new reactive APIs.
133+
134+
```java
135+
@RestController
136+
@RequestMapping
137+
class MessageController {
138+
139+
@GetMapping
140+
Flux<Message> allMessages(){
141+
return Flux.just(
142+
Message.builder().body("hello Spring 5").build(),
143+
Message.builder().body("hello Spring Boot 2").build()
144+
);
145+
}
146+
}
147+
148+
@Data
149+
@Builder
150+
@AllArgsConstructor
151+
@NoArgsConstructor
152+
class Message {
153+
154+
String body;
155+
}
156+
```
157+
158+
We just created a very simple Controller which includes a simple method to return all messages. Note here we return `Flux<Message>` instead of `List<Message>`.
159+
160+
Run the application via IDE run button, or execute `mvn spring-boot:run` in your command line tool.
161+
162+
```
163+
. ____ _ __ _ _
164+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
165+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
166+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
167+
' |____| .__|_| |_|_| |_\__, | / / / /
168+
=========|_|==============|___/=/_/_/_/
169+
:: Spring Boot :: (v2.0.0.M4)
170+
171+
...
172+
2017-10-01 21:30:13.856 INFO 14460 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080
173+
2017-10-01 21:30:13.857 INFO 14460 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
174+
2017-10-01 21:30:13.889 INFO 14460 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 7.001 seconds (JVM running for 28.193)
175+
```
176+
You can use `curl` to taste the APIs.
177+
178+
```
179+
# curl http://localhost:8080
180+
[{"body":"hello Spring 5"},{"body":"hello Spring Boot 2"}]
181+
```
182+
183+
It works well.
184+
185+
## RouterFunction
186+
187+
Another features provided in `spring-webflux` is its functional `RouterFunction` instead of traditional Controller. For those developers who prefer Lambda, this is a better option.
188+
189+
The above `MessageController` can be replaced with the following `RouterFunction` bean:
190+
191+
```java
192+
@Bean
193+
public RouterFunction<ServerResponse> routes() {
194+
return route(GET("/"),
195+
(req)-> ok()
196+
.body(
197+
BodyInserters.fromObject(
198+
Arrays.asList(Message.builder().body("hello Spring 5").build(),
199+
Message.builder().body("hello Spring Boot 2").build()
200+
)
201+
)
202+
)
203+
);
204+
}
205+
```
206+
207+
These codes are equivalent with the former controller in functionality.
208+
209+
## Source codes
210+
211+
Check out the [source codes](https://github.com/hantsy/gs-boot-reactive) from my github account.
212+

start.png

62.9 KB
Loading

0 commit comments

Comments
 (0)