Microservices are the cool new kids on the block. Whether you’re not yet sold on the idea of a distributed system or if you just can’t get enough of it, I’ve got you covered. This post is a quick start guide to get you up and running with microservices in Java. The task isn’t necessarily easy for me, though. Microservices can model all kinds of real world structures. They’re a perfect example of Conway’s Law in execution.

That means that everything could go great when transitioning to a distributed system, provided that you and your organization have already decided that a microservices architecture is ideal. But things can also go haywire very easily, and you could end up maintaining multiple hosts, deploying multiple codebases, dealing with complicated logging, and supporting different executions. We’ll focus on the bright side this time. As we move along, I’m going to reinforce some of the concepts related to Java microservices that you may or may not already be familiar with.

Today, we’re going to create a very simple boilerplate to help you get started. It’s simple enough to get you up and running quickly, but it should offer enough content to prove that it works. This initial setup will consist of a service discovery (more on that later) and a concierge service that’ll welcome us in.

Microservices Java signified by boxes with coffee cups

Hit the Ground Running

First things first, we need some tools! Java is our programming language of choice today. IntelliJ, Eclipse, or your favorite IDE will be our wingman.

Since Java is our weapon of choice, we’ll use Spring as our base to get up and running. With Spring as our foundation, we can be pretty sure we’re in for a smooth flight. Briefly, though, if you don’t know what Spring is, you’re missing out. Spring is a framework that streamlines a lot of the boilerplate that normally comes with setting up a Java project. If you’re building microservices in Java, it’s very important to have the right tool set.

As for myself, I’m going to be setting up a new Spring project using IntelliJ.

In the next screen, I’m going to be adding one of Netflix’s greatest creations (after Sense 8): Eureka. Before that though, I think it’s time for a quick knowledge check. Microservices by themselves are no good if there isn’t a way to track which one is active and which one isn’t. Moreover, in complex systems, keeping a list of services available becomes increasingly important.

Picture high-availability systems with global replication. How would you keep track of the same service deployed at five different locations? How would you load balance requests between them? That’s complicated—unless, of course, you have a service that discovers other services as well as load balances between them. Netflix faced the same issue, and the solution they came up with was Eureka. That’s what we’ll use today.

Here’s what that first setup screen looks like. A Spring project offers a number of dependencies that go from database management to sessions and security. The ones we’re interested in are mentioned below.

And that’s it! Let’s do some coding.

Services Discovery

The first thing we have to do is create a service discovery class. Normally, this is one of the most complex aspects of handling microservices in Java. As I said above, there’s a lot that can go into a full-service microservices architecture. So, let’s take a look at how much Spring can automate that for us.

And that’s it. That didn’t take long whatsoever.  I gotta admit, I was very surprised when I first saw how easy it was. Let’s go over some of the items in there:

  • @EnableEurekaServer marks your module as a Eureka Server, basically tying your microservices together.
  • The @SpringBootApplication annotation attempts to use the Spring auto-configuration services, based on the dependencies you’ve added.

Once that’s done, we need to configure the actual settings for the server, which is pretty quick. When the application boots, it looks for an applications.properties file, which should be blank the first time you open it. The settings in there should be simple and straightforward. For the time being, this is all we need:

Running the Project

There are a couple of ways to run the project. The first method is to open up a terminal session in your favorite OS. Then, navigate to the directory you’re using to store the project and run the following:

#Windows
mvmw spring-boot:run
#Mac OS/Linux
./mvmw spring-boot:run

For me (and this is probably your case as well), I just hit that run button on my IDE and waited for the magic happen. However you do it, the console should display a lot of information, beginning with the Spring details and the information of our base DiscoveryService. I’d like to talk about a few things showing up in the console output before we move on.

  • Tomcat initialized and Starting service. Make sure this line is displaying the port you previously set. In our case, it’s port 6969. If not, you may need to go back to the applications.properties file and double-check the settings.
  • Initiating Jersey application. What’s a Jersey application? Jersey is a library that will assist us with the REST API. It’s not something you need to manually include, as it’s already there as part of the Maven dependencies.

