This post is part of the series ‘Microservices with Mo’:

Part 0: Intro

Part 1: Setting up docker

Part 2: The architecture

Last time we scratched our head a bit and thought about components of a microservice architecture. There we decided to start with implementing our first microservice which will be covering the business logic

Like already explained in the introduction to this series: This service will not be very clever — since this series is more about the surrounding of this service.

What we gonna do:

We gonna write an awesome microservice based on Spring Boot which will provide an REST Endpoint where our imaginary clients can GET how many requests the service already has received in prior to the user’s call. For the sake of simplicity this information will be since the restart of the service.

Let’s do this:

In general a good place to start a Spring Boot Microservice is http://start.spring.io/. Here we can easily select all the packages we need for our microservice. Since it is a very easy one we won’t need much. As you can see in the image below I have chosen a meaningful Group- and ArtifactId and also added following dependencies:

Web: Since we will be creating a REST-Interface we will leverage the integrated tomcat and the capabilities of WebMVC

Actuator: With this dependency our API will be extended with some endpoints like /info and /health to give information about our microservice.

DevTools: A great article about them with all the features listed can be found here. Tldr: They are a great set of tools which save a lot of time during developing by enabling Live Reloads for template engines, reload upon changes in the classpath and much more. Maybe they are a bit overkill since we will be not spending a lot of time developing this services but for me they became some sort of no-brainer.

start.spring.io with our Configuration

Now you can hit the Generate Project Button or use the shown hotkey. Shortly after that you should have a generated zip-File in your download folder of choice. This file contains a already prepared runnable Spring Boot Project.

I now created extracted that zip file and moved the extracted folder directly into our git repository. Also I copied the Dockerfile we prepared earlier from the template-service folder and adjusted jar-name. Mind that the default Jar contains the version number and therefore needs to be called counterservice-0.0.1-SNAPSHOT.jar . I hope that I will find a proper solution for that problem but until now it has to do it.

Now it is time to open the project in your favorite IDE. I will be using IDEA IntelliJ since it is my favorite and I could write a different story about how much I love it — but not here.

When we open the project we can see that there is already an Application class which is the starting point of our application. If we have a look into it we can see that it is annotated with @SpringBootApplication. This is were the magic happens. It enables SpringBootConfiguration, AutoConfiguration and the lovely ComponentScan. With this annotation the Application will automatically pick up any Component, Configuration and other spring related Beans which are hierarchical below the package in which the application class is located. Since we will be creating our packages just here — the scanner is able to pickup everything we create.

Since we want to create a REST-Endpoint we need to create a REST-Controller. I therefore created the package api and put a class called CounterController here.

There is not much to do here:

Add @RestController ,to the top of the class. Create a Request-Mapping for the endpoint. I prefer the more specific @GetMapping which is just an alias. In order to save the state we will use a static variable to count the number of requests we have dealt since the restart of the server.

We will be using an AtomicLong here instead of a long or Long since the access to the variable needs to be thread-safe in case there is more than one request at a time. Return and increase the variable after returning it

The code snippet below shows how our CounterController looks like:

@RestController

public class CounterController {



private static AtomicLong count = new AtomicLong(0);



@GetMapping(path = "/count")

public long getCount(){

return count.getAndIncrement();

}

}

Hint: Hope you forgive that we created an API which is solely returning a long instead of a proper JSON Response. But I think there are other tutorials out there how to properly introduce DTOs.

After doing just that we can start our SpringBootApplication by running the CounterServiceApplication class or by executing /mvnw spring-boot:run inside our counterservice folder.

If we go hit http://localhost:8080/count after starting it we will be served with the number of requests the server had in prior to our call. SUCCESS!

Cleaning up

Time to update our docker-compose.yml in the root folder of our git repository to now use our newly created microservice.

version: '3'

services:

counterservice:

build: counterservice

ports:

- "8080:8080"

And now we can run it with docker-compose up … Oh noes wait!

ERROR: Service ‘counterservice’ failed to build:

ADD failed: stat /var/lib/docker/tmp/docker-builder575431593 /target/counterservice-0.0.1-SNAPSHOT.jar: no such file or directory

Oh well, yes we could fix it with a fast mvn clean install inside the counterservice folder. But I think this is not very convenient when introducing other microservices. The cleanest solution would probably be to move the maven build also inside the container and start it there we did before.

The other solution we can go for is to create a multi-module maven project.

Maven allows multi-module projects which can be aggregates or source of inheritance. In our case we want to have a single pom.xml we can run to build all services we will be creating during this series. The pom.xml below is the one I put inside the root folder of our git repository.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.marcuseisele.springboot_microservices</groupId>

<artifactId>parent</artifactId>

<version>1.0.0</version>

<packaging>pom</packaging>



<modules>

<module>counterservice</module>

</modules>

</project>

Now a single mvn clean package inside the root of our git repository will also create our newly created microservice. After that we can again run docker-compose up and see our microservice booting up in all its beauty. Feel free to again check http://localhost:8080/count.

Recap & Outlook:

So what did we achieve in this part?

Besides creating our first service in this series we also modified the project to be a multi-module maven project. Also we updated the docker-compose.yml in a way to start our newly created service. Not a bad start I guess 😊

The next session will be covering the usage of a configuration service. Just looking at our current service I think I need to find a proper use-case for that one. At the moment our service does not use any configuration at all.

I am also looking forward to make the next session since I only know configuration services from architecture images and from reading tutorials. The services at my workspace rely on the deployment scripts to inject the proper configuration via environment variables.

As usual I am looking for feedback and don’t mind any questions if you get stuck. If you have any issues with the code and don’t mind having a peek at the intended solution: here is the branch I created for this session. Also feel free to raise your question in the comments below.

Thanks for following along!

Update: Link to next session — Config Service