共計 3330 個字符,預計需要花費 9 分鐘才能閱讀完成。
本篇文章為大家展示了如何理解反應式數據庫驅動規范 R2DBC,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
1. 簡介
R2DBC 是一種異步的、非阻塞的關系式數據庫連接規范。盡管一些 NoSQL 數據庫供應商為其數據庫提供了反應式數據庫客戶端,但對于大多數項目而言,遷移到 NoSQL 并不是一個理想的選擇。這促使了一個通用的響應式關系數據庫連接規范的誕生。作為擁有龐大用戶群的關系式數據庫 MySQL 也有了反應式驅動,不過并不是官方的。但是 Spring 官方將其納入了依賴池,說明該類庫的質量并不低。所以今天就嘗嘗鮮,試一下使用 R2DBC 連接 MySQL。
2. 環境依賴
基于 Spring Boot 2.3.1 和 Spring Data R2DBC,還有反應式 Web 框架 Webflux,同時也要依賴 r2dbc-mysql 庫,所有的 Maven 依賴為:
!--r2dbc mysql 庫 --
dependency
groupId dev.miku /groupId
artifactId r2dbc-mysql /artifactId
/dependency
!--Spring r2dbc 抽象層 --
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-data-r2dbc /artifactId
/dependency
!-- 自動配置需要引入的一個嵌入式數據庫類型對象 --
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-data-jdbc /artifactId
/dependency
!-- 反應式 web 框架 --
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-webflux /artifactId
/dependency
MySQL 版本為 5.7,沒有測試其它版本。
3. R2DBC 配置
所有的 R2DBC 自動配置都在 org.springframework.boot.autoconfigure.data.r2dbc 包下,如果要配置 MySQL 必須針對性的配置對應的連接工廠接口 ConnectionFactory,當然也可以通過 application.yml 配置。個人比較喜歡 JavaConfig。
@Bean
ConnectionFactory connectionFactory() { return MySqlConnectionFactory.from(MySqlConnectionConfiguration.builder()
.host(127.0.0.1)
.port(3306)
.username(root)
.password(123456)
.database(database_name)
// 額外的其它非必選參數省略
.build());
}
詳細配置可參考 r2dbc-mysql 的官方說明:https://github.com/mirromutth/r2dbc-mysql
當 ConnectionFactory 配置好后,就會被注入 DatabaseClient 對象。該對象是非阻塞的,用于執行數據庫反應性客戶端調用與反應流背壓請求。我們可以通過該接口反應式地操作數據庫。
4. 編寫反應式接口
我們先創建一張表并寫入一些數據:
create table client_user
user_id
varchar(64) not null comment
用戶唯一標示 primary key,
username varchar(64) null comment
名稱 ,
phone_number varchar(64) null comment
手機號 ,
gender tinyint(1) default
0
null comment
0 未知 1 男 2 女
)
對應的實體為:
package cn.felord.r2dbc.config;
import lombok.Data;
* @author felord.cn
*/
@Data
public class ClientUser {
private String userId;
private String username;
private String phoneNumber;
private Integer gender;
}
然后我們編寫一個 Webflux 的反應式接口:
package cn.felord.r2dbc.config;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
* The type User controller.
*
* @author felord.cn
* @since 17 :07
*/
@RestController
@RequestMapping(/user)
public class UserController {
@Resource
private DatabaseClient databaseClient;
/**
* 查詢
* @return 返回 Flux 序列 包含所有的 ClientUser
@GetMapping(/get)
public Flux ClientUser clientUserFlux() { return databaseClient.execute( select * from client_user).as(ClientUser.class)
.fetch()
.all();
}
/**
* 響應式寫入.
* @return Mono 對象包含更新成功的條數
@GetMapping(/add)
public Mono Integer insert() { ClientUser clientUser = new ClientUser();
clientUser.setUserId( 34345514644
clientUser.setUsername( felord.cn
clientUser.setPhoneNumber( 3456121
clientUser.setGender(1);
return databaseClient.insert().into(ClientUser.class)
.using(clientUser)
.fetch().rowsUpdated();
}
}
調用接口就能獲取到期望的數據結果。
5. 總結
乍一看 R2DBC 并沒有想象中的那么難,但是間接的需要了解 Flux、Mono 等抽象概念。同時目前來說如果不和 Webflux 框架配合也沒有使用場景。就本文的 MySQL 而言,R2DBC 驅動還是社區維護(不得不說 PgSQL 就做的很好)。
然而需要你看清的是反應式才是未來。如果你要抓住未來就需要現在就了解一些相關的知識。這讓我想起五年前剛剛接觸 Spring Boot 的感覺。
上述內容就是如何理解反應式數據庫驅動規范 R2DBC,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注丸趣 TV 行業資訊頻道。