Create a microservices application architecture with Spring Boot and Spring Cloud - Part 4 - Business domain services (Minnesota microservice)

Introduction

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.

Overview

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.

Tutorial

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 {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

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 {
	mavenCentral()
	maven { url "https://repo.spring.io/milestone" }
}

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

dependencies {
	compile('org.springframework.cloud:spring-cloud-starter-config')
	compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
	compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
	compile('org.springframework.cloud:spring-cloud-starter-openfeign')
	compile('org.springframework.cloud:spring-cloud-starter-sleuth')
	implementation('org.springframework.boot:spring-boot-starter-jersey')
	implementation('org.springframework.boot:spring-boot-starter-web')
	testImplementation('org.springframework.boot:spring-boot-starter-test')
}

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:

@EnableDiscoveryClient
@EnableSwagger2

 

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

 

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. 

@Bean
public Docket swaggerApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
            .apis(RequestHandlerSelectors.basePackage("com.bryanlor.controllers"))
            .paths(PathSelectors.any())
        .build()
        .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:

@RestController

Code Explanation

  • @RestController
    • 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.

@GetMapping("/greeting")
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:

spring:
 application:
   name: minnesota-service
 cloud:
   config:
     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:

server:
 port: 9091
eureka:
 client:
   serviceUrl:
     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)