Creating a Microservice Class Architecture – CM002
DeegeU Java Course
The “Creating a Microservice Class Architecture” video is part of a larger playlist called “Creating a Java microservice from scratch“.
Transcript – Creating a Microservice Class Architecture
Hi! In this video we’re going to take a quick look at the architecture for the our microservice we’re creating. It’s important to go over this so we can understand what the microservice does, and how the code is organized.
This is the third video in a larger playlist for creating microservices in Java.
We’re going to look at this in two steps. First we’ll cover the use cases for our trivia service. This will give us an understanding on what tasks the service handles. This will be mostly from the user perspective, where the user is likely some other system.
Next we’ll go over the class diagram for the code we’ll create. It will give us a map of how our service will operate. The class diagram also helps us understand how we’re going to organize our code.
Microservice Architecture Usecases
So let’s talk about the use cases. First off, someone calling our microservice might not know what calls can be made. One use case for our microservice is get a list actions we can perform. We’ll need to provide a directory of services available.
Our trivia microservice should be able to return trivia questions. We know we need to return a question. We need it to be both random, and we need a particular one.
We need to get a list of questions. This list will be the complete list of questions, and a smaller list. We will want the complete list for an admin tool, so we can view all the questions available. A smaller list of particular questions could be useful for a game where we want to select the question list in advance.
In both cases, we’ll page the results. If we have millions of questions in our database, we don’t want to return all of them in one call. We’ll return 10 at a time. We’ll also provide links for the client to request the next 10. We’ll add the use case, get navigational links for our lists.
We also need to know how many questions there are. This isn’t as important now, but later on when we add more features like categories, this will be more important.
This is a good list of use cases for now. We can add more later as our microservice becomes more mature. For now we just want to get things started, and our development process in place. We’ll expose each of these use cases in a REST API.
Microservice Class Architecture
We’ll start with our JAX-RS application. It doesn’t do much other than expose the REST endpoint “/trivia”. You can think of the TriviaApp class as the “main method” for our application. We’ll include our directory here. This makes sense, since this will be the only URL we guarantee. Clients can navigate our API from here.
Now let’s create a class that represents our trivia question. A trivia question should have, the question, four possible answers, the correct answer, and maybe a hint. We also want to give it an identifier and a date the question was last modified.
We’ll also create a builder for the trivia questions. This makes it a bit cleaner when we create the trivia questions, and it makes sure we never create incomplete trivia questions. I hate questions with no answers!
In order to get questions from our database, we need to create an class that handles our data access. Right now we don’t want to set up a full database, we just need something to test with. Later we’ll want to replace this object with something backed by a real database. To make our lives easier, we’ll create an interface with all the data tasks. This will be getting particular questions, getting random questions, getting a list of questions, and getting the total count. The interface will look like this.
We’ll then create a class adhering to this interface that just uses an array.
Finally we’ll have a class that exposes all these calls in REST endpoints.
That’s what we’re starting with. Of course there are many features we can add to our service. If there are features you want to see added, let me know and we’ll add more videos adding more features. For now, lets get this application build and in our continuous integration build process.
Thanks for watching the video. You can find the diagrams and the complete transcripts on DeegeU.com. Next we’ll take a look at the implementing our Trivia microservice. If you like what you’re seeing or have any questions, let me know in the comments or on DeegeU.com! Like and subscribe, and I’ll see you in the next tutorial!
- Class diagrams
All media created and owned by DJ Spiess unless listed below.
- No infringement intended
Dispersion Relation by Kevin MacLeod is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/by/4.0/)
Get the code
The source code for “Creating a Microservice Class Architecture” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:
git clone https://github.com/deege/deegeu-quizzer.git
Go to the Support > Getting the Code page for more help.
Don't miss another video!
New videos come out every week. Make sure you subscribe!
Your personal instructor
My name is DJ Spiess and I'm a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I've programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I've worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.