Your first microservice

During this tutorial, we’ll try to explain how we can create microservices using many useful tools and from scratch.

We follow the plan below during this demonstration :

  1. Create customer service (microservice).
  2. Create configuration service to centralize services configurations  .
  3. We  create a service discovery to register our service and where we use eureka discovery.
  4. And finally we try to create a proxy to manage multiple instances of our microservices. 

1-Create customer service (microservice).

Here we go!!The first step is to create the java project using springboot.So to do that we can use an IDE that embads Spring tool or create the project on the web site

For our case we use spring tool suite to create our project like shown here:


We specify the group and the artifact id:


This is an important step where we have to chose all dependencies that we need in our microservice. Below all dependencies that we must add and a description of each one:


  • Web: Support for full-stack web development, including Tomcat and spring-webmvc
  • H2: Embedded database.
  • JPA:(Java persistance API) is  a java application programming interface specification that describes the management of relational database in applications using JAVA.
  • Actuator: provides production ready features to help monitor and manage applications.
  • Config client: Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments
  • Eureka discovery : Is a service registry where the service registers itself  and tells the registry where it lives (host, port, node name…). This registry is  built and  open-sourced by Netflix.
  • Stream Rabbit:  Spring Cloud Stream is a framework for building message-driven microservices . Spring Cloud Stream builds upon Spring Boot to create DevOps friendly microservice applications and Spring Integration to provide connectivity over messaging middleware such as Apache Kafka and RabbitMQ. In our case we use RabbitMQ.By adding @EnableBinding to your main application, you get immediate connectivity to a message broker and by adding @StreamListener to a method, you will receive events for stream processing.
  • Zipkin Client: Zipkin Zipkin is the Twitter open-source implementation of Google’s distributed tracing system,Dapper. It’s a great tool for people who wants to understand the bottleneck in their multi-services system.We 
  • REST Repository: Spring Data REST is itself a Spring MVC application and is designed in such a way that it should integrate with your existing Spring MVC applications with very little effort. An existing (or future) layer of services can run alongside Spring Data REST with only minor considerations.

We find out with the project below :


What we need know is to create the customer entity.So for this we create Class Customer in package com.exemple.entity:

package com.exemple.entity;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Customer implements Serializable {

 private static final long serialVersionUID = 1L;
 private Long id;// id
 private String name;// name

 public Customer(String name) {
 super(); = name;

 public Long getId() {
 return id;

 public void setId(Long id) { = id;

 public String getName() {
 return name;

 public void setName(String name) { = name;

 public String toString() {
 return "Customer [id=" + id + ", name=" + name + "]";


Now we create customer Repository in package com.exemple.dao :

package com.exemple.dao;

import com.exemple.entity.Customer;

public interface CustomerRepository extends JpaRepository<Customer, Long> {


Override run method to store data in database and print it like shown in the code below:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import com.exemple.dao.CustomerRepository;
import com.exemple.entity.Customer;

public class CustomerServiceApplication {
public static void main(String[] args) {, args);
 class DummyCLR implements CommandLineRunner {</span>
<span style="color: rgb(0, 0, 0);" data-mce-style="color: #000000;">
 private final CustomerRepository customerRepository;

 public DummyCLR(CustomerRepository customerRepository) {
 this.customerRepository = customerRepository;

 public void run(String... arg0) throws Exception {
       Stream.of("Appel", "Miscrosof", "John", "Ahmed", "Spring").forEach(c -&amp;amp;gt; Customer(c)));



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s