如何为在EC2实例上运行的Spring Boot项目配置AWS RDS?

时间:2019-07-17 01:36:46

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

我有一个从我的Spring Boot项目运行jar的AWS EC2 Linux实例。我创建了一个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).

1 个答案:

答案 0 :(得分:0)

为项目使用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);
    }
}