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:

Guide to CopyOnWriteArrayList
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Queue using Two Stacks
Spring Cloud – Tracing Services with Zipkin
Introduction to Spring MVC HandlerInterceptor
Most commonly used String methods in Java
Guide to WeakHashMap in Java
Java Program to Perform Deletion in a BST
Converting a Stack Trace to a String in Java
Java Map With Case-Insensitive Keys
HTTP Authentification and CGI/Servlet
Java Program to Implement the Monoalphabetic Cypher
Tính đa hình (Polymorphism) trong Java
Using JWT with Spring Security OAuth (legacy stack)
Spring Boot - Sending Email
Retrieve User Information in Spring Security
Java Program to Create a Random Linear Extension for a DAG
Java Program to Perform Arithmetic Operations on Numbers of Size
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement Bellman-Ford Algorithm
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Implement Cartesian Tree
The Difference Between Collection.stream().forEach() and Collection.forEach()
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Giới thiệu Google Guice – Injection, Scope
Spring Boot Security Auto-Configuration
Lập trình đa luồng với CompletableFuture trong Java 8
Model, ModelMap, and ModelAndView in Spring MVC
Spring REST API with Protocol Buffers
Java 8 Collectors toMap
Java Program to Implement Knight’s Tour Problem
Extract network card address