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:

How to Iterate Over a Stream With Indices
Java Program to Implement Meldable Heap
Introduction to Using FreeMarker in Spring MVC
Map Serialization and Deserialization with Jackson
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
A Guide to TreeMap in Java
Tính đóng gói (Encapsulation) trong java
Java Program to Implement Stack using Two Queues
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Giới thiệu Google Guice – Injection, Scope
Convert a Map to an Array, List or Set in Java
HttpClient 4 Cookbook
Exploring the Spring Boot TestRestTemplate
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Construct K-D Tree for 2 Dimensional Data
A Guide to ConcurrentMap
An Example of Load Balancing with Zuul and Eureka
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement Network Flow Problem
Registration – Password Strength and Rules
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to implement Priority Queue
Java Program to Find Strongly Connected Components in Graphs
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Spring Boot - Cloud Configuration Client
Spring Boot - Database Handling
Multi Dimensional ArrayList in Java
HandlerAdapters in Spring MVC
Generating Random Numbers in a Range in Java
Quản lý bộ nhớ trong Java với Heap Space vs Stack