Spring Cloud AWS – RDS – Spring Cloud AWS – RDS

最后修改: 2018年 1月 12日

中文/混合/英文(键盘快捷键:t)

In the previous article, we were focusing on EC2; now, let’s move on to the Relational Database Service.

前一篇文章中,我们关注的是EC2;现在,让我们继续关注关系型数据库服务。

1. RDS Support

1.RDS支持

1.1. Simple Configuration

1.1.简单配置

Spring Cloud AWS can automatically create a DataSource just by specifying the RDS database identifier and the master password. The username, JDBC driver, and the complete URL are all resolved by Spring.

Spring Cloud AWS可以自动创建一个DataSource,只需指定RDS数据库标识符和主密码。用户名、JDBC驱动程序和完整的URL都是由Spring解决。

If an AWS account has an RDS instance with DB instance identifier as spring-cloud-test-db having master password se3retpass, then all that’s required to create a DataSource is the following line in application.properties:

如果AWS账户有一个RDS实例,其DB实例标识符为spring-cloud-test-db,主密码为se3retpass,那么创建DataSource所需的就是application.properties中的以下一行。

cloud.aws.rds.spring-cloud-test-db.password=se3retpass

Three other properties can be added if you wish to use values other than the RDS default:

如果你希望使用RDS默认值以外的值,可以添加其他三个属性。

cloud.aws.rds.spring-cloud-test-db.username=testuser
cloud.aws.rds.spring-cloud-test-db.readReplicaSupport=true
cloud.aws.rds.spring-cloud-test-db.databaseName=test

1.2. Custom Datasource

1.2.自定义数据源

In an application without Spring Boot or in cases where custom configurations are required, we can also create the DataSource using the Java-based configuration:

在没有Spring Boot的应用程序中,或者在需要自定义配置的情况下,我们也可以使用基于Java的配置创建DataSource

@Configuration
@EnableRdsInstance(
  dbInstanceIdentifier = "spring-cloud-test-db", 
  password = "se3retpass")
public class SpringRDSSupport {

    @Bean
    public RdsInstanceConfigurer instanceConfigurer() {
        return () -> {
            TomcatJdbcDataSourceFactory dataSourceFactory
             = new TomcatJdbcDataSourceFactory();
            dataSourceFactory.setInitialSize(10);
            dataSourceFactory.setValidationQuery("SELECT 1");
            return dataSourceFactory;
        };
    }
}

Also, note that we need to add the correct JDBC driver dependency.

另外,注意我们需要添加正确的JDBC驱动依赖关系。

2. Conclusion

2.结论

In this article, we had a look at various ways of accessing AWS RDS service; in the next and final article of the series, we’ll have a look at AWS Messaging support.

在这篇文章中,我们了解了访问AWS RDS服务的各种方式;在下一篇也是该系列的最后一篇文章中,我们将了解AWS消息支持。

As usual, the examples are available over on GitHub.

像往常一样,这些例子可以在GitHub上找到over

Next »

Spring Cloud AWS – Messaging Support

« Previous

Spring Cloud AWS – EC2