将Sping Boot 应用部署到Heroku时出现“Failed to configure a DataSource”错误

ev7lccsx  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在努力让我的Sping 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的错误。有没有人遇到过类似的问题,或者可以指出我可能出错的地方?任何建议或指针将是无价的!

mefy6pfw

mefy6pfw1#

该问题是由于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文件,并且一切都开始正确连接。

相关问题