Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
bookstore-app
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Narendar Vakiti
bookstore-app
Commits
3ff6cb57
Commit
3ff6cb57
authored
Jun 12, 2020
by
Narendar Vakiti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added feign client
parent
f67e7324
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
143 additions
and
77 deletions
+143
-77
README.md
README.md
+7
-39
pom.xml
pom.xml
+17
-1
BookstoreApplication.java
src/main/java/com/bookstore/BookstoreApplication.java
+4
-0
BookDetails.java
src/main/java/com/bookstore/bean/BookDetails.java
+9
-10
ExceptionStatus.java
src/main/java/com/bookstore/bean/ExceptionStatus.java
+15
-0
AuthorController.java
src/main/java/com/bookstore/controller/AuthorController.java
+44
-0
BookStoreController.java
...in/java/com/bookstore/controller/BookStoreController.java
+25
-27
BookFeignService.java
src/main/java/com/bookstore/service/BookFeignService.java
+12
-0
application.yml
src/main/resources/application.yml
+10
-0
No files found.
README.md
View file @
3ff6cb57
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"
}
pom.xml
View file @
3ff6cb57
...
...
@@ -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>
...
...
src/main/java/com/bookstore/BookstoreApplication.java
View file @
3ff6cb57
...
...
@@ -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
)
{
...
...
src/main/java/com/bookstore/bean/BookDetails.java
View file @
3ff6cb57
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
;
}
src/main/java/com/bookstore/bean/ExceptionStatus.java
0 → 100644
View file @
3ff6cb57
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
;
}
src/main/java/com/bookstore/controller/AuthorController.java
0 → 100644
View file @
3ff6cb57
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
;
}
}
src/main/java/com/bookstore/controller/BookStoreController.java
View file @
3ff6cb57
...
...
@@ -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
;
}
}
src/main/java/com/bookstore/service/BookFeignService.java
0 → 100644
View file @
3ff6cb57
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
();
}
src/main/resources/application.yml
0 → 100644
View file @
3ff6cb57
spring
:
application
:
name
:
bookstore
eureka
:
client
:
serviceUrl
:
defaultZone
:
${EUREKA_URI:http://localhost:8761/eureka}
instance
:
preferIpAddress
:
true
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment