: Error creating bean with name 'org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$JpaInvokerConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
时间: 2025-06-20 13:19:40 浏览: 13
<think>好的,我现在需要解决用户的问题,他们遇到了一个Spring Boot应用启动时的错误,错误信息涉及多个Bean创建失败,最终指向SQLNonTransientConnectionException: Public Key Retrieval is not allowed。用户之前已经询问过关于Public Key Retrieval的问题,现在这个错误出现在更复杂的上下文中,结合了Spring Cloud、JPA、ShardingSphere等组件。
首先,我需要回顾用户之前的问题,了解他们可能已经尝试过哪些解决方案。之前给出的解决方案主要是关于在JDBC连接字符串中添加allowPublicKeyRetrieval=true和useSSL=false,或者启用SSL。现在错误信息中提到了Spring Cloud的RefreshAutoConfiguration、JpaInvokerConfiguration,以及ShardingSphere的SpringBootConfiguration,这说明用户的配置可能比较复杂,涉及多个层次的数据源配置。
接下来,我需要分析错误链。错误开始于RefreshAutoConfiguration$JpaInvokerConfiguration初始化失败,嵌套的原因是DataSourceInitializerInvoker创建失败,再进一步是dataSource Bean创建失败,最终的根本原因是SQL连接问题,即Public Key Retrieval is not allowed。这说明问题可能出在数据源的配置上,特别是在使用ShardingSphere时,可能没有正确传递JDBC参数。
用户可能在使用ShardingSphere作为数据源代理,而ShardingSphere的配置文件中可能没有正确设置MySQL连接参数,导致驱动无法获取公钥。需要检查ShardingSphere的配置文件,确保JDBC URL中包含allowPublicKeyRetrieval=true和useSSL=false等参数。同时,考虑到Spring Cloud的自动配置,尤其是在使用配置刷新时,可能需要确保配置的属性能正确传递给数据源。
另外,用户可能混淆了Spring Boot的数据源自动配置和ShardingSphere的配置方式。ShardingSphere可能通过自定义的DataSource Bean来初始化数据源,这时候需要确保在ShardingSphere的配置中正确传递所有必要的JDBC参数,特别是当使用MySQL 8.0以上版本时,认证插件的问题需要处理。
还可能存在依赖版本的问题,比如ShardingSphere的版本与MySQL Connector/J的版本是否兼容,是否支持相关的参数。需要确认用户使用的ShardingSphere版本是否支持在数据源配置中传递这些参数,或者是否有已知的问题需要升级。
此外,用户可能在多模块项目中配置了多个数据源,或者ShardingSphere的分片规则配置有误,导致数据源初始化失败。需要检查是否有语法错误或配置遗漏,特别是在YAML或properties文件中,是否正确嵌套了数据源参数。
最后,需要提醒用户,虽然allowPublicKeyRetrieval=true可以解决连接问题,但在生产环境中应考虑更安全的方式,如启用SSL,并确保ShardingSphere的配置符合安全要求。同时,提供详细的步骤和配置示例,帮助用户逐步排查问题,确保每个环节都正确配置。</think>---
### **问题定位与解决步骤**
这个错误是典型的 **Spring Boot 应用启动时数据源初始化失败**,根本原因在于 **MySQL 连接配置问题**(`Public Key Retrieval is not allowed`),但涉及多个组件(Spring Cloud、ShardingSphere、JPA)的交互。以下是详细分析与解决方案:
---
### **一、错误链解析**
错误信息的关键路径如下:
```text
1. Error creating bean with name 'RefreshAutoConfiguration$JpaInvokerConfiguration'
↓
2. BeanCreationException: DataSourceInitializerInvoker 初始化失败
↓
3. BeanCreationException: 创建 ShardingSphere 的 'dataSource' Bean 失败
↓
4. SQLNonTransientConnectionException: Public Key Retrieval is not allowed
```
**核心问题**:**ShardingSphere 的数据源配置未正确处理 MySQL 8.0+ 的身份验证协议**。
---
### **二、根本原因**
| 组件 | 问题根源 |
|---------------------|------------------------------------------------------------------------|
| **MySQL Server** | 默认使用 `caching_sha2_password` 认证插件,需公钥验证 |
| **ShardingSphere** | 数据源配置未添加 `allowPublicKeyRetrieval=true` |
| **Spring Boot** | 自动配置触发数据源初始化,但因连接失败导致级联错误 |
---
### **三、解决方案**
#### **步骤 1:修正 ShardingSphere 数据源配置**
在 **ShardingSphere 的配置文件(YAML 或 properties)** 中,为每个数据源添加 MySQL 连接参数:
```yaml
spring:
shardingsphere:
datasource:
names: ds0
ds0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/db_name?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
username: root
password: your_password
```
**关键参数**:
- `allowPublicKeyRetrieval=true`:允许公钥检索
- `useSSL=false`:开发环境临时禁用 SSL(生产环境需启用)
- `serverTimezone=UTC`:避免时区问题
#### **步骤 2:验证依赖版本兼容性**
检查以下依赖版本是否兼容:
| 依赖项 | 推荐版本 |
|----------------------|--------------------|
| MySQL Connector/J | 8.0.25+ |
| ShardingSphere | 5.1.0+ |
| Spring Boot | 2.7.x / 3.0.x |
若使用旧版 ShardingSphere(如 4.x),需升级或手动配置数据源 Bean。
#### **步骤 3:处理 Spring Cloud 配置刷新**
如果使用 **Spring Cloud Config** 或 **@RefreshScope**,需确保配置热更新不会破坏数据源:
```java
@Configuration
@RefreshScope
public class DataSourceConfig {
// 显式定义数据源 Bean(覆盖 ShardingSphere 自动配置)
@Bean
public DataSource dataSource() {
// 手动创建 HikariDataSource 并注入参数
}
}
```
#### **步骤 4:生产环境安全配置**
**推荐方案**(启用 SSL + 固定公钥):
1. 生成 MySQL SSL 证书:
```bash
mysql_ssl_rsa_setup --datadir=/var/lib/mysql-ssl
```
2. 修改 ShardingSphere 配置:
```yaml
jdbc-url: jdbc:mysql://host:3306/db?useSSL=true&requireSSL=true&trustCertificateKeyStoreUrl=file:/path/to/truststore.jks
```
---
### **四、错误排查流程图**
```mermaid
graph TD
A[启动报错] --> B{检查 ShardingSphere 配置}
B -->|缺少连接参数| C[添加 allowPublicKeyRetrieval=true]
B -->|参数已存在| D{检查 MySQL 服务状态}
D -->|认证协议不匹配| E[修改用户认证插件为 mysql_native_password]
D -->|SSL 问题| F[启用 SSL 或配置信任证书]
C --> G[重启应用]
G -->|成功| H[问题解决]
G -->|失败| I[检查依赖版本冲突]
```
---
### **五、补充说明**
- **ShardingSphere 特定配置**:若使用分库分表规则,确保 `dataSources` 配置优先级高于分片规则。
- **日志调试**:启用 `DEBUG` 级别日志定位初始化失败点:
```properties
logging.level.org.springframework.boot.autoconfigure=DEBUG
logging.level.apache.shardingsphere=DEBUG
```
- **连接池调优**:若使用 HikariCP,检查 `max-lifetime` 和 `connection-timeout` 是否过短。
通过上述步骤,可系统性解决因 MySQL 公钥检索限制导致的 Spring Bean 初始化失败问题。
阅读全文
相关推荐










