Reactive programming refers to a data-driven, asynchronous, and concurrent programming paradigm. In short, asynchronous data flow programming. The data flow is created, combined, filtered, transformed, and so on, and finally the required processing and results are obtained. It provides a high-performance, event-driven, more elegant asynchronous programming experience that takes full advantage of computing resources, while also providing a backpressure mechanism to prevent system overload. The typical frameworks are RxJava, Reactor and so on.
For a long time, Java responsiveness was only able to interact with non-relational databases such as MongoDB and Redis. Most of our data is still in relational databases, and most of the time Java uses JDBC to operate on relational databases, which is blocked and synchronized. Therefore, there is an urgent need for a responsive database – driven protocol. There are two responsive database driven protocols on the market: ADBA and R2DBC
R2BDC was born because Spring officially needed a database interaction API that could satisfy asynchronous responses after Spring 5 released the responsive Web framework Spring WebFlux. In the absence of standards and drivers, the Pivotal team began to look at Reactive Relational Database Connectivity, The R2DBC specification API is presented to evaluate the feasibility and to discuss whether database vendors are interested in supporting reactive asynchronous non-blocking drivers. It started with PostgreSQL, H2, and MSSQL, and now MySQL has joined. The latest version of R2DBC is 0.8.1.RELEASE. The R2DBC connection pool and R2DBC proxy are provided in addition to the driver implementation. Cloud native applications are also supported.
The r2DBC based MySQL driver is officially supported after the RELEASE of Spring Boot 2.3.0
It is also very easy to create a Spring WebFlux to operate MySQL using R2DBC, provided you have a preliminary understanding of WebFlux
Import dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
</dependency>
Copy the code
The configuration file configures the connection address
spring:
r2dbc:
password: admin
url: r2dbcs:mysql://localhost:3306/test_db
username: root
Copy the code
Code – Create database relational mapping do
@Data
@Table("mall_ad")
public class AdBean {
@Id
private Integer id;
private String name;
private String link;
private String url;
private String content;
private LocalDateTime startTime;
private LocalDateTime endTime;
private Byte deleted;
}
Copy the code
Code – Establish vo
@Data
@Builder
public class AdVo {
private String name;
private String link;
private String url;
private String content;
}
Copy the code
Code – Create database operation objects
public interface AdRepository extends ReactiveCrudRepository<AdBean.Integer> {}Copy the code
Code – Create service
@Service public class AdServiceImpl implements AdService { @Autowired private AdRepository adRepository; /** * Query current ads ** @return
*/
@Override
public Flux<AdVo> findAll() {// This requires some understanding of stream operationsreturnadRepository.findAll().map( adBean -> AdVo.builder() .name(adBean.getName()).link(adBean.getLink()) .url(adBean.getUrl()).content(adBean.getContent()) .build() ); }}Copy the code
Code – Create controller
@RestController
@RequestMapping("/api/ad")
public class AdController {
@Autowired
private AdService adService;
@GetMapping("/findAll")
public Flux<AdVo> findNowAd(a) {
returnadService.findAll(); }}Copy the code
Start the project
Test the API
/ / 20200720213921 / / http://127.0.0.1:8080/api/ad/findAll/ [{" name ":" cooperation Who is your food, "" link" : ""," url ": "Http://yanxuan.nosdn.127.net/65091eebc48899298171c2eb6696fe27.jpg", "content", "cooperation Who is your dish "}, {" name ": Food festival "activities", "link" : ""," url ":" http://yanxuan.nosdn.127.net/bff2e49136fcef1fd829f5036e07f116.jpg ", "content" : Food festival "activities"}, {" name ":" activity Mother's day "and" link ":" ", "url" : "Http://yanxuan.nosdn.127.net/8e50c65fda145e6dd1bf4fb7ee0fcecc.jpg", "content" : "activity Mother's day 5}]Copy the code
Full project address gitee.com/yichengxian…