Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building Microservices With Netflix OSS, Apache Kafka and Spring Boot – Part 1: Service Registry and Config Server

DZone's Guide to

Building Microservices With Netflix OSS, Apache Kafka and Spring Boot – Part 1: Service Registry and Config Server

This project will show you how to build a registration system that will send a confirmation mail to new users, using Netflix OSS, Apache Kafka, and Spring Boot.

· Microservices Zone ·
Free Resource

The new Gartner Critical Capabilities report explains how APIs and microservices enable digital leaders to deliver better B2B, open banking and mobile projects.

As microservice architecture projects gained popularity over the last few years, the possibility to work on such a project logically appeared in our team. Knowing the process of building microservices becomes a must. In the following article, I'm going to summarize the knowledge I got through the last few months of research and development of microservice projects.

Using Netflix OSS, Apache Kafka, and Spring Boot, I'm going to build a relatively simple system mixing the two main ways of communication between microservices: REST and messaging. You can find all projects that I will build in Git.

The Solution

The goal is to build a registration system that will send a confirmation mail after a new user is registered. The following components will be part of this system:

1. Service registry (Eureka) - Where all services will register themselves.

2. Config server (Spring Cloud Config) - Where all services will take their configurations from. Config server will keep configuration files in the git repository.

3. Gateway (Zuul) - Will redirect all the requests to the needed microservice.

4. User service - Using this one, new users will register. On every new registration, the User service will send a message "USER_REGISTERED" to the message broker (Kafka).

5. Email service - Using this one, we will send emails. On "USER_REGISTERED" message received, the Email service will send a confirmation email to the new user.

Service Registry (Eureka)

In an environment with lots of services able to communicate with each other, they will need to know each other (their network addresses). At first glance, you may think that keeping this in the configuration files may do the job, and it will, if you stick with 2, 3, or 4 services. But maintaining 30, 40, or even 100+ services will bring you a configuration hell, and with cloud‑based microservice applications, this will get even worse.

Here, the service registry comes to help. Its purpose is to store the network locations of service instances. Netflix Eureka is a good example of such a registry where service instances register to the registry. Eureka, for its part, pings the registered services every 30 seconds to verify that they are up and running.

Implementing such a registry with Spring Boot and Netflix OSS stack is an easy task. Create a new project ( ms-discovery) with SPRING INITIALIZR and add a "Eureka Server" dependency. We should obtain the spring-cloud-starter-eureka-server dependency in the pom.xml automatically.

/pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

Then just add the @EnableEurekaServer annotation in the main application *Application.java file. This annotation will activate the Eureka Server related configuration.

/MsDiscoveryApplication.java
@EnableEurekaServer
@SpringBootApplication
public class MsDiscoveryApplication {
 public static void main(String[] args) {
  SpringApplication.run(MsDiscoveryApplication.class, args);
 }
}

Finally, tune the configuration files. In the bootstrap.yml, put only the name and the running port of the application.

/bootstrap.yml
server:
  port: ${port:8761}
spring:
  application:
    name: ms-discovery

In application.yml, put the standard application configuration. register-with-eureka and fetch-registry will prevent the registry from complaining that there are no replica nodes for it to connect to. You will need to change these when going to prod, but for local testing, it is okay.

/application.yml
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

"Short" Command Line Arguments

If you prefer using shorter commands to set configuration properties on the command line, you can use placeholders ${port:8761}:

java -jar ms-discovery-0.0.1-SNAPSHOT.jar --port=3232

instead of the standard

java -jar ms-discovery-0.0.1-SNAPSHOT.jar --server.port=3232

To test if everything is fine, build and run the project.

1. mvn clean install.
2. Go to the target folder.
3. Run  java –jar ms–discovery–0.0.1–SNAPSHOT.jar.
4. Check that Eureka is running on http://localhost:8761.

Config Server (Spring Cloud Config)

Any enterprise application will run in different environments (DEV, QA, PROD) and modify property files before the deploy is not welcomed. One of the solutions is the so-called externalized configuration pattern where the configurations are read from an external source. Pivotal's implementation of this pattern is the Spring Cloud config server. It works the following way: property files for each service and each environment are kept in the git repository. On startup, every service asks the config server for the proper configurations needed.

In the current solution, we are going to build the cloud config server as a discovery service client, so on startup, each microservice will take the config server location from the discovery service. Again, with the help of SPRING INITIALIZR, create a new project (ms-config-server) and add the "Config Server" and "Eureka Discovery" dependencies.

Except the ms-config-server, we need the git repository with the config files. We will name it ms-config-properties and it will have a tree structure containing the property files for every microservice and every environment:

ms-config-properties
ms-user
default
ms-user.yml
dev
ms-user.yml
prod
Ms-user.yml

Again, in the pom, we should automatically have spring-cloud-starter-eureka and spring-cloud-config-server.

/pom.xml
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

We need to modify the main *Application.java file by adding the @EnableEurekaClient annotation to enable Eureka discovery for clients. By default, we will look for the Eureka server running on http://localhost:8761 to register. We should also enable the config server that other applications will talk to, adding the annotation @EnableConfigServer.

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class MsConfigServerApplication {

 public static void main(String[] args) {
  SpringApplication.run(MsConfigServerApplication.class, args);
 }
}

By default, the config server runs on port 8888.

/bootstrap.yml
server:
  port: ${port:8888}
spring:
  application:
    name: ms-config-server

In the application properties, we configure the URI to the git repository and the way the config server searches in the repository. They are going to be kept in the ms-config-properties subfolder of the repository, so:

/application.yml
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/isilona/microservices/
          search-paths:
            - "ms-config-properties/{application}/{profile}"


To test if everything is fine, build and run the project.

1. mvn clean install.
2. Go to the target folder.
3. Run.
4. Check that Config server will return some of the configurations we have in the ms-config-properties folder: http://localhost:8888/ms-service/dev.
5. Check that Config server managed to register the Eureka discovery.

You can check out Part 2 and Part 3 of "Building microservices with Netflix OSS, Apache Kafka, and Spring Boot."

The new Gartner Critical Capabilities for Full Lifecycle API Management report shows how CA Technologies helps digital leaders with their B2B, open banking, and mobile initiatives. Get your copy from CA Technologies.

Topics:
integration ,microservices ,kafka ,netflix oss ,spring boot

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}