Microservice – Find Book Service using Spring Boot

What you will learn here about Microservices

  • Microservice – Find Book Service using Spring Boot

microservices

prerequisite:
1)Please Start Naming server Before starting the Find Book service. Please visit following link for knowing how to implement naming server in microservice

Eureka naming server in microservices

2)Please Start Book Exchange service Before starting the Find Book service. Please visit following link for Book exchange service

Microservice – Book Exchange Service using Spring Boot

Please click on following link to download Find Book Service project.

Download Find Book Service project (9 downloads)

Microservice – Find Book Service using Spring Boot

Please follow the following steps to know to how to consume simple microservice using openfeign.
1)First create a simple maven project

2)Add following dependency in pom.xml which is shown below

Spring boot starter dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring boot open feign dependency

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

Eureka client discovery dependency

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

Dev tools dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>runtime</scope>
	<optional>true</optional>
</dependency>

open feign dependency

3)Please adding following configurations in application.properties of find book service which is shown below

spring.application.name=find-service
server.port=8100

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

naming server configuration in properties file

4)Please use @EnableFeignClients annotation to enable openfeign clients which is shown below

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class FindBookServiceApplication {

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

}

enable feign clients spring boot

5)Now please create Book entity which is shown below

import org.springframework.stereotype.Component;

@Component
public class Book {

	private int id;
	private String book_Name;
	private String author;
	private String description;
	private double price;
	private String environment;
	
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	public String getEnvironment() {
		return environment;
	}
	public void setEnvironment(String string) {
		this.environment = string;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBook_Name() {
		return book_Name;
	}
	public void setBook_Name(String book_Name) {
		this.book_Name = book_Name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
}

book entity

6)Now please create feign client to consume another microservice using openfeign client which is shown below

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "exchange-service", url="localhost:8000")
public interface ProxyService {
	
	@GetMapping("book/id/{id}")
	public Book getBook(@PathVariable int id);

}

feign client spring boot

7)Now please create rest controller to get data from feign client which is shown below

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FindBookController {
	
	@Autowired
	ProxyService proxy;
	
	@GetMapping("book/id/{id}")
	public Book getBook(@PathVariable int id) {
		return proxy.getBook(id);
	}

}

feign client rest controller

8)Now please run the find book service

9)Now please go to browser and visit URL http://localhost:8100/book/id/1001 which is shown below
consume microservice using feign

You may also like...