Microservice – Book Exchange Service using Spring Boot

What you will learn here about Microservices

  • Microservice – Book Exchange Service using Spring Boot

microservices

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

Eureka naming server in microservices

Please click on following link to download Book exchange service project

Download Book exchange Service project (668 downloads)

Microservice – Book Exchange Service using Spring Boot

Please follow the following steps to know to how to make simple microservice with inbuit H2 database.
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 data Jpa dependency

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

H2 database dependency

<dependency>
	<groupId>com.h2database</groupId>
	<artifactId>h2</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>

netflix eureka client dependency

3)Add following properties in application.properties which is shown below

spring.application.name=exchange-service
server.port=8000

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

spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.defer-datasource-initialization=true

h2 database properties

4)Create Entiry class with respective fields which is shown below

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

@Entity
public class Book {
	
	@Id
	@GeneratedValue
	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;
	}
}

microservice with h2 database

5)Create Jpa repository for Entity class which is shown below

import org.springframework.data.jpa.repository.JpaRepository;

public interface BooksRepository extends JpaRepository<Book, Integer>{

}

spring data jpa repository

6)Create data.sql under resources to insert data into H2 database which is shown below

insert into book(id, author, book_name, description, price) 
	values(1000,'Robert keyosaki','Rich Dad Poor Dad','Financial advice',300.50);
	
insert into book(id, author, book_name, description, price) 
	values(1001,'Joseph murph','Power of subconcious mind','Personality development',270.00);	

insert data into h2 database

7)Create rest controller which is shown below

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

@RestController
public class BookExchangeController {
	
	@Autowired
	BooksRepository repository;
	
	@Autowired
	Environment environment;
	
	@GetMapping("book/id/{id}")
	public Book getBook(@PathVariable int id) {
		
		Book book=repository.findById(id).get();
		book.setEnvironment(environment.getProperty("local.server.port"));
		
		return book;
	}
}

spring boot find book service

8)Now please start the your bookexchangeservice

9)Now go to browser and visit localhost:8000/book/id/1001 which is shown below
find book service spring boot

You may also like...