This repository provides a simple solution for application teams to seamlessly connect to RDS reader and writer replicas without making significant changes to the source code.
- Reader replica connections are automatically handled by using
get,find, orlistprefixed functions. - Writer replica connections are managed through
insert,update, ordeleteprefixed functions. - Minimal source code changes required.
- Easy-to-implement solution for managing separate read and write database operations.
-
Clone the repository:
git clone https://github.com/JohnDeere-Tech/isg-alexandria-sample-java-rds-replica.git cd isg-alexandria-sample-java-rds-replica -
Mention the reader and write replica strings in application.properties file
spring.datasource.write.jdbc-url=jdbc:mysql://localhost:3306/master spring.datasource.read.jdbc-url=jdbc:mysql://localhost:3306/slave
-
Encrypt the DB passwords using 2 way encrption key and mention that as environment variable ,
jasypt.encryptor.password=******* -
Use the prefixed functions in your application:
- For read operations (queries), use methods prefixed with
get,find, orlist. - For write operations (inserts/updates/deletes), use methods prefixed with
insert,update, ordelete. - For changing the prefix/suffix for reader/writer functions,visit DataSourceAspect.java
- For read operations (queries), use methods prefixed with
// Read operation example
User user = userRepository.findById(123); // Automatically routed to reader replica
// Write operation example
userRepository.insertNewUser(newUser); // Automatically routed to writer replica- To build the project run on windows with java correto 21,
mvnw.cmd clean package