Skip to content

BoasAndreasen/Microservices

Repository files navigation

Microservice Documentation

Each microservice has one or more controller classes which handles the incoming HTTP requests, domain classes which represent the entities in a microservice, data transfer objects which are a subset of the domain classes, repositories which help store and access the domain classes, and event classes which include streams, consumers, and producers of Kafka events.

The video microservice is available at localhost:8080 and stores videos, hashtags, and users. In addition to the fields that each domain class stores including id and name, there are relationships between these classes which include that each video class has hashtags, viewers, likers, and dislikers, each hashtag has videos that it hashtagged, and each user has videos that he uploaded, watched, liked, and disliked.

The video microservice controllers have a number of endpoints that can be accessed by using the provided CLI. To achieve the functionality described in part 1.1, the user can go into the video-cli folder and run the following commands using ./gradlew run --args="command parameters". Add a user using “add-user ‘username1’”, add a video with username1 as the uploader and title1 as the title “add-video ‘title1’ ‘username1’”, add two hashtags “add-hashtag ‘hashtag1’” and “add-hashtag ‘hashtag2’”, add the hashtags to the video “add-video-hashtag ‘title1’ ‘hashtag1’” and “add-video-hashtag ‘title1’ ‘hashtag2’”, like a video “add-video-liker ‘title1’ ‘username1’, dislike a video “add-video-disliker ‘title1’ ‘username1’”, and watch a video using “add-video-viewer ‘title1’ ‘username1’”.

When a video is uploaded, a video-posted event is sent where key is the title of the video, and the values are the hashtags of the video. Similarly, a video-liked event is sent when a video is liked with key ‘title1’ and value [“hashtag2”, “hashtag1”], a video-disliked event is sent when a video is disliked, and a video-watched event when a video is watched. The user can now list all videos using “get-videos”, get videos of a specific user using “get-videos-uploaded ‘username1”, or get videos of a specific hashtag using “get-videos-hashtagged ‘hashtag1’”.

The trending hashtag microservice is available at localhost:8081 and it only stores hashtags with their like count and window start and end time. Instead of the CLI adding the hashtags, the microservice has a stream that listens to the video-liked topic and takes the set of hashtags, groups them by key, and counts the number of occurrences of each hashtag within a sliding time window of 1 hour and produces a video-liked-by-day event where the key is a WindowedIdentifier with a name, start time, and end time such as {"name":"hashtag2","startMillis":1705064965385,"endMillis":1705068565385}, and the value is the number of occurrences.

The microservice then has a listener that saves the values from video-liked-by-day to the hashtag repository. To get the top ten hashtags within the last hour, the user can go into the trending-hashtag-cli folder and use the command “get-10trendingHashtags” which because of the rolling window calls a query which looks at all the values in the database, groups those in the last hour by hashtag name, and selects and sorts the value with the highest like count which are then limited to 10 by the controller.

The subscription microservice is available at localhost:8082 and because it maintains a list of the hashtags of watched videos, it listens to video-watched events and stores new videos and hashtags from each event while updating the view count of already stored videos.

In addition, after adding a user using a username “add-user ‘username1’” and adding a hashtag using a hashtag name “add-hashtag ‘hashtag1’”, the user can subscribe to a hashtag using “add-subscriber ‘hashtag1’ ‘username1’” and unsubscribe using “delete-subscriber ‘hashtag1’ ‘username1’”. This produces a hashtag-subscribed event and a hashtag-unsubscribed event where the key is the newly subscribed or unsubscribed to hashtag, and the value is all the hashtags that a user has subscribed to. This is so that the hashtag recommendation microservice can operate on these events without collecting personal information.

To get the top 10 next videos to watch use “get-top-ten-by-username ‘username1’, the microservice gets all the hashtags that a user has subscribed to and then gets all the videos that the hashtag has been used in and sorts them by views and gets the top 10 but unfortunately does not filter out already watched videos due to http connection failures within the docker container. To get any results from the command, the views have to be added to the videos after the user subscribed.

The hashtag recommendation microservice has a listener that listens to the subscription microservice subscribe and unsubscribe events. To store this information anonymously, the data storage works like a HashMap with a HashMap inside it where the value in the second hashtag is the counter. To implement this in a relational database since it doesn’t support HashMaps, the data storage was implemented as three columns where the third was the counter and the others were the HashMap keys.

When a user creates two or more subscriptions in the subscription microservice after creating a user and hashtag using “add-subscription ‘hashtag1’ ‘username1’” and then “add-subscription ‘hashtag2’ ‘username1’”, the counter for hashtag2 in hashtag1 increases to 1 and the counter for hashtag1 in hashtag2 increases by 1. To get the count of the top 10 related hashtags, the command “get-ten-other-hashtags ‘hashtag1’” can be used.

structurizr-88950-Container-001