Once all of that finishes, if you visit your localhost with the port you set (in my case http://localhost:6969/), you’ll see the Spring dashboard. We’re onto something here!

This interface provides an overview of the overall health of the system and any other notifications we may need to know about. Below those (not seen in the screenshot) are additional metrics that are relevant to the server, such as memory, memory usage, uptime, replicas information, and more.

One key item to note here is that the instances panel is still empty, which means that we haven’t created an instance (a microservice) that connects to our discovery. Let’s address that quickly now.

Concierge Service

A concierge service sounds fancy all right. Now, that we’ve created a discovery service, it’s time to create an actual service that connects to it.

Our little concierge service will be very simple. It’ll respond to a specific URL, which we’ll pass a name to. Despite being very simple, this example will give us an opportunity to cover a few aspects of microservices that are specific to the latest version of Spring.

For our microservice, it’s important to mention that it needs to be a different project or codebase. It may seem obvious, but that’s one of the questions I had myself when I started working with microservices. That doesn’t mean that we can’t split our project in modules or that several services can’t run on the same host. They can. We’re just trying to walk before we run.

So, to create the concierge service, create a new project, make sure you include Eureka again, and write a simple class.

First, we should create a simple server-class. This isn’t complicated at all, and a few lines will get us going.

This isn’t anything we haven’t seen before. Our concierge module will also require its own settings. Remember when we set up the Eureka server earlier? This is a similar setup. Open the application.properties file and add a few settings:

#Microservice server settings
spring.application.name=concierge-service
spring.freemarker.enabled=false
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=/templates/

eureka.client.serviceUrl.defaultZone=http://localhost:6969/eureka/
server.port=2222

#sandbox security settings NOT PRODUCTION READY
spring.security.user.name=user 
spring.security.user.password=pass

One really cool thing about Spring version 2.0, which is reflected in the above code, is that security is enabled by default. So when you visit the microservice, it now includes a nice login page by default. That’s why I’ve explicitly added a set of credentials. Also, notice that we’re using a different port this time.

Next in line is a class that actually does something. Our concierge class will receive a name from the URL and display it.

I won’t go into too much detail into what’s happening here, but notice that Spring annotations will do much of the legwork for you, such as mapping a URL request to a method and indicating a REST response instead of a view.

So, it’s time for the moment of truth. I’ll be running the discovery service first and then the concierge service.

Boom! There’s our service, and it’s automatically tracked and visualized! Pardon my excitement; it’s amazing how easily that worked. Surely enough, if we visit the service at http://localhost:2222/greeting/john, we’ll get a response and a warm welcome.

Microservices in Java: Wrapping Up

That’s how you create your first microservice architecture in Java. It didn’t take long at all, but there sure are a lot of different steps. This is just setting up. We haven’t gone through the migration from a monolith or server requirements. Also, we haven’t discussed logging. By now you should know the importance of keeping all your services looked upon. But will you check dozens of logs from each server? Will you set up your own Syslog server?

Of course not. What you should do is get your company and your brand new microservices approach the right tools to get your microservices going. Scalyr has you covered there. Your DevOps team will appreciate it.

There’s so much left to cover, and I wish I could make this post go on forever. We haven’t discussed database patterns! Do we split the database or the code first? Who knows? How big can Java microservices be? There are so many questions still, but time’s up. You don’t need me for that. It’s on you now; it’s time to break that monolith. Good luck!

This post was written by Guillermo Salazar. Guillermo is a solutions architect with over 10 years of experience across a number of different industries. While his experience is based mostly in the web environment, he’s recently started to expand his horizons to data science and cybersecurity.

Comments are closed.

Jump in with your own data. Free for 30 days.

Free Trial