Hello everyone!
I will give you tutorials to build a Forum-Based Website using Redis. We will have the software architecture like this picture.
In short, we will have MongoDB for persistent data and utilize Redis as a cache, but not only using it as a cache server. We will explore more about Redis's capabilities. We will use Redis Stack for the development process, but you might need Redis Enterprise for production use.
Let's get started!
Setup Project
We will use a microservices structure but with the same database for each service. This approach is called the shared-database-per-service pattern. If you want to know more about the shared-database-per-service pattern, please visit these links:
- Shared Database Explanation from microservices.io by Chris Richardson
- Shared Database Explanation from AWS
- Shared Database Explanation from baeldung.com
Why did I choose the shared-database-per-service pattern? I didn't want this tutorial to have more complex. I want to introduce microservices but as simple as it does. We can refactor our code to use the database per service pattern for later. It's quite common when we will migrate from monolith to microservices using the shared-database-per-service pattern.
Tools Preparation
-
Please download/setup Docker. Please visit this page for more information about it. Please make sure the Docker Compose is also within the installation. Please visit the installation page if you don't have it yet. Verify the installation of Docker using
docker version
command. Otherwise, for Docker Compose usingdocker compose version
command. We will host MongoDB and Redis using Docker. -
Install .NET SDK. I use .NET 6. We will use .NET for
User Service
andThread Service
. Please visit this page for the installer and the guide. Please verify the installation usingdotnet --info
command. -
Install Node.js and yarn. Check this page for .Node.js installer. I use node version
18.3.0
. Check this page for yarn installer. I still use version 1 of yarn. Please verify your installation withnode --version
andyarn --version
.
Project Structure Preparation
We will use this project structure.
forum-api-microservices/
├── app/
│ ├── auth-service/
│ │ ├── src/
│ │ │ └── index.ts
│ │ ├── .eslintignore
│ │ ├── .eslintrc.json
│ │ ├── .gitignore
│ │ ├── Dockerfile
│ │ ├── package.json
│ │ ├── tsconfig.json
│ │ └── yarn.lock
│ ├── thread-service/
│ │ ├── ThreadService/
│ │ │ ├── appsettings.json
│ │ │ ├── Program.cs
│ │ │ └── ThreadService.csproj
│ │ ├── .editorconfig
│ │ ├── .gitignore
│ │ ├── Dockerfile
│ │ └── thread-service.sln
│ └── user-service/
│ ├── UserService/
│ │ ├── appsettings.json
│ │ ├── Program.cs
│ │ └── UserService.csproj
│ ├── .editorconfig
│ ├── .gitignore
│ ├── Dockerfile
│ └── user-service.sln
├── docker-compose.yml
├── LICENSE
└── README.md
User Service
We will use .NET 6 for User Service Project. Please follow this step by step.
- Please create directory
app/user-service
and run all the commands below in that directory. - Generate Project using this command
dotnet new webapi -minimal -o UserService
. - Generate
.gitignore
file. Using this command:dotnet new gitignore
. - Generate
.sln
file. Using this command:dotnet new sln
. - Add our
.csproj
or project to the solution file. Using this command:dotnet sln add UserService
. - Verify your project structure. Your project structure should be like the project stucture section.
- Please check your project completeness by building the project. Run this command:
dotnet build
.
Auth Service
We will use Node.js as the main language for Auth Service. Why didn't we use the same language? These are the special things when we use the microservices approach. We can use different languages for different services. Let's go!
- Please make sure you've created the
app/auth-service
directory and we will run all the commands below in that directory. -
Initiate
package.json
file usingyarn init
. Fill in all the information that you need. -
Install all initial dependencies.
yarn add express@^4.18.1 # install main dependency yarn add --dev typescript@^4.7.4 ts-node-dev@^2.0.0 @types/express@^4.17.13 @types/node@^14.11.2 # install main development tools yarn add --dev eslint@^8.18.0 @typescript-eslint/eslint-plugin@^5.29.0 @typescript-eslint/parser@^5.29.0 # install linter
-
Initiate
tsconfig.json
file. Using this command:yarn tsc --init
. Override the config like this.
{ "compilerOptions": { /* Visit https://aka.ms/tsconfig to read more about this file */ "target": "es2017", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "module": "node16", /* Specify what module code is generated. */ "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ "outDir": "./lib", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ "strict": true, /* Enable all strict type-checking options. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ } }
-
Initiate our project using this file and store it with name
index.ts
and store it atsrc
folder.
import express from 'express'; const port = parseInt(process.env.PORT || '9000'); const app = express(); app.use(express.json()); app.get("/", (req, res) => { res.json({ message: 'Hello World!', }); }) app.listen(port, () => { console.log(`Server listen on port ${port}`) });
-
Please make sure your
.eslintrc.json
file like this.
{ "parser": "@typescript-eslint/parser", "plugins": ["@typescript-eslint"], "extends": [ "eslint:recommended", "plugin:@typescript-eslint/recommended" ] }
-
Add some scripts runner and modify other configs in your
package.json
.
// ... other configs "main": "lib/index.js", "scripts": { "start": "node lib/index.js", "start:dev": "ts-node-dev src/index.ts", "compile": "tsc", "lint": "eslint .", "fix": "eslint . --fix" }, // ... other configs
Thread Service
The step by step of Thread Service will same with User Service.
- Please create directory
app/thread-service
and run all the commands below in that directory. - Generate Project using this command
dotnet new webapi -minimal -o ThreadService
. - Generate
.gitignore
file. Using this command:dotnet new gitignore
. - Generate
.sln
file. Using this command:dotnet new sln
. - Add our
.csproj
or project to the solution file. Using this command:dotnet sln add ThreadService
. - Verify your project structure. Your project structure should be like the project stucture section.
- Please check your project completeness by building the project. Run this command:
dotnet build
.
You can check the final result will be like this.
Repository
bervProject / forum-api-microservices
Forum API Microservices
forum-api-microservices
Forum API Microservices
Directory Structure
-
/app : Microservices Source Code
- Currently we have Auth Service, User Service, and Thread Service.
- docker-compose.yml : Containerize MongoDB & Redis. Will help for development.
Microservices Development
- You will need to copy or modify
docker-compose.yml
to ignore the deployment of microservices. - Run Redis & MongoDB using
docker compose up -d
. - Go to the microservice you want to update and read the README.md of each directory to understand how to run them.
Development
- Build Images of Microservices:
docker compose build
- Run all:
docker compose up -d
Software Architecture
License
MIT
MIT License
Copyright (c) 2022 Bervianto Leo Pratama's Personal Projects
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute,
…What's Next?
We will continue to build User Service in the next part. So please navigate to the next part.
Top comments (0)