【问题标题】:How to connect PostgreSQL RDS to spring boot in elastic beanstalk?如何在弹性beantalk中将PostgreSQL RDS连接到spring boot?
【发布时间】:2019-10-30 19:54:50
【问题描述】:

我有一个 Spring Boot 应用程序,它已经可以连接到我笔记本电脑中的本地 Postgres。我将我的 Spring Boot jar 文件上传到弹性 beanstalk 并且部署良好我可以看到我的应用程序中的所有内容,但它无法连接到我在 RDS 中创建的 Postgres db。

我已尝试将 rds 数据库实例名称的数据库标识符和主密码放在应用程序属性文件中,但不起作用。

#cloud.aws.rds.mydbinstanceidentifier
#cloud.aws.rds.testdbspringiloilo.password=mydbmasterpassword

当我尝试请求来自数据库的对象时,它会显示一个白标签页面,告诉我它已超时。

【问题讨论】:

标签: postgresql amazon-web-services spring-boot amazon-elastic-beanstalk


【解决方案1】:

您的应用程序无法访问 RDS 实例并且正在超时。确保:

1- 安全组有一个入口规则,允许在数据库端口(Postgres 默认为 5432)上建立应用程序的子网/安全组的连接
2- 启动 RDS 实例的子网的访问列表允许从您的 Beanstalk 环境传入的流量。
3-您在属性文件中配置了正确的端点和端口。

还可以考虑为您的机密使用环境变量,而不是将它们作为纯文本存储在您的应用程序文件中。

【讨论】:

  • 谢谢 Walid,我会进行检查。
  • 在弹性beantalk中创建spring应用程序后,我在部署前在配置页面中添加了rds的快照。我认为这将确保应用程序和 RDS 中的 VPC/Sec 组都允许流量。
【解决方案2】:

您尝试使用我们的 cloud.aws.rds 配置,但不要使用 Spring Cloud AWS。

你需要额外的依赖:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-aws</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-aws-jdbc</artifactId>
    </dependency>

请在官方文档中阅读更多关于 Spring Cloud AWS 的信息: https://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html

还有一个很好的示例:https://github.com/codeurjc/spring-cloud-aws-sample

【讨论】:

    【解决方案3】:

    好的,这就是我解决问题的方法。我不知道其中哪些实际上解决了它。

    1. 我添加了一条新规则来在 vpc 中打开端口 5432。 (这让我很困惑,因为我明确添加了 Postgres rds 快照,所以配置页面在执行脚本时不应该这样做吗?)

    2. 我向 pom 文件添加了一个新的依赖项,我不太确定这是否真的很重要,因为我已经可以运行应用程序但无法连接到数据库。

      org.springframework.boot spring-boot-starter-web

    3. 我没有使用 db 标识符,而是使用了快照的端点地址。

    感谢所有输入的家伙!干杯!

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 2016-09-14
      • 2017-09-20
      • 2020-08-24
      • 2020-11-30
      • 2019-06-24
      • 2019-06-07
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多