将 Spring Boot 应用程序部署到 Heroku 时出现“无法配置数据源”错误

问题描述 投票:0回答:1

我正在努力让我的 Spring Boot 应用程序在 Heroku 上运行。虽然该应用程序在我的 本地计算机 上完美运行,但它在 Heroku 上抛出错误,无论是在 Heroku 本地网络上还是在 dyno 上。 heroku logs 的错误是:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

相关详情如下:

应用程序配置(application.yaml):

spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: ${db_url} username: ${db_user} password: ${db_pass} server: port: 8081

数据库

:我在 Heroku 上使用 JawsDB for MySQL。

Heroku 环境变量

:我已经在 Heroku 上设置了环境变量,并且可以使用 heroku config 确认它们的存在: db_pass: password (sample value) db_url: jdbc:mysql://hostname:3306/db_name (sample value) db_user: user (sample value)

数据库连接

:使用上述详细信息,我可以从终端连接到数据库,甚至成功创建表。

本地测试

:当硬编码连接详细信息并在本地运行应用程序时,我可以从数据库表中检索信息,没有任何问题。 尽管如此,我在 Heroku 上仍面临上述错误。有没有人遇到过类似的问题或者可以指出我可能出错的地方?任何建议或指示都是无价的!

java mysql spring-boot heroku database-connection
1个回答
0
投票
pom.xml

文件中缺少资源配置引起的,特别是位于 src/main/resources 目录中的 .yaml 文件。 为了解决此问题,将以下资源配置添加到 pom.xml 中:

<resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.yaml</include> </includes> </resource>

添加此内容后,应用程序能够正确打包并识别 
application.yaml

文件,并且一切都开始正常连接。

© www.soinside.com 2019 - 2024. All rights reserved.