本篇文章為大家展示了如何理解反應式數據庫驅動(dòng)規范R2DBC,內容簡(jiǎn)明扼要并且容易理解,絕對能使你眼前一亮,通過(guò)這篇文章的詳細介紹希望你能有所收獲。
R2DBC是一種異步的、非阻塞的關(guān)系式數據庫連接規范。盡管一些數據庫供應商為其數據庫提供了反應式數據庫客戶(hù)端,但對于大多數項目而言,遷移到NoSQL并不是一個(gè)理想的選擇。這促使了一個(gè)通用的響應式關(guān)系數據庫連接規范的誕生。 作為擁有龐大用戶(hù)群的關(guān)系式數據庫也有了反應式驅動(dòng),不過(guò)并不是官方的。但是Spring官方將其納入了依賴(lài)池,說(shuō)明該類(lèi)庫的質(zhì)量并不低。所以今天就嘗嘗鮮,試一下使用R2DBC連接MySQL。
基于Spring Boot 2.3.1和Spring Data R2DBC,還有反應式Web框架Webflux,同時(shí)也要依賴(lài)r2dbc-mysql庫,所有的Maven依賴(lài)為:
<!--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> <!--自動(dòng)配置需要引入的一個(gè)嵌入式數據庫類(lèi)型對象--> <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,沒(méi)有測試其它版本。
所有的R2DBC自動(dòng)配置都在org.springframework.boot.autoconfigure.data.r2dbc
包下,如果要配置MySQL必須針對性的配置對應的連接工廠(chǎng)接口ConnectionFactory
,當然也可以通過(guò)application.yml
配置。個(gè)人比較喜歡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的官方說(shuō)明:https://github.com/mirromutth/r2dbc-mysql
當ConnectionFactory
配置好后,就會(huì )被注入DatabaseClient
對象。該對象是非阻塞的,用于執行數據庫反應性客戶(hù)端調用與反應流背壓請求。我們可以通過(guò)該接口反應式地操作數據庫。
我們先創(chuàng )建一張表并寫(xiě)入一些數據:
create table client_user ( user_id varchar(64) not null comment '用戶(hù)唯一標示' primary key, username varchar(64) null comment '名稱(chēng)', phone_number varchar(64) null comment '手機號', gender tinyint(1) default 0 null comment '0 未知 1 男 2 女 ' )
對應的實(shí)體為:
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; }
然后我們編寫(xiě)一個(gè)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; /** * 查詢(xún) * * @return 返回Flux序列 包含所有的ClientUser */ @GetMapping("/get") public Flux<ClientUser> clientUserFlux() { return databaseClient.execute("select * from client_user").as(ClientUser.class) .fetch() .all(); } /** * 響應式寫(xiě)入. * * @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(); } }
調用接口就能獲取到期望的數據結果。
乍一看R2DBC并沒(méi)有想象中的那么難,但是間接的需要了解Flux
、Mono
等抽象概念。同時(shí)目前來(lái)說(shuō)如果不和Webflux框架配合也沒(méi)有使用場(chǎng)景。就本文的MySQL而言,R2DBC驅動(dòng)還是社區維護(不得不說(shuō)PgSQL就做的很好)。
然而需要你看清的是反應式才是未來(lái)。如果你要抓住未來(lái)就需要現在就了解一些相關(guān)的知識。這讓我想起五年前剛剛接觸Spring Boot的感覺(jué)。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站