Commit 3ff6cb57 authored by Narendar Vakiti's avatar Narendar Vakiti

added feign client

parent f67e7324
This springboot application is for call rest services from BookStore API (https://gitlab.mynisum.com/nvakiti/bookstore-api.git)
**Below are the URL's of BookStore App**
1. Hystrix circuit breaker
* http://localhost:8082/getbooks
Used Hystrix circuit breaker. Fallback method will call to show some information to the user when rest service is down.
2. Used Spring RestTemplate
3. Used Feign Client
4. Added Eureka Client
5. Added to Zuul Proxy Server
Above Url access http://localhost:8083/getbookdetails BookStore-API Rest service
* http://localhost:8082/getauthor
Above Url access http://localhost:8083/getauthordetails BookStore-API Rest service
* http://localhost:8082/addbooks
Above Url access http://localhost:8083/addbookdetails BookStore-API Rest service
Same above URL's we can access through gateway application using below URL's
* http://localhost:8081/bookstore/getbooks
* http://localhost:8081/bookstore/getauthor
* http://localhost:8081/bookstore/addbooks
**Hystrix circuit breaker**
Here I user Hystrix circuit breaker. Fallback method will call to show some information to user when rest service is down.
when we try to access http://localhost:8082/getbooks **or** http://localhost:8081/bookstore/getbooks
internally http://localhost:8083/getbookdetails rest service will call, if the service is down then fallback method will call.
**Payload Request** for http://localhost:8082/addbooks
{
"bookId": 101,
"bookName": "Head First Java",
"bookPrice": 500.0,
"active": true,
"authorId": 1011,
"authorName": "Herbert Schildt",
"address": "USA"
}
......@@ -12,11 +12,13 @@
<artifactId>bookstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bookstore</name>
<description>Demo project for Spring Boot</description>
<description>Project for Spring Boot - BookStore</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
......@@ -24,6 +26,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
......@@ -48,6 +63,7 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
......
......@@ -2,10 +2,14 @@ package com.bookstore;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableHystrix
@EnableEurekaClient
@EnableFeignClients
public class BookstoreApplication {
public static void main(String[] args) {
......
package com.bookstore.bean;
import java.io.Serializable;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Getter;
......@@ -18,19 +19,17 @@ import lombok.ToString;
@Getter @Setter
@ToString @AllArgsConstructor @NoArgsConstructor
public class BookDetails implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1450252897638356990L;
private int bookId;
private String bookName;
private double bookPrice;
private boolean active;
private int authorId;
private boolean availability;
private String level;
private String rating;
private LocalDate publishedDate;
private String authorName;
private String address;
private String addedBy;
}
package com.bookstore.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor @AllArgsConstructor @ToString
public class ExceptionStatus {
private String message;
private String details;
private int statuscode;
private String url;
}
package com.bookstore.controller;
import com.bookstore.bean.BookDetails;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@RestController
public class AuthorController {
private static final Logger logger = LoggerFactory.getLogger(AuthorController.class);
/**
* Calling Author Rest API to fetch author details
* @author nvakiti
* @return
*/
@GetMapping("/getauthor")
public String getAuthor() {
String uri = "http://localhost:8083/getauthordetails";
logger.info("API URI "+uri);
String response = null;
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
response = result.getBody();
logger.info("Author Details Respose "+response);
} catch (Exception e) {
logger.error(e.getMessage());
}
return response;
}
}
......@@ -2,8 +2,11 @@ package com.bookstore.controller;
import java.util.Arrays;
import com.bookstore.bean.ExceptionStatus;
import com.bookstore.service.BookFeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
......@@ -28,6 +31,9 @@ import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
public class BookStoreController {
private static final Logger logger = LoggerFactory.getLogger(BookStoreController.class);
@Autowired
private BookFeignService bookFeignService;
/**
* Calling Books Rest API to fetch books details
......@@ -66,32 +72,7 @@ public class BookStoreController {
logger.info("Started fallback getBooksFallback() for /getbooks");
return "Service is not available, please try again later";
}
/**
* Calling Author Rest API to fetch author details
* @author nvakiti
* @return
*/
@GetMapping("/getauthor")
public String getAuthor() {
String uri = "http://localhost:8083/getauthordetails";
logger.info("API URI "+uri);
String response = null;
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
response = result.getBody();
logger.info("Author Details Respose "+response);
} catch (Exception e) {
logger.error(e.getMessage());
}
return response;
}
@PostMapping("/addbooks")
public String addBooks(@RequestBody BookDetails bookDetails) {
String uri = "http://localhost:8083/addbookdetails";
......@@ -113,6 +94,23 @@ public class BookStoreController {
}
return response;
}
@GetMapping("/getbookdetails")
public String getBookDetails(){
ResponseEntity<String> responseEntity = null;
String response = null;
try{
responseEntity = bookFeignService.getBookDetails();
response = responseEntity.getBody();
}catch(Exception e){
logger.error("Exception while fetching book details :: "+e.getMessage());
ExceptionStatus status = new ExceptionStatus("Unable to fetch book details",
"Exception while fetching book details for your request",400,"/getbookdetails");
response = new Gson().toJson(status);
}
return response;
}
}
package com.bookstore.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(url = "http://localhost:8083/", name = "BOOK-SERVICE")
public interface BookFeignService {
@GetMapping("/getbookdetails")
public ResponseEntity<String> getBookDetails();
}
spring:
application:
name: bookstore
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
preferIpAddress: true
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment