Microservice MySQL implementation¶
Introduction¶
An Enterprise application must require a modern and industry-standard database implementation. This page covers three component within my multi-module project which use MySQL instances. The Game service, User service & Highscore service.
Execution¶
My implementation required a drastic change in my architecture, highlighted more clearly in the Scaleable Architecture learning goal. The short summary is: each service on its own requires its own database implementation. This page will cover the implementation in my Game service.
First concept¶
Initially I created a docker compose file so that this system could work locally. A lot of troubleshooting had to be done in order to get the compose to work – mostly due to hotloading Spring properties into it. Without this, I couldn’t have the database fill itself with example records.
Here is my initial configuration (that did not work)
After manual adjustments and trial & error runs, I did more research into this as my requirements for the database was more complex than forcing mysql in my docker compose. Then I found a website: bezkoder which had a more elaborate version. I implemented this in my own project and made a few adjustments, and it worked!
Additionally I added the .env to my project as well.
version: "3.8"
services:
mysqldb:
image: mysql:5.7
restart: unless-stopped
env_file: .env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- 3306:3306
volumes:
- db:/var/lib/mysql
gameService:
depends_on:
- mysqldb
build: ./gameService/
image: ramses-gameservice:latest
container_name: ram
restart: on-failure
env_file: .env
ports:
- 9521:9521
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
volumes:
db:
After changing up the yml, I re-ran docker-compose up -d; resulting in a successful deployment.
Validation¶
Here is the Docker container running locally:
And here is the output of the terminal that is being ran, showing a successful deployment:
In this screenshot I’m using Postman to test if the API call works in my local Docker setup: