Open In App

Spring Cloud AWS – RDS

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The fundamental component of Spring Cloud AWS, Spring Cloud AWS Core offers fundamental security and configuration setup services. This module will be used by developers through other modules, not directly. Support for cloud-based environment setups is offered via the core module, which gives users direct access to the CloudFormation metadata unique to each application stack and the instance-based EC2 information.

Implementation of RDS in Spring Cloud AWS

Below is the step-by-step implementation process to enable RDS in Spring Cloud AWS.

Spring Cloud AWS Maven dependency

Maven users can directly leverage Spring Cloud AWS module dependencies upon explicitly configuring the specific module. In addition to the Amazon SDK (Software Development Kit) required to run the modules, the Spring Cloud AWS module also contains all transitive dependencies for the Spring modules. The following will be the typical setup for dependencies:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-context</artifactId>
<version>{spring-cloud-version}</version>
</dependency>
</dependencies>

Configure custom EC2 Client

Sometimes, it is necessary to have a custom EC2 client to retrieve the instance information. The custom EC2 client is supported by the context-instance-data element with the amazon-ec2 attribute.

XML




<beans ...>
  
  <aws-context:context-credentials>....</aws-context:context-credentials>
  <aws-context:context-region ... />
  <aws-context:context-instance-data  amazon-ec2="myCustomClient"/>
  
  <bean id="myCustomClient" class="com.amazonaws.services.ec2.AmazonEC2Client">
    ...
  </bean>
</beans>


Spring Configuration

Spring Cloud AWS may generate a DataSource just by defining the RDS datasource username and the password. The username, JDBC driver, and entire URL are all determined by Spring.

Here, we are using Spring’s configuration properties to define the RDS datasource configuration in application.properties or application.yml file.

spring.datasource.url=jdbc:mysql://<rds-endpoint>:3306/<database-name>
spring.datasource.username=<username>
spring.datasource.password=<password>

Data source configuration

In order for Spring Cloud AWS to collect database metadata information via the Amazon RDS service, the data source configuration must, at the very least, include security and region configurations.

XML




       xsi:schemaLocation="http://www.springframework.org/schema/cloud/aws/jdbc
  
 <aws-context:context-credentials>
  ...
 </aws-context:context-credentials>
  
 <aws-context:context-region region="..."/>
  
<jdbc:data-source
         db-instance-identifier="myRdsDatabase"
         password="${rdsPassword}">
</jdbc:data-source>
</beans>


Read-replica configuration

Read-only transactions will be sent to one of the available read-replicas by Spring Cloud AWS, which will look for any read-replicas that are made for the master database. The example illustrates how to construct a read-replica business service.

Java




@Service
public class SimpleDatabaseService {
  
    private final JdbcTemplate jdbcTemplate;
  
    @Autowired
    public SimpleDatabaseService(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
  
    @Transactional(readOnly = true)
    public Person loadAll() {    
    }
  
    @Transactional
    public void updatePerson(Person person) {
    }
}


Enable Spring Cloud AWS Integration

Using @EnableContextInstanceData in Spring Boot main class we can enable integration with AWS instance metadata, which is useful for obtaining information like instance ID, availability zone, etc.

Custom Datasource

In an application without Spring Boot or in circumstances when custom configurations are necessary, we may alternatively construct the DataSource using the Java-based configuration:

Java




User
@Configuration
@EnableRdsInstance(
  dbInstanceIdentifier = "db-instance-id"
  password = "password)
public class SpringRDSSupport {
  
    @Bean
    public RdsInstanceConfigurer instanceConfigurer() {
        return () -> {
            TomcatJdbcDataSourceFactory dataSourceFactory
             = new TomcatJdbcDataSourceFactory();
            dataSourceFactory.setInitialSize(10);
            dataSourceFactory.setValidationQuery("SELECT 1");
            return dataSourceFactory;
        };
    }
}


Below is the demonstration of the usage of Spring Cloud AWS with RDS:

Java




import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.aws.jdbc.config.annotation.EnableRdsInstance;
  
@SpringBootApplication
@EnableRdsInstance(databaseName = "db-name", dbInstanceIdentifier = "db-instance-id")
public class Application 
{
    public static void main(String args[]) 
    {
        SpringApplication.run(Application.class, args);
    }
}


Conclusion

In conclusion, Spring Cloud AWS Core offers fundamental security and configuration setup services. This module will be used by developers through other modules, not directly. Support for cloud-based environment setups is offered via the core module, which gives users direct access to the CloudFormation metadata unique to each application stack and the instance-based EC2 information.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads