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
<beans xmlns:xsi="http://www.geeksforgeeks.org/2024/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/cloud/aws/jdbc"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/cloud/aws/jdbc
http://www.springframework.org/schema/cloud/aws/jdbc/spring-cloud-aws-jdbc.xsd">
<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.
Similar Reads
Spring Cloud AWS - S3 In Spring Boot, Spring Cloud AWS can provide integration with the Amazon Web Service (AWS), and it can include the Amazon Storage Service(S3). When it's working with the S3 in the Spring Cloud AWS application. This integration can allow the developers to easily interact with the S3 buckets and the o
9 min read
Spring Cloud AWS - EC2 The Spring Cloud for AWS (Amazon Web Services) component makes integrating with hosted Amazon Web Services easier. It provides an easy method to use popular Spring idioms and APIs, such as the messaging or caching API, to communicate with AWS-provided services. Without worrying about infrastructure
3 min read
What Is Spring AWS Cloud ? Nowadays, cloud computing has been involved in almost all application development. Many big and startup companies prefer to use the cloud as it is very efficient and easy to set up their infrastructure. When it comes to Java development, Spring and Spring boot frameworks have been preferred by many
14 min read
Spring Cloud AWS - Messaging Support Spring Cloud for AWS integration process with hosted Amazon Web Services is made easier. It provides an easy means to use popular Spring idioms and APIs, like the messaging or caching API, to interface with services supplied by AWS. The hosted services allow developers to focus on developing their a
4 min read
Spring Data R2DBC Spring Data introduced reactive programming support with the release of Spring Data Reactive Modules. This module allows you to work with reactive database drivers (like MongoDB Reactive, R2DBC for SQL databases, etc.) and provides APIs for performing asynchronous database operations using reactive
3 min read