Skip to content

Commit ce8d86f

Browse files
committed
Update README 🍬
1 parent 37973fd commit ce8d86f

File tree

5 files changed

+718
-16
lines changed

5 files changed

+718
-16
lines changed

README.MD

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ Mỗi module đều có hướng dẫn chi tiết đi kèm.
1919
---
2020

2121
## Các bài viết hiện có
22-
22+
- []():
2323
- [basic-dependency-loosely-coupled](https://github.com/loda-kun/spring-boot-learning/tree/master/basic-dependency-loosely-coupled): Hướng dẫn loosely coupled
2424
- [spring-boot-1-helloworld-@Component-@Autowired](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-1-helloworld-%40Component-%40Autowired): 「Spring Boot #1」Hướng dẫn @Component và @Autowired
2525
- [spring-boot-2-helloworld-@Primary - @Qualifier](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-2-helloworld-%40Primary%20-%20%40Qualifier): 「Spring Boot #2@Autowired - @Primary - @Qualifier
2626
- [spring-boot-3-bean-life-cycle-@PostConstruct-@PreDestroy](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-3-bean-life-cycle-%40PostConstruct-%40PreDestroy): 「Spring Boot #3」Spring Bean Life Cycle + @PostConstruct và @PreDestroy
2727
- [spring-boot-4-@Component-@Service-@Repository](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-4-%40Component-%40Service-%40Repository): 「Spring Boot #4@Component vs @Service vs @Repository
2828
- [spring-boot-5-Component-Scan](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-5-Component-Scan): 「Spring Boot #5」Component Scan là gì?
2929
- [spring-boot-6-@configuration-@Bean](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-6-%40configuration-%40Bean): 「Spring Boot #6@Configuration và @Bean
30+
- [spring-boot-7-spring-application-properties-@Value](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-7-spring-application-properties-%40Value): 「Spring Boot #7」Spring Boot Application Config và @Value
31+
- [spring-boot-8-@Controller-web-helloworld](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-8-%40Controller-web-helloworld): 「Spring Boot #8」Tạo Web Helloworld với @Controller
32+
- [spring-boot-9-thymeleaf](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-9-thymeleaf): 「Spring Boot #9」Hướng dẫn chi tiết làm Web với Thymeleaf + Demo Full
33+
- [spring-boot-@Lazy-Anotation](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-%40Lazy-Anotation): 「Spring Boot」Annotation @Lazy trong Spring Boot
34+
- [spring-boot-webflux](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-boot-webflux): Xây dựng ứng dụng Reactive với Spring 5 Webflux
35+
- [spring-cloud-config-server](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-cloud-config-server) + [spring-cloud-config-client](https://github.com/loda-kun/spring-boot-learning/tree/master/spring-cloud-config-client): Hướng dẫn cấu hình nhiều properties bằng Spring Cloud Config Server
3036
- [jpa-hibernate-one-to-one](https://github.com/loda-kun/spring-boot-learning/tree/master/jpa-hibernate-one-to-one): Hướng dẫn sử dụng @OneToOne
3137
- [jpa-hibernate-one-to-many](https://github.com/loda-kun/spring-boot-learning/tree/master/jpa-hibernate-one-to-many): Hướng dẫn sử dụng @OneToMany@ManyToOne
3238
- [jpa-hibernate-many-to-many](https://github.com/loda-kun/spring-boot-learning/tree/master/jpa-hibernate-many-to-many): Hướng dẫn sử dụng @ManyToMany

spring-boot-webflux/readme.md

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# Source
2+
Vào link để xem chi tiết có hình ảnh minh họa:
3+
4+
[Loda.me - Spring Boot - Xây dựng ứng dụng Reactive với Spring 5 Webflux
5+
](https://loda.me/spring-boot-xay-dung-ung-dung-reactive-voi-spring-5-webflux-loda1557549671284)
6+
7+
# Content without images
8+
9+
### Tổng quan
10+
Spring Webflux Framework là một phần của Spring 5 và cung cấp [**Reactive Programming**][reactive-programming] nhằm hỗ trợ cho việc xây dựng ứng dụng web.
11+
Trong hướng dẫn này, chúng tôi sẽ đi một vài khái niệm để hiểu rõ về Spring Webflux, tiếp theo là xây dựng một ứng dụng Reactive Rest APIs đơn giản sử dụng Spring Webflux.
12+
### Reactive Streams API
13+
Đầu tiên chúng ta hiểu cơ chế hoạt động của **Reactive Streams API**.
14+
15+
**Reactive Stream API** được tạo bởi các kỹ sư từ Netflix, Pivotal, Lightbend, RedHat, Twitter, and Oracle và bây giờ là một phần của Java 9. Nó định nghĩa 4 interface:
16+
17+
`Publisher:` Phát ra một chuỗi các sự kiện đến `subscriber` theo yêu cầu của người mà `subscriber` đến nó. Một `Publisher` có thể phục vụ nhiều `subscriber`. Interface này chỉ có một phương thức:
18+
19+
_Publisher.java_
20+
```java
21+
public interface Publisher<T>
22+
{
23+
public void subscribe(Subscriber<? super T> s);
24+
}
25+
```
26+
27+
`Subscriber:` Nhận và xử lý sự kiện được phát ra bởi `Publisher`. Chú ý rằng không có gì xảy ra cho tới khi Subscription – nó được gọi là báo hiệu yêu cầu cho `Publisher`.
28+
29+
_Subscriber.java_
30+
```java
31+
public interface Subscriber<T>
32+
{
33+
public void onSubscribe(Subscription s);
34+
public void onNext(T t);
35+
public void onError(Throwable t);
36+
public void onComplete();
37+
}
38+
```
39+
40+
`Subscription:` Định nghĩa mỗi quan hệ 1-1 giữa `Publisher``Subscriber`. Nó chỉ có thể được sử dụng bởi một `Subsriber` duy nhất và được sử dụng để báo hiệu yêu cầu (request) hoặc hủy (cancel) data.
41+
42+
_Subscription.java_
43+
```java
44+
public interface Subscription<T>
45+
{
46+
public void request(long n);
47+
public void cancel();
48+
}
49+
```
50+
51+
`Processor:` Đại diện cho giai đoạn xử lý gồm cả `Publisher``Subscriber` đồng thời tuân thủ nguyên tắc của cả 2.
52+
53+
_Processor.java_
54+
```java
55+
public interface Processor<T, R> extends Subscriber<T>, Publisher<R>
56+
{
57+
}
58+
```
59+
60+
Bản chất, một `Subscriber` tạo một ` Subscription` tới `Publisher`, sau đó `Publisher` gửi một sự kiện cho `Subsriber` với một luồng các phần tử.
61+
62+
![Subscriber-Publisher-Subscription](../../images/loda1557549671284/2.jpg)
63+
64+
### Spring WebFlux
65+
**Spring Webflux** là một phiên bản song song với **Spring MVC** và hỗ trợ non-blocking reactive streams. Nó hỗ trợ khái niệm **back pressure** và sử dụng Server Netty để run hệ thống reactive. Nếu bạn đã quen thuộc với style **Spring MVC** thì bạn cũng dễ dàng làm việc với **Spring Webflux**.
66+
67+
Spring webflux sử dụng project reactor ( Thư viện implements phỗ biến nhất) như một thư viện reactive, vì thế nó cung cấp 2 kiểu `Publisher`:
68+
69+
`Mono`: Phát ra 0 hoặc 1 phần tử.
70+
71+
`Flux` : Phát ra 0..N phần tử.
72+
73+
### Dependencies
74+
Dươi đây là dependency của Webflux, nó đã kéo theo các dependencies khác gồm:
75+
76+
1. spring-boot và spring boot-starter
77+
2. spring-webflux framework
78+
3. reactor-core
79+
80+
```
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter-webflux</artifactId>
84+
<version>2.0.5.RELEASE</version>
85+
</dependency>
86+
```
87+
88+
### Demo String Boot WebFlux
89+
Chúng tôi sẽ xây dựng một ứng dụng đơn giản sử dụng Spring Webflux.
90+
91+
- Để đơn giản chúng tôi tạo một đối tượng đơn giản là `Employee` với 2 thuộc tính đơn giản
92+
- Tạo một Rest APIS cho việc query danh sách `Employees` sử dụng @RestController.
93+
- Cuối cùng là tạo 1 DB đơn giản và hỗ trợ Reactive bằng việc trả về kiểu `Flux` mà Webflux cung cấp. Các bạn có thể sử dụng DB lữu trữ khác hỗ trợ Reative như là **MongoDB**.
94+
95+
_pom.xml_
96+
```
97+
<dependencies>
98+
<dependency>
99+
<groupId>org.springframework.boot</groupId>
100+
<artifactId>spring-boot-starter-webflux</artifactId>
101+
<version>2.0.5.RELEASE</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.springframework.boot</groupId>
105+
<artifactId>spring-boot-starter-test</artifactId>
106+
<version>2.0.5.RELEASE</version>
107+
<scope>test</scope>
108+
</dependency>
109+
</dependencies>
110+
```
111+
112+
_EmployeeApplication.class_
113+
```java
114+
import org.springframework.boot.SpringApplication;
115+
import org.springframework.boot.autoconfigure.SpringBootApplication;
116+
import org.springframework.context.annotation.ComponentScan;
117+
118+
@SpringBootApplication
119+
public class EmployeeApplication {
120+
public static void main(String[] args) {
121+
SpringApplication.run(EmployeeApplication.class, args);
122+
}
123+
}
124+
```
125+
_Employee.class_
126+
```java
127+
public class Employee {
128+
private String id;
129+
private String name;
130+
131+
public Employee(String id, String name) {
132+
this.id = id;
133+
this.name = name;
134+
}
135+
136+
public String getId() {
137+
return id;
138+
}
139+
140+
public void setId(String id) {
141+
this.id = id;
142+
}
143+
144+
public String getName() {
145+
return name;
146+
}
147+
148+
public void setName(String name) {
149+
this.name = name;
150+
}
151+
}
152+
```
153+
_EmployeeService.class_
154+
```java
155+
import reactor.core.publisher.Flux;
156+
import reactor.core.publisher.Mono;
157+
158+
public interface EmployeeService {
159+
Flux<Employee> findAll();
160+
}
161+
162+
```
163+
_EmployeeServiceImpl.class_
164+
```java
165+
import org.springframework.stereotype.Service;
166+
import reactor.core.publisher.Flux;
167+
168+
@Service
169+
public class EmployeeServiceImpl implements EmployeeService {
170+
171+
@Override
172+
public Flux<Employee> findAll() {
173+
return Flux.just(new Employee("29750","AtomPtit") , new Employee("18273", "HungCD"));
174+
}
175+
}
176+
```
177+
_EmployeeController.class_
178+
```java
179+
import org.springframework.beans.factory.annotation.Autowired;
180+
import org.springframework.web.bind.annotation.GetMapping;
181+
import org.springframework.web.bind.annotation.RestController;
182+
import reactor.core.publisher.Flux;
183+
184+
@RestController
185+
public class EmployeeController {
186+
@Autowired
187+
private EmployeeService employeeService;
188+
189+
@GetMapping("/employees")
190+
private Flux<Employee> getAllEmpployees() {
191+
return employeeService.findAll();
192+
}
193+
}
194+
195+
```
196+
197+
OUTPUT:
198+
```java
199+
[
200+
{
201+
"id": "29750",
202+
"name": "AtomPtit"
203+
},
204+
{
205+
"id": "18273",
206+
"name": "HungCD"
207+
}
208+
]
209+
```
210+
211+
### Kết luận
212+
Cả `Spring MVC` và Spring `Webflux` để hỗ trợ kiến trúc Client-Server nhưng điểm khác nhau chính là mô hình `concurrency` và hành động mặc định trong tính chất non-blocking và threads.
213+
214+
Trong Spring MVC, nó mặc định rằng ứng dụng có thể bị block tại thread hiện tại, trong khi webflux thì mặc định threads là non-blocking .
215+
216+
Reactive và non-blocking nhìn chung thì không làm cho ứng dụng chạy nhanh hơn. Lợi ích mà nó được kỳ vọng là mở rộng ứng dụng với số luồng nhỏ và yêu cầu ít bộ nhớ hơn. Nó làm cho các ứng dụng trở nên linh hoạt hơn khi tải.
217+
218+
Cuối cùng, source code hoàn chỉnh được sử dụng trong hướng dẫn này có sẵn [trên Github][link-github]
219+
<a class="btn btn-icon btn-github mr-1" target="_blank" href="https://github.com/loda-kun/spring-boot-learning">
220+
<i class="fab fa-github"></i>
221+
</a>
222+
[reactive-programming]: https://loda.me/gioi-thieu-reactive-programming-voi-reactor-loda1556032486705
223+
[link-github]: https://github.com/loda-kun/spring-boot-learning

0 commit comments

Comments
 (0)