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:

HttpAsyncClient Tutorial
Java Program to Implement Sorted Circular Doubly Linked List
A Guide to TreeMap in Java
Java Program to Implement Jarvis Algorithm
Java Program to Implement Bresenham Line Algorithm
Lớp LinkedHashMap trong Java
Introduction to Spring Security Expressions
An Introduction to ThreadLocal in Java
Java Program to Evaluate an Expression using Stacks
Java Program to Implement Insertion Sort
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Implement RenderingHints API
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Converting Between a List and a Set in Java
Giới thiệu Google Guice – Dependency injection (DI) framework
Java Program to Perform Naive String Matching
Từ khóa static và final trong java
Java Program to Implement EnumMap API
Testing in Spring Boot
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Jackson Exceptions – Problems and Solutions
JPA/Hibernate Persistence Context
Java Program to implement Bit Matrix
Guide to CountDownLatch in Java
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Using Spring @ResponseStatus to Set HTTP Status Code
Transactions with Spring and JPA
New Features in Java 8
Java Program to Implement Heap Sort Using Library Functions
Java Program to Implement Merge Sort on n Numbers Without tail-recursion