【问题标题】:How to Configure AWS RDS for a Spring Boot Project Running on an EC2 Instance?如何为在 EC2 实例上运行的 Spring Boot 项目配置 AWS RDS?
【发布时间】:2021-09-29 23:36:32
【问题描述】:

我有一个 AWS EC2 Linux 实例运行我的 Spring Boot 项目中的一个 jar。我创建了一个 AWS RDS Postgres 实例,并尝试从我的 EC2 实例中运行的服务中调用它。我主要遇到配置问题。下面,我尝试在我的 spring application.properties 文件中进行以下配置:

cloud.aws.rds.capstoneinstance
cloud.aws.rds.capstoneinstance.password = mypassword
cloud.aws.rds.capstoneinstance.username = myusername
cloud.aws.rds.capstoneinstance.readReplicaSupport = true
cloud.aws.rds.capstoneinstance.databaseName = mydbname

这是我的 Gradle 导入:

implementation("org.springframework.cloud:spring-cloud-aws-jdbc:2.1.2.RELEASE")

我的程序甚至不会运行,但我相信那只是因为 spring 正在寻找“spring.datasource.url”等。

这是输出:

***************************
APPLICATION FAILED TO START
***************************

Description:

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).

【问题讨论】:

    标签: spring amazon-web-services spring-boot amazon-ec2 amazon-rds


    【解决方案1】:

    如果您使用的是 AWS Aurora RDS 集群(Aurora 引擎),而不是直接使用 AWS RDS Postgres(Postgres 引擎),那么 AWS 云驱动程序似乎不支持该功能。

    如果此功能对您很重要,请提供更多详细信息。这是他们的路线图很长一段时间。 https://github.com/awspring/spring-cloud-aws/issues/50

    【讨论】:

      【解决方案2】:

      为您的项目使用 JDBC 配置。

      package com.objectone.indeskarchive.config;
      
      import com.zaxxer.hikari.HikariConfig;
      import com.zaxxer.hikari.HikariDataSource;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.Primary;
      import org.springframework.jdbc.core.JdbcTemplate;
      import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
      
      import javax.sql.DataSource;
      
      /**
       * @author stelansimonsz
       */
      @Configuration
      public class JdbcAuroraDatasourceConfiguration {
      
          @Value("${aurora.datasource.url}")
          private String url;
          @Value("${aurora.datasource.username}")
          private String username;
          @Value("${aurora.datasource.password}")
          private String password;
          @Value("${aurora.datasource.driver-class-name}")
          private String driverClassName;
          @Value("${aurora.datasource.max-pool-size}")
          private int maxPoolSize;
      
          @Bean(name = "mysqlDataSource")
          @Primary
          public DataSource dataSource() {
              HikariConfig hikariConfig = new HikariConfig();
              hikariConfig.setDriverClassName(driverClassName);
              hikariConfig.setUsername(username);
              hikariConfig.setPassword(password);
              hikariConfig.setJdbcUrl(url);
              hikariConfig.setMaximumPoolSize(maxPoolSize);
              return new HikariDataSource(hikariConfig);
          }
      
          @Bean(name = "mysqlJdbcTemplate")
          public JdbcTemplate jdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
              return new JdbcTemplate(dataSource);
          }
      
          @Bean(name = "namedParameterJdbcTemplate")
          public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
              return new NamedParameterJdbcTemplate(dataSource);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-01
        • 1970-01-01
        • 2019-03-05
        • 2021-09-15
        • 2019-06-23
        • 2020-01-14
        • 1970-01-01
        • 2022-06-12
        相关资源
        最近更新 更多