In this module We will create posting microservice system and deploy it using Docker.
During this task we'll need to implement the next two services:
- User service
- Post service
And add interaction between them.
- User service is CRUD service for managing users. It should use persistent storage for storing users
Service definition should be next:
| GET /greeting | Simple API for testing purpose | ||||
| Response | Body | Description | Code | ||
| "Hello, k8s!" | 200 – OK 500 – Internal server error occurred. |
||||
| POST /users | Uploads a new user | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| { "username": "some-username" } |
Username of user to be created | ||||
| Response | Body | Description | Code | ||
| {
"id": 123, "username": "some-username", "amountOfPosts": 0 } |
Long id – ID of created user String username – username of created user String amount – amount of posts for the user |
200 – OK 400 – Validation error or request body is an invalid 500 – Internal server error occurred. |
|||
| GET /users/{id} | Gets user’s data | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| Long id | Id of user to get | Id of existing user | |||
| Response | Body | Description | Code | ||
| {
"id": 123, "username": "some-username", "amountOfPosts": 0 } |
Long id – ID of created user String username – username of created user String amount – amount of posts for the user |
200 – OK 404 – User doesn’t exist with given id 500 – Internal server error occurred. |
|||
| DELETE /users/{id} | Delete a user | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| Long id | Id of user to delete | Id of existing user | |||
| Response | Body | Description | Code | ||
| 200 – OK 404 – Not Found 500 – Internal server error occurred. |
|||||
| PUT /users/{id} | Update user’s data | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| Long id | Id of user to update | Id of existing user | { "username": "some-other-username" } |
String username – new username for the user | |
| Response | Body | Description | Code | ||
| {
"id": 123, "username": "some-username", "amountOfPosts": 0 } |
200 – OK 400 – Validation error or request body is an invalid 404 – User doesn’t exist with given id 500 – Internal server error occurred. |
||||
- Post service is CRUD service for managing posts. It should use persistent storage for storing posts
Service definition should be next:
| GET /greeting | Simple API for testing purpose | ||||
| Response | Body | Description | Code | ||
| "Hello, k8s!" | 200 – OK 500 – Internal server error occurred. |
||||
| POST /posts | Uploads a new post | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| { "authorId": 123, "text": "Hi friends, I recently watched amazing movie at the cinema" } |
Data of post from specified user | ||||
| Response | Body | Description | Code | ||
| { "id": 12, "authorId": 123, "text": "Hi friends, I recently watched amazing movie at the cinema", "postedAt": "28-09-2022" } |
Long id – ID of created post Long authorId – ID of a user who created a post String text – text of the post Date postedAt – date when the post was created |
200 – OK 400 – Validation error or request body is an invalid 500 – Internal server error occurred. |
|||
| GET /posts/{id} | Gets post’s data | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| Long id | Id of post to get | Id of existing post | |||
| Response | Body | Description | Code | ||
| {
"id": 12, "authorId": 123, "text": "Hi friends, I recently watched amazing movie at the cinema", "postedAt": "28-09-2022" } |
Long id – ID of a post Long authorId – ID of a user who created a post String text – text of the post Date postedAt – date when the post was created |
200 – OK 404 – Post doesn’t exist with given id 500 – Internal server error occurred. |
|||
| DELETE /posts/{id} | Delete a post | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| Long id | Id of post to delete | Id of existing post | |||
| Response | Body | Description | Code | ||
| 200 – OK 404 – Not Found 500 – Internal server error occurred. |
|||||
| PUT /posts/{id} | Update posts’s data | ||||
| Request | Parameter | Description | Restriction | Body example | Description |
| Long id | Id of post to update | Id of existing post | { "text": "Hi friends, I changed the topic of my post" } |
Text of post to be updated | |
| Response | Body | Description | Code | ||
| { "id": 12, "authorId": 123, "text": "Hi friends, I changed the topic of my post; note t", "postedAt": "02-10-2022" } |
Long id – ID of updated post Long authorId – ID of a user who created a post String text – text of the post Date postedAt – date when the post was updated |
200 – OK 400 – Validation error or request body is an invalid 404 – Post doesn’t exist with given id 500 – Internal server error occurred. |
|||
When a new post is created or deleted, numberOfPosts for user should be changed. We should implement a call from post service to user service.
- Create Dockerfiles to package your applications as docker images.
- Push these local images to the own public registries in https://hub.docker.com/. Specify version of your container (f.e. 1.0.0).
- Create a docker-compose file that would run all containers for your microservice application. Add init scripts for the database to run when container starts up. Once you have a compose file, you can create and start your application containers with a single command:
docker-compose up.
Note: Set Database url, username and password via environment variables for your database and application containers.
This is a brief guide to help you get started with the Posting Microservice System, including building the service, creating a Docker image, and running the service in a container.
- Java
- Maven
- Docker
- Pull these docker images from the public registries in https://hub.docker.com/. i) https://hub.docker.com/r/santanubarua/post-service ii) https://hub.docker.com/r/santanubarua/user-service
- Run the docker-compose file
docker-compose up.
Note: Set Database url, username and password via environment variables for database and application containers.