How to Build a Chat App With Spring Boot and DynamoDB
Join the DZone community and get the full member experience.
Join For FreeThe goal of this article is to show the simplest chat implementation with one server and multiple clients based on Spring Boot and deployed to AWS.
For source code, you can refer to this link: https://github.com/acierto/aws-chat.git
First I'd like to tell amount the used API in the project:
- User first has to connect to the server with api GET call
/connect?name=USER_NAME
. If your username is unique and you successfully connected, you'll get back a token. You will need this token to send messages or be able disconnect. - To send a message, you need to send a POST call
/send?token=TOKEN
, and in the body of the api call, enclose the message text. If the message is sent successfully, as a result you'll getSENT
in response. - You can check the entire history of all users by GET api call
/history
- And when you would like to disconnect by sending GET api call
/disconnect
.
It's a very simple API and could be greatly improved, but the goal is not implementing a perfect API for chat, or handling all potential cases. Rather we'll see how to combine the things together. And keep the less code to easier grasp for beginners how it works.
Before going into details of the implementation, I'd suggest you to run that application locally, to configure so that you can experiment with what you read later.
First, you need to configure the AWS CLI at your computer. You can follow AWS documentation for that.
The most important that you have created ~/.aws/credentials
having there something like:
xxxxxxxxxx
[default]
aws_access_key_id=...
aws_secret_access_key=...
and ~/.aws/config
to specify your region
xxxxxxxxxx
[default]
region=eu-west-1
output=json
After that you need to:
- Checkout the project:
git clone git@github.com:acierto/aws-chat.git
cd aws-chat && ./gradlew :aws-chat-server:bootRun
- this command starts the server. First time it can take a while to pull the docker image of Dynamodb. Wait till it starts.- Then in a separate terminal window you can start a client (or multiple clients):
cd aws-chat && ./gradlew :aws-chat-client:bootRun
.
Now let's see what is happening under the hood of the server part. The main class which boots the application is ChatServerApplication. You can see the annotation on that class @SpringBootAnnotation. This annotation does many things if you have a look of its sources
xxxxxxxxxx
ElementType.TYPE) (
RetentionPolicy.RUNTIME) (
excludeFilters = { (type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), (
type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) (
It runs auto-configuration of multiple modules and scans all packages. You can tweak it, by disabling some configurations or restricting which packages to scan. In this example it is not requires though.
Alone with boosting the application, we also setting up and running AWS Dynamo DB locally. So it's easy and free to work with it. The entire configuration for this part is done in build.gradle:
xxxxxxxxxx
repositories {
mavenCentral()
maven {
url 'http://dynamodb-local.s3-website-us-west-2.amazonaws.com/release'
}
}
configurations {
dynamodb
}
dependencies {
implementation 'com.amazonaws:aws-java-sdk-core:1.11.880'
implementation 'com.amazonaws:aws-java-sdk-dynamodb:1.11.880'
implementation 'com.github.derjust:spring-data-dynamodb:5.1.0'
implementation 'com.amazonaws:DynamoDBLocal:1.13.2'
dynamodb fileTree(dir: 'lib', include: ["*.dylib", "*.so", "*.dll"])
dynamodb 'com.amazonaws:DynamoDBLocal:1.13.2'
}
dockerCompose {
useComposeFiles = ['docker-compose.yml']
}
And docker-compose.yml
with docker image of DynamoDB
xxxxxxxxxx
version'3.7'
services
dynamodb-local
image amazon/dynamodb-local latest
container_name dynamodb-local
ports
"8000:8000"
It's very nice to be able to browse the data and you can configure it with next steps:
npm install -g dynamodb-admin
export DYNAMO_ENDPOINT=http://localhost:8000
dynamodb-admin
Once you finish with that you can see something like this:
It also allows to manipulate with data, so really handy to make it up and running.
The configuration to the database is configured in application.yml, you can configure there the endpoint:
xxxxxxxxxx
aws
server
dynamodb
endpoint
http://localhost:8000
Configuration in Java is pretty simple for that. You can find many examples where also specified AWS credentials here, but it is not needed and actually an anti-pattern, as you will have to keep those credentials in your configuration files. Better to have them only in ~/.aws/credentials
and make application read it implicitly:
xxxxxxxxxx
basePackages = "com.acierto.awschat.server.storage.repositories") (
public class DynamoDBConfig {
"${aws.server.dynamodb.endpoint}") (
private String dynamoDbEndpoint;
"${aws.server.signingRegion}") (
private String signingRegion;
public AmazonDynamoDB amazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(dynamoDbEndpoint, signingRegion))
.build();
}
}
To make the work with DynamoDb easy and minimum from the code perspective, here configured Spring-Data. It really simplifies a lot of internal things which you have to know to work with this database. I recommend to use it as well.
When application boots the first time, 2 tables are created:
xxxxxxxxxx
public class StorageManager {
private final AmazonDynamoDB amazonDynamoDB;
public StorageManager(AmazonDynamoDB amazonDynamoDB) {
this.amazonDynamoDB = amazonDynamoDB;
}
public void createTables() {
createTable(Message.class);
createTable(User.class);
}
private boolean tableExists(Class<?> table) {
ListTablesResult result = amazonDynamoDB.listTables();
return result.getTableNames().contains(table.getSimpleName());
}
private void createTable(Class<?> table) {
if(!tableExists(table)) {
DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(table);
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
}
}
}
There is no built in functionality in DynamoDB to create table only when it doesn't exist, and no API to check it just by name, so one of the solutions as here, to list all created tabes so far and check there.
When you create table in DynamoDB you have to specify the throughput new ProvisionedThroughput(1L, 1L)
, it's important as the bigger throughput you specify the higher pay check you'll get in the end of the month. In real application you have to tune it, so that it's optimal for users and your bill. First parameter is responsible for read throughput and second for write. The capacity is measure in Units. I advice to check official link to read about that https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html as there quite some theory how to calculate it. And if you are going to take AWS Developer Certification exam, you have to know it well.
Querying with dynamodb-spring-data is very simple, you have to only define interfaces:
xxxxxxxxxx
public interface UserRepository extends CrudRepository<User, String> {
Optional<User> findById(String id);
User findByName(String name);
User findBySessionToken(String sessionToken);
}
xxxxxxxxxx
public interface MessageRepository extends CrudRepository<Message, String> {
Optional<Message> findById(String id);
}
All queries will be built for you.
One thing what is only required from you, to properly define entities for the database. Like let's have a look at user:
xxxxxxxxxx
tableName = "User") (
public class User {
private String id;
private String name;
private String sessionToken;
Only one field is mandatory for DynamoDb, is to define a hash key.
With a massive data you have to have a look at the usage of DynamoDBIndexHashKey
and DynamoDBIndexRangeKey
. The difference between them only what kind of cache to use - global or local. As you might guess from names it depends where is the data is located, across different partitions or on the same partition. Though each of the option has own limitations which have to be considered carefully.
The client is pretty simple, it is also a spring-boot application which sends 3 messages and signs off. In resources there are 2 files added with the list of names and messages, which are picked randomly. If you will start playing with a big load of clients, could happen a name collision, but it's fine and user process will not be able to send messages.
In logs you will see similar to this:
xxxxxxxxxx
2020-11-03 21:45:00.318 INFO 26710 --- [ main] c.a.a.client.ChatClientApplication : Started ChatClientApplication in 1.252 seconds (JVM running for 1.59)
2020-11-03 21:45:00.399 INFO 26710 --- [poolScheduler-1] c.a.a.client.ScheduledMessageSender : [200 OK] Received response 977afd97-a2d5-41ec-8e75-0310281de75f
2020-11-03 21:45:00.469 INFO 26710 --- [poolScheduler-1] c.a.a.client.ScheduledMessageSender : [200 OK] Received response SENT
2020-11-03 21:45:01.411 INFO 26710 --- [poolScheduler-1] c.a.a.client.ScheduledMessageSender : [200 OK] Received response SENT
2020-11-03 21:45:02.424 INFO 26710 --- [poolScheduler-1] c.a.a.client.ScheduledMessageSender : [200 OK] Received response SENT
2020-11-03 21:45:03.403 INFO 26710 --- [poolScheduler-1] c.a.a.client.ScheduledMessageSender : [200 OK] Received response SENT
2020-11-03 21:45:04.720 INFO 26710 --- [poolScheduler-1] c.a.a.client.ScheduledMessageSender : [200 OK] Received response SIGNED OFF
2020-11-03 21:45:04.742 INFO 26710 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-11-03 21:45:04.742 INFO 26710 --- [extShutdownHook] heduledMessageSender$CustomTaskScheduler : Shutting down ExecutorService 'poolScheduler'
The scheduler is running to send a message each second and after some interval it is stopped, user is disconnecting and the entire process is killing by System.exit(0)
.
I also prepared a docker file to run it on EC2
xxxxxxxxxx
FROM openjdk:11-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} aws-chat-server.jar
ENTRYPOINT ["java","-jar","/aws-chat-server.jar"]
You can also experiment with that by running multiple instances of clients on ECS, to make a load, changing the frequency and the amount of send messages, etc. Take into account that you will have to configure Dynamo DB on your AWS account first. But it will be quite easy by following GUI wizard.
Opinions expressed by DZone contributors are their own.
Comments