设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 站长学院 > MySql教程 > 正文

如何理解反应式数据库驱动规范R2DBC

发布时间:2022-01-17 13:18 所属栏目:115 来源:互联网
导读:本篇文章为大家展示了如何理解反应式数据库驱动规范R2DBC,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。 1. 简介 R2DBC是一种异步的、非阻塞的关系式数据库连接规范。尽管一些NoSQL数据库供应商为其数据库提供

        本篇文章为大家展示了如何理解反应式数据库驱动规范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的感觉。

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读