Spring Cloud AWS – RDS

1. RDS Support

1.1. Simple Configuration

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.

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:

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:

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

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:

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

2. Conclusion

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.

As usual, the examples are available over on GitHub.

Related posts:

Using a Mutex Object in Java
Java Program to Solve any Linear Equations
Java 9 Stream API Improvements
Java Program to Implement Gauss Jordan Elimination
The DAO with Spring and Hibernate
ClassNotFoundException vs NoClassDefFoundError
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Implement Knight’s Tour Problem
Using the Not Operator in If Conditions in Java
Spring Boot - Actuator
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Compute DFT Coefficients Directly
Check If a File or Directory Exists in Java
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Convert Hex to ASCII in Java
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Java Program to Implement LinkedHashSet API
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Check whether Directed Graph is Connected using BFS
Tạo chương trình Java đầu tiên sử dụng Eclipse
“Stream has already been operated upon or closed” Exception in Java
Java Program to Implement VList
Java Program to Implement Sorted Circularly Singly Linked List
A Quick Guide to Spring Cloud Consul
Spring Boot - Tomcat Deployment
Filtering and Transforming Collections in Guava
Lập trình mạng với java
Giới thiệu Json Web Token (JWT)
Java Program to Perform Insertion in a BST
Java Program to Represent Graph Using Incidence Matrix