Creating a Java microservices architecture from scratch – CM000

by Nov 15, 2016




DeegeU Java Course

The “Creating a Java Microservices Architecture from Scratch” video is part of a larger microservices tutorial. This is the first video, introducing you to the series.

Transcript – Creating a Java Microservices Architecture from Scratch?

Hi! Over the next few videos, we’re creating a trivia question microservice from scratch, end-to-end. That means all the code, and all the infrastructure to build, test, deploy and run. To start, this video is an road map for the microservice we’re going to create. It will give you an idea for all the technologies we’ll use to create our microservice.

This tutorial demonstrates a way to create a microservice. This isn’t THE way. There are many solutions to this problem, but hopefully this will give you enough direction to start creating your own microservices.

These videos will also diverge from how other tutorials work on DeegeU.com. Every video in this playlist builds on the previous video. We’re building something bigger, and we can’t do it in one video. So let’s get started!

Usecases for our Java microservice

Imagine we’re creating an online trivia game. The game will likely need to handle many tasks. We’ll need code to handle our users, code to get new questions, code to handle processing answers, and probably several other functions not listed here. Each function could map to it’s own microservice.

To start, we’ll focus on the trivia questions service. What we learn from the trivia questions service can apply to any other services we need to create later on down the road.

Our trivia question microservice will handle the trivia questions for our game. Let’s list out some of the use cases we’ll need for our microservice. The usecases matter because they describe the API we need to create.

For starters, we’ll need to get a question. This could be a particular question, or maybe we need a random question. We’ll do both. We could expand this to get questions from a category, or other more complex use cases, but we’ll keep things simple for the tutorials.

We’ll also want to be able to get the list of questions. This will simply return the list of questions available. We might have a million questions, so we won’t return all of the questions at once. We’ll page the number of questions and keep it at a reasonable amount. Our microservice API will also need to tell the user how to navigate forwards and backwards in the list of questions.

We’re going to put all of this inside a continuous integration process. If we’re constantly building and deploying our applications, we can easily lose track of which version is running in production. This means our microservice needs to be able to tell us which version is running. We’ll add a call that returns the version information for the microservice.

These calls will make up the API for our microservice. Since I just mentioned continuous integration, lets talk about how that’s going to look when we’re all set up.

Continuous integration for our Java microservice

Our continuous integration system will look like this. We’ll use a git repository to manage our version control. In git, we’ll add a trigger for when we issue a pull request to notify a build server to build our code. This will build our microservice, run our tests, package it up and store it in an artifact manager, and then deploy it to production.

We need to make sure we have unit tests in place for our microservice. These will protect us from deploying broken code. Since we’re storing every build in an artifact manager, we can roll back to previous versions just in case we deploy something bad.

This all happens automatically, so that’s why it’s important we include testing and version information in our microservice. We need to know what is running, and what code was used to create it.

You can use almost any programming language to create microservices, but we’re going to use Java for these tutorials. All of the concepts still apply for any language you use. We’ll use Maven to describe how to build our microservice. If you’re unfamiliar with Maven, you might want to check out the Maven playlist. It’s listed below.

REST API and WildFly Swarm

Our trivia microservice will expose a REST API that returns trivia questions in a JSON format. Almost every language knows how to make HTTP calls and process JSON text.

We’re going to implement our microservice using Wildfly Swarm. That’s a framework for creating Java microservices that can run independent of a Java EE container. Technically, we’re putting the JEE container inside our application. If you’re uncertain what a Java EE container is, that’s ok. The short story is in the past we ran web applications inside a web container like Wildfly or Websphere. That makes our microservices unnecessarily complicated, and we end up deploying much more than just our application. Wildfly Swarm hides all of this inside a jar. At the end of the day, our application is just a jar, but we still get most of the benefits of running in a Java EE container. There’s a bit more to that, but that’s all we really need to know now.

Deploying our app in a Docker container

Our microservice will be created in a Docker container. This will ensure that what runs on our development machine matches what is deployed in production. It will also make it very easy to provision more instances of our microservice as needed. We will cover preparing our application to run in a container, and building the application into a container.

Finally we need to deploy our Docker container to run somewhere. We’re going to run it on AWS, but you can configure your build to deploy to where ever you run your Docker containers.

Conclusion

So hopefully this gives you a taste for everything we’re going to cover. It’s pretty exciting! We’ll cover each topic in depth as we build our service. If you have any questions, let me know. For some of the bigger questions, I’ll add them to the end of these tutorials. You can find the code on GitHub, and the complete transcripts on DeegeU.com.

<p><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- DeegeU - Right Side -->
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-5305511207032009" data-ad-slot="5596823779"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>

Tools Used

  • Java
  • NetBeans
  • Apache Maven
  • WildFly 1.0.0.final
  • Jenkins
  • Docker
  • AWS

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Music:
Airport Lounge – Disco Ultralounge by Kevin MacLeod is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/by/4.0/)
Source: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100806
Artist: http://incompetech.com/

Get the code

The source code for “Creating a Java microservices architecture from scratch” 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.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

<h2>Don't miss another video!</h2> <p>New videos come out every week. Make sure you subscribe!<br><script src="//apis.google.com/js/platform.js"></script></p> <div class="g-ytsubscribe" data-channel="deegeu" data-layout="full" data-count="default"></div> <p></p>

Comments

comments

DJ Spiess

DJ Spiess

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.

Pin It on Pinterest

Share This