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 :
- Create customer service (microservice).
- Create configuration service to centralize services configurations .
- We create a service discovery to register our service and where we use eureka discovery.
- 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 https://start.spring.io/.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 DevOpsfriendly 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:- Web: Support for full-stack web development, including Tomcat and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.exemple.entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Customer implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id;// id private String name;// name public Customer(String name) { super(); this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + "]"; } } |
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.example; import java.util.stream.Stream; 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; @SpringBootApplication public class CustomerServiceApplication { public static void main(String[] args) { SpringApplication.run(CustomerServiceApplication.class, args); } @Component class DummyCLR implements CommandLineRunner {</span> <span style="color: rgb(0, 0, 0);" data-mce-style="color: #000000;"> private final CustomerRepository customerRepository; @Autowired public DummyCLR(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } @Override public void run(String... arg0) throws Exception { Stream.of("Appel", "Miscrosof", "John", "Ahmed", "Spring").forEach(c -&amp;gt; customerRepository.save(new Customer(c))); customerRepository.findAll().forEach(c-&amp;gt;System.out.println(c)); } } } |
TAGS :
COMMENTS