Create a microservices application architecture with Spring Boot and Spring Cloud - Part 3 - Discovery service

Introduction

The discovery service’s main purpose is to help us manage the services within a microservices application architecture.  There are a couple of approaches: client-side service discovery and server-side service discovery. An example of a server-side service discovery would be a DNS server that is configured to do what a client-side service discovery would do, which is to find services and keep track of them.

Overview

The discovery service in this application will be a client-side service discovery instead of a server-side service discovery.  We’ll be using Eureka to be our discovery service. More information can be found here at https://github.com/Netflix/eureka.

 

The following 3 services: proxy, General, and Minnesota; will eventually be registered with Eureka and be tracked by it after we create them later on in the tutorial.

Tutorial

1.)  Create a new Spring Boot app called, discovery_service. Ensure that Gradle is used, which should be selected by default anyways.

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.boot:spring-boot-starter-actuator')
	compile('org.springframework.cloud:spring-cloud-starter-sleuth')
	compile('org.springframework.boot:spring-boot-starter-web')
	implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
	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 DiscoveryServiceApplication.java.

7.)  Add the following package import to the import section at the top for that class:

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

@EnableEurekaServer


 

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

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

spring:
 application:
   name: discovery-service
 cloud:
   config:
     uri: http://localhost:8088

Explanation

  • spring.application.name is the name we give the service.  This value correlates with the name of the config file from the config server.
  • spring.cloud.config.uri is the URL at which the config server can be reached.  This is especially important so that this microservice can get the config file.

 

11.)  Go back to the Config Server project.

12.)  Create a directory called, config, in /src/main/resources.  The full path for the new directory will then be /src/main/resources/config.

 

13.)  Then inside of the new directory, config, create a new file called, discovery-service.yml, in which we will be inserting some config properties for our discovery service(s).

 

14.)  Next, inside of discovery-service.yml, make sure the following properties are inserted:

server:
 port: 8061
eureka:
 instance:
   hostname: localhost
 client:
   registerWithEureka: false
   fetchRegistry: false
   serviceUrl:
     defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Explanation

  • In this config file, we're assigning the port 8061 to be where our discovery service will be located.  The defaultZone property is set to allow us to access the Eureka service page later to look at what services are registered.

15.)  Start up the discovery service.  It should pick up the configuration properties from the config server without you needing to restart the config server.

16.)   If you go to http://localhost:8061/ you should now see a page for Eureka in which you can check which services it has detected.  Right now it should show no services.

Code Repository

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


Next tutorial part...

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