[Tutorial] Create a microservices application architecture with Spring Boot and Spring Cloud – Part 4 – Business domain services (Minnesota microservice)

April 30, 2019

For this part of the tutorial series, we’re going to focus on the first of 2 business domain services that will provide us with the responses we want when we go to the respective URL in the browser for this microservice application. We’ll be creating the Minnesota microservice in this part.


In the next couple of parts, we will be creating our business domain services.  I call these business domain services because they will be our microservices that will hold the business logic for our separate business domains, General and Minnesota.  For this specific part, we will focus our attention on the Minnesota microservice first.


The business domain services will be regular Spring Boot apps that implement the OpenFeign library in order for each service to also communicate with each other.

In the above diagram, data flow is shown to move between the services in which we will be using the Feign library to allow for that communication to happen.  Other data flow happens too but for simplicity sake, I’m only showing the flow between the 2 business domain services.


Minnesota Service

1.)  Create a new Spring Boot app called minnesota_service.  Make sure Gradle is used.

2.)  Open up the build.gradle file.

3.)  Remove the content that’s currently in the build.gradle file and then add the following to the file instead:

buildscript {
	ext {
		springBootVersion = '2.1.1.RELEASE'
	repositories {
	dependencies {

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.bryanlor'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
	maven { url "https://repo.spring.io/milestone" }

ext['springCloudVersion'] = 'Greenwich.RC2'

dependencies {
	compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"

4.)  Save the file.

5.)  Right click on the project and go to Gradle → Refresh Gradle Project.  You can also use the gradle commands in the command-line to build the project so that it pulls in the dependencies needed.


6.)  Open up MinnesotaServiceApplication.java and add the following import statements:

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

7.)  Then below @SpringBootApplication, add the following annotations and resolve any imports required for the annotation:


Code Explanation

@EnableDiscoveryClient Allows the service to register itself with the registry, in this case it would be Eureka.  We don’t need to use the specific annotation for Eureka because this one will use whatever implementation is on the classpath.

@EnableSwagger2 Enables Swagger 2 for the application/service.  For more information on implementation, check out https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api


8.)  Next, add the following method to the class so that Swagger will be able to build API documentation for this service.  Resolve any missing imports. 

public Docket swaggerApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(new ApiInfoBuilder().version("2.0").title("Minnesota API").description("Documentation Minnesota API v1.0").build());

9.)  Then create a new controller class called, HomeController, with a package signature of com.bryanlor.controllers.

10.)  Open up the new controller class you created.

11.)  In the package import section, import the following package:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

12.)  Then, above the class declaration, insert the following annotation:



Code Explanation


– A convenience method that combines the following Spring annotations @Controller and @ResponseBody.

– This annotation will have the controller class automatically serialize return objects for methods into HttpResponse.

13.)  Then in the class body, we’re going to add a simple controller method that returns a string.  Resolve any imports needed.

public String retrieveGreeting() {
    return "Welcome to Minnesota!";

Code Explanation

  • @GetMapping(“/greeting”)
    • Quick and simple way to annotate a HTTP GET request to a specific handler method.  This is mapping the URL path, “/greeting“, to the method retrieveGreeting().


14.)  Create a bootstrap.yml file in /src/main/resources/ if it doesn’t exist yet.

15.)  Open bootstrap.yml file and make sure it has the following properties:

   name: minnesota-service
     uri: http://localhost:8088

16.)  Go back to the Config Server project.

17.)  Create a new file called, minnesota-service.yml, in the following directory,  /src/main/resources/config/, in which we will be inserting some config properties for our Minnesota service(s).

18.)  Next, inside minnesota-service.yml insert the following properties:

 port: 9091
     defaultZone: http://localhost:8061/eureka/

19.)  Now, we move on to creating the General microservice.

Code Repository

Grab the code at https://github.com/lorbs28/bl-minnesota-service.

Next tutorial part…

Create a microservices application architecture with Spring Boot and Spring Cloud – Part 5 – Business domain services (General microservice)

You Might Also Like