Spring data r2dbc:与 mysql 连接时出现问题 - 找不到“r2dbcEntityTemplate”

Posted

技术标签:

【中文标题】Spring data r2dbc:与 mysql 连接时出现问题 - 找不到“r2dbcEntityTemplate”【英文标题】:Spring data r2dbc: Problem connecting with mysql - 'r2dbcEntityTemplate' could not be found 【发布时间】:2021-11-20 02:30:59 【问题描述】:

我的 Spring Boot 应用程序正在尝试使用 Spring Data r2dbc 连接到 mysql 5.7。设置似乎非常简单。但是,我一直遇到意外的异常。

任何帮助将不胜感激。

日志:

2021-09-28 10:13:48.591  INFO 12261 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
2021-09-28 10:13:48.651  INFO 12261 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 52 ms. Found 1 R2DBC repository interfaces.
//...
2021-09-28 10:13:49.250  WARN 12261 --- [  restartedMain] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 
//...
Error creating bean with name 'healthCheckRepository' defined in application.model.respository.r2dbc.HealthCheckRepository defined in @EnableR2dbcRepositories declared on MainApplication: Cannot resolve reference to bean 'r2dbcEntityTemplate' while setting bean property 'entityOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'r2dbcEntityTemplate' available
//... 
Description:

Parameter 0 of constructor in application.service.HealthCheckService required a bean named 'r2dbcEntityTemplate' that could not be found. 


Action:

Consider defining a bean named 'r2dbcEntityTemplate' in your configuration.

应用程序.yaml

spring:
  r2dbc:
    url: r2dbc:pool:mysql//server/database?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useLocalSessionState=true
    username: username
    password: password
    pool:
      enabled: true
      max-size: 20
      validation-query: SELECT 1
      initial-size: 5
      max-idle-time: 30m
  data:
    r2dbc:
      repositories:
        enabled: true

build.gradle

plugins 
    id 'java'
    id 'org.springframework.boot' version '2.5.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id "io.freefair.lombok" version "6.2.0" //using

dependencies 
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'

//    implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'

    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    runtimeOnly 'dev.miku:r2dbc-mysql:0.8.2.RELEASE'
    runtimeOnly 'mysql:mysql-connector-java'

【问题讨论】:

为什么不用 com.mysql.jdbc.Driver 而不是 r2dbc? @NguyễnVănQuyền 我需要一个用于潜在高负载环境的反应式驱动程序 ***.com/questions/56655291/…。这可能会对您有所帮助! @NguyễnVănQuyền 感谢您的尝试。不幸的是,它并没有真正帮助,因为我在application.yaml 中定义连接配置,而不是在代码中创建ConnectionFactoryDataClient (github.com/spring-projects/spring-data-r2dbc/issues/143) 【参考方案1】:

原来是 Spring 启动问题。解决方法请参考下面的 git issue

https://github.com/spring-projects/spring-data-r2dbc/issues/659

【讨论】:

以上是关于Spring data r2dbc:与 mysql 连接时出现问题 - 找不到“r2dbcEntityTemplate”的主要内容,如果未能解决你的问题,请参考以下文章

Spring认证中国教育管理中心-Spring Data R2DBC框架教程一

Spring Data R2DBC响应式操作MySQL

MYSQL R2DBC 的 Spring Data 多主机设置

Spring Data r2dbc - 实体继承

Spring Data R2DBC 响应式数据库操作使用

#yyds干货盘点#Reactive访问Spring Data R2DBC