r2dbc-mysql 使用的一些问题


1. timezone 警告

  • application.yml 相关配置:
#
spring:
  application:
    name: xxx
  r2dbc:
    url: r2dbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai
...
  • 控制台警告内容如下:
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
[actor-tcp-nio-1] dev.miku.r2dbc.mysql.MySqlConnection     : The server timezone is <?й???׼ʱ?> that's unknown, trying to use system default timezone
  • 如上: 虽然已经配置了 timezone ,但仍然有警告。
  • 经查询 官方(https://github.com/mirromutth/r2dbc-mysql)说明,timezone 相关配置如下:

在这里插入图片描述

  • 也就是需要将 application.yml 相关配置 修改为如下:
#
spring:
  application:
    name: xxx
  r2dbc:
    url: r2dbc:mysql://localhost:3306/test?serverZoneId=Asia/Shanghai
...
  • 修改后实测不报警告了:

在这里插入图片描述

2. dev.miku.r2dbc-mysql 与 spring-boot-starter-parent 兼容性问题

  • 当 pom.xml 相关配置如下时(r2dbc-mysql 不指定具体版本号):
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!--<version>2.7.10</version>-->
    <version>2.6.1</version>
    <!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <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>
        <scope>runtime</scope>
    </dependency>

    <!--<dependency>
        <groupId>io.asyncer</groupId>
        <artifactId>r2dbc-mysql</artifactId>
        <version>1.0.0</version>
    </dependency>-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!--<dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  • 使用 2.7.10 或者 3.xx 时不会引入 r2dbc-mysql,mysql-connector-java也就是说 spring-boot-starter-parent 没有在 <dependencyManagement> 中定义相关的版本;
  • 实测 使用 2.6.1 或者 2.3.7.RELEASE 可以引入 r2dbc-mysql,mysql-connector-java 这两个依赖;

以下为猜测

  • 个人猜测高版本可能要用 dev.miku.r2dbc-mysql 的继任者 io.asyncer.r2dbc-mysql 但是到目前为止 maven 中央仓库还没有 io.asyncer.r2dbc-mysql: 1.0.0 但是有另外一个 :
<dependency>
  <groupId>com.github.jasync-sql</groupId>
  <artifactId>jasync-mysql</artifactId>
  <version>2.1.16</version>
</dependency>
  • 我不知它们3者什么关系,我也没细看文档,以上都是瞎猜的。
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring WebFlux中使用R2DBC连接MySQL并集成Flyway,可以按照以下步骤进行操作: 1. 在pom.xml中添加所需的依赖项: ```xml <!-- Spring WebFlux --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- R2DBC MySQL 驱动 --> <dependency> <groupId>dev.miku</groupId> <artifactId>r2dbc-mysql</artifactId> <version>0.8.2.RELEASE</version> </dependency> <!-- Flyway --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> ``` 2. 在application.properties中配置R2DBC连接和Flyway: ```properties # R2DBC MySQL 连接配置 spring.r2dbc.url=r2dbc:mysql://localhost:3306/test spring.r2dbc.username=root spring.r2dbc.password=root # Flyway 配置 flyway.locations=classpath:db/migration flyway.clean-disabled=false flyway.baseline-on-migrate=true ``` 3. 创建数据库迁移脚本文件,存放在`src/main/resources/db/migration`目录下。例如,创建一个名为`V1__init.sql`的脚本文件,用于初始化数据库: ```sql CREATE TABLE `user` ( `id` INT PRIMARY KEY, `name` VARCHAR(50) NOT NULL, `age` INT NOT NULL ); ``` 4. 在Spring Boot应用程序中创建一个数据访问对象(DAO)来处理与数据库的交互。可以使用R2DBC提供的`DatabaseClient`或Spring Data R2DBC来简化数据库访问。 5. 启动应用程序,Flyway将自动执行数据库迁移脚本,初始化数据库。 注意:R2DBC是非阻塞的数据库访问方式,与传统的JDBC和Spring Data JPA不同,需要使用异步的方式进行操作。确保代码中的异步操作正确处理。 希望对你有所帮助!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值