Microservices platform with Spring Cloud – part 1

This is the first post in a series I intend to write about developing a complete platform based on microservices. This series will focus on implementing complete platform, including individual services ans supporting tools, such as logging, monitoring, security etc. In the series, we will use Spring Boot framework, along with Spring Cloud,  for building our platform.

The goal of this series is to provide an example on how to build complete system based on microservices. There are a lot of resources online that show how to build individual services or parts of the infrastructure, but I could not find a comprehensive documents on how to integrate the whole things together. Because, in the world of microservices, how you deploy, manage and monitor your services, is just as important as how you build it. I hope that this series will be useful in showcasing some simple scenarios common in microservices deployment.

Configuration server

For microservices-based systems, it is extremely important to have one central place to manage configuration, both common configuration options and on per-service basis. There are a lot of tools for this task, depending on environment, language and other options. Since we are using Spring Cloud, we will use configuration server it  provides out of the box.

This configuration server provides a lot of flexibility and features, which is out of the scope for this article. You can read in details how to set it up here. For this tutorial, we will use the simplest possible configuration required to showcase microservices in action.

Git repository for configuration files

Spring’s configuration server uses Git repository as a backend for storing configuration files. Using version control instead of plain files gives the benefit of auditing changes of configuration and easy rollback in case of misconfiguration. For the purpose of this exercise, we will create local Git repository and use it as a backend for configuration files:

In this snippet, we initialize Git repository in directory called config-repo . Then, we create a file called user-service.properties  and add an entry to it. Then, we commit changes. In this example, file user-service.properties  will be used later in the post to provide configuration for a service we will create.

Set up configuration server

Now that we have configuration backend ready, it is time to implement actual configuration server. Thanks to Spring Boot, this is as easy as creating a project and adding some annotations.

First, we will create Maven pom.xml  file and add required dependencies. Create the following POM in config server directory:

Next, create the main Spring Boot application file. Note the annotation we set. They tell Spring Boot that we want to create configuration server.

The final step is to set configuration properties for the server. Within configuration, we need to set the path to Git repository holding configuration files:

This configuration specifies that server will run on port 8888, use specified Git repo path and expect Eureka server to run on specified URL. The complete code for this part of tutorial can be found in Github.

Configuration  server exposes several endpoints through which clients can query configuration options. You can find detailed instruction in Spring Cloud documentation linked above. To see current configuration parameters for our service, invoke the following command, or paste the link in the browser:

This will query configuration for application called user-service and profile dev. Since we did not specify any profile information, configuration server will pull default options.

Eureka server

First part of our infrastructure is ready. Now it is time to set up service registry and discovery. We will use Netflix Eureka for this purpose, since Spring Cloud supports it out of the box. To set up Eureka server, we will simply create new Spring Boot application and set some annotations which will do the magic. First, we will need to setup Maven POM file, and add required dependnencies:

We will set some configuration options in application.properties file:

This configuration specifies that Eureka server will run on port 8761 on local machine, will not register itself with other Eureka instances and will not fetch registry information at startup.

The final step is to create Spring Boot application code:

That’s all there is to it! You can now build and run the application, and you have fully functional Eureka server. If you got everything right, you should get something like the following if you visit http://localhost:8761 :

Now, it’s time to make some actual services!

User service

We will create a simple service called user-service  (this will be application name). This will be extremely simple service, which will only be used to showcase concepts related to microservices development. It will allow user to signup using it’s name and email address. But, for now, we just want to create basic service and have it register with Eureka.

Microservice is just a Spring Boot application with some special dependencies and configuration. We will just need to create w few files. First, a Maven POM with dependnecies:

Here, we add dependencies which enable support for Eureka and configuration server. Next, we will set basic configuration for the service, like specifying service name and configuration server URL:

Finally, it’s time to code application main class. Just as with previous examples, we just need to set some annotations and Spring will do all the magic by itself.

Now we can start the complete system. Starting sequence should be: Eureka server, configuration server and finally user service. If you did everything correctly, and visit Eureka main page at http://localhost:8761 , you should see something like this:

Note that both configuration server and user service are registered with Eureka. Although in user service we did not specify  Eureka URL, it was picked up from configuration server and used for registration.

Using remote configuration

As a way to demonstrate how services pick up remote configuration parameters, we will create a simple controller in user service which will display configuration value we specified in Git repo when creating configuration server:

If you now restart user service and visit http://localhost:8800/greet , you should see the value of configuration parameter user.greeting  in the browser.

Messaging service

Finally, we will create another service called messaging-service , which we will use later in the series. This is exactly the same code as for user-service , except that server port and application name are different. Please refer to source code for details.

What next?

At this point, you have a fully functional microservices system. It is not very usefull, but it shows some important aspects, such as centralized configuration and service registration and discovery. You can clone the source code from Github, and play around with it.

In the next installment of the series, we will see how we can get microservices to interact with each other.

List of all the posts in this series

Leave a Reply

Your email address will not be published. Required fields are marked *