Microservices (Spring Boot) with DynamoDB

In the era of loosely coupled services, where for a single operation a microservice is being used, Microservice is a good choice for many of the software companies. Though Microservices have pros and cons (we are not going to discuss that in this content), because of ease of development and deployment it has played a large part in shaping up a decentralized application. Cloud has a big part on that. Integrating the microservices in cloud makes easy to monitor without owning Servers or DB. In this course we are going to integrate microservices with Cloud noSQL DB.
We cannot deny that, noSQL DB are the one of the widely used DB in recent times. And Dynamo DB is one of the powerful no SQL DBs in cloud, which provides performance at scale, serverless and enterprise ready application.
We will create a microservice(which can be deployed in an EC2 instance), and then the same will expose the endpoints to be used by the users to perform CRUD operations in Dynamo DB.

Application Structure

Design of the Spring Boot application:
Controller -> Service -> Dao -> Dynamo Configuration + Dynamo Repository Function calls + AWS Dynamo creds

The AWS configuration can be done, by putting the values in appication.properties as below:
#AWS Configuration Credentials
amazon.aws.accesskey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
amazon.aws.secretkey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
amazon.aws.region=us-xxxx-x


Configuration of pom.xml:
Apart from Spring Boot maven dependencies the below dependencies need to be added:

             groupId - com.amazonaws,
artifactId - aws-java-sdk-dynamodb
version - 1.11.506

groupId  - com.github.derjust
artifactId - spring-data-dynamodb
version - 5.0.3

Use Spring CRUD (org.springframework.data.repository.CrudRepository) repository for CRUD operations.

Create the Entity classes and annotate them with DynamoDBTable(by providing the table name):
@DynamoDBTable(tableName=”geeks18_table”)

In Dynamo DB two kinds of primary keys exist.
1. Partition Key: a simple primary key, composed of one attribute known as the partition key, which DynamoDB uses the partition key’s value as input to an internal hash function.
2. Partition key and sort key : Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.

The above implementations can be done by below annotation in the entity class (in the field level mapping):
@DynamoDBHashKey and @DynamoDBRangeKey and other columns can be annotated with @DynamoDBAttribute (which is not mandatory).

DB Config class:
Additional annotation to mark the config class for Dynamo DB repository is needed as below:
@EnableDynamoDBRepositories

Get the values from property file and map AWS dynamoDB credentials with the application as below:

@Configuration
@EnableDynamoDBRepositories
public class DynamoDbConfig {

@Value("amazon.aws.region")
private String amazonAwsRegion;

@Value("amazon.aws.accessKey")
private String amazonAwsAccessKey;

@Value("amazon.aws.secretKey")
private String amazonAwsSecretKey;

public AWSCredentialsProvider amazonAWSCredentialsProvider() {
    return new AWSStaticCredentialsProvider(amazonAWSCredentials());
}

@Bean
public AWSCredentials amazonAWSCredentials() {
    return new BasicAWSCredentials(amazonAwsAccessKey, amazonAwsSecretKey);
}

@Bean
public DynamoDBMapperConfig dynamoDBMapperConfig() {
    return DynamoDBMapperConfig.DEFAULT;
}

@Bean
public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB, DynamoDBMapperConfig config) {
    return new DynamoDBMapper(amazonDynamoDB, config);
}

@Bean
public AmazonDynamoDB amazonDynamoDB() {
    return AmazonDynamoDBClientBuilder.standard().withCredentials(amazonAWSCredentialsProvider()).withRegion(amazonAwsRegion).build();
}
}

Create the repository class as below (you can implement your own method or override the existing ones based on the requirement):

@EnableScan
public interface RequestRepository extends CrudRepository{
}

Example methods to be called (same CRUD operations):
repository.save(product);
repository.deleteById(id);
repository.findAll();
repository.findById(id);

The whole application can be exported as jar and can be deployed in an EC2 instance. Which will make the services up with no physical resources involved.

Please find below the Git code for the application:
https://github.com/dmahapatra/awsDynamoSpringBoot

3 Comments

  1. in some cases it is better to use AWS SDK instead of DynamoDB annotations, e.g. @DynamoDBTable, @DynamoDBAttribute. Especially when you need to write some custom code optimized for performance.

Leave a Reply

Your email address will not be published.


*