AWS Aurora Serverless Spring Boot 通信链接错误 [英] AWS Aurora Serverless Spring Boot Communication Link Error

查看:85
本文介绍了AWS Aurora Serverless Spring Boot 通信链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个原型,用于将我们基于 Spring Boot 的应用程序迁移到 AWS Aurora 数据库,包括无服务器模式.使用预配置模式,事情按预期工作.但是,在无服务器模式下,应用程序无法从 EC2 实例连接到数据库,以下情况除外:

I am doing a prototype for moving our Spring Boot based application to AWS Aurora DB including Serverless mode. With Provisioned mode things work as expected. However with serverless mode the application is not able to connect to the DB from EC2 instance with exception as:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'entityManagerFactory' defined in class path resource 
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: 
Invocation of init method failed; nested exception is javax.persistence.PersistenceException: 
[PersistenceUnit: default] Unable to build Hibernate SessionFactory; 
nested exception is org.hibernate.exception.JDBCConnectionException: 
Unable to open JDBC Connection for DDL execution

.....

Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: 
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. 
The driver has not received any packets from the server.

我尝试减小初始池大小等,但仍然出现相同的错误

I have tried reducing the initial pool size etc but still get the same error

spring.datasource.url=jdbc:mysql://xxxxxx.cluster-xxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/db_example
spring.datasource.username=xxxx
spring.datasource.password=xxxxxxx
spring.datasource.hikari.maximum-pool-size=1
spring.datasource.hikari.minimum-idle=0
spring.datasource.hikari.connection-timeout=60000
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
....

当我将数据源更改为 SimpleDataSource 或从 EC2 实例运行一个简单的 java jdbc 连接时,它可以工作.

When I change the Datasource to SimpleDataSource or run a simple java jdbc connection from EC2 instance it works.

@Bean("dataSource")
public DataSource dataSource() {
  SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
  ....
}     

对于 Spring 启动 JPA/JDBC 应用程序,这是否正常?我们必须使用 SimpleDriverDataSource 吗?也许我们更应该使用 https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html?那可能会重新编写很多与数据库相关的代码.

Is this normal with a Spring boot JPA/JDBC Application? Do we have to use a SimpleDriverDataSource? Perhaps are we rather supposed to use https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html? That will probably be re-writing a lot of DB related code.

推荐答案

在无服务器模式下,虽然通过随时可用的实例警告池进行了高度优化,但您的应用程序需要一些时间才能真正连接到 Aurora.

In serverless mode, although highly optimized with a warn up pool of ready-to-go instances, your application requires some time until you can actually connect to Aurora.

因此,很可能在应用程序启动时与数据库的连接尚不可用.

For this reason, it is very likely that at the start of the application the connection to the database is not yet available.

对于您的错误跟踪,您似乎正在尝试执行某种 DDL 操作,这可能是因为您正在使用 hbm2ddl 创建或验证数据库架构,而此操作需要在应用程序启动时建立连接.

It seems, for your error trace, that you are trying to do some kind of DDL operation, probably because you are using hbm2ddl to create or validate the database schema, and this operation requires a connection just while application is starting.

为了支持这种数据库架构,你可以懒惰地初始化你的 bean(在实体管理器和 Hibernate 的情况下可能是基石),或者寻找像 Liquibase 这样的 DDL 生成替代方法(尽管我认为问题仍然存在).

In order to support this kind of database architecture, you can lazily initialize your beans (which in the case of entity managers and Hibernate could be cornerstone), or look for an alternative approach for DDL generation like Liquibase (although I think the problem will remain).

您可以尝试调整您的 Hikari 连接参数.例如,本文提出了一种巧妙的配置,并提供了对 AWS Aurora 的深入了解:

You can try to tweak your Hikari connection parameters. For instance, this article propose a clever configuration and provides a great insight into AWS Aurora:

https://silexdata.com/modern-applications-and-aws-aurora-serverless/

我唯一不明白的是为什么您的代码可以使用 SimpleDriverDataSource:也许它提供了一些默认值,允许您的应用程序从一开始就连接到 Aurora 而不会出错.

The only thing that I do not understand is why your code works with SimpleDriverDataSource: maybe it provides some defaults that allow your app to connect to Aurora without error from the beginning.

这篇关于AWS Aurora Serverless Spring Boot 通信链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