BAEL-947 Adding combined endpoint and feign clients.
This commit is contained in:
+6
@@ -1,5 +1,7 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.gateway;
|
||||
|
||||
import com.baeldung.spring.cloud.bootstrap.gateway.client.book.BooksClient;
|
||||
import com.baeldung.spring.cloud.bootstrap.gateway.client.rating.RatingsClient;
|
||||
import com.netflix.appinfo.InstanceInfo;
|
||||
import com.netflix.discovery.EurekaClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -8,6 +10,7 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClientSpecification;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
@@ -16,6 +19,8 @@ import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter;
|
||||
import org.springframework.cloud.sleuth.zipkin.ZipkinProperties;
|
||||
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import zipkin.Span;
|
||||
|
||||
@@ -25,6 +30,7 @@ import java.util.List;
|
||||
@SpringBootApplication
|
||||
@EnableZuulProxy
|
||||
@EnableEurekaClient
|
||||
@EnableFeignClients
|
||||
public class GatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.gateway.client.book;
|
||||
|
||||
import com.baeldung.spring.cloud.bootstrap.gateway.client.rating.Rating;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Book {
|
||||
private Long id;
|
||||
private String author;
|
||||
private String title;
|
||||
private List<Rating> ratings;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public List<Rating> getRatings() {
|
||||
return ratings;
|
||||
}
|
||||
|
||||
public void setRatings(List<Rating> ratings) {
|
||||
this.ratings = ratings;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.gateway.client.book;
|
||||
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@FeignClient(value = "book-service")
|
||||
public interface BooksClient {
|
||||
@RequestMapping(method = RequestMethod.GET, value="/books/{bookId}")
|
||||
Book getBookById(@PathVariable("bookId") Long bookId, @RequestHeader("Cookie") String session);
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.gateway.client.rating;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Rating {
|
||||
private Long id;
|
||||
private Long bookId;
|
||||
private int stars;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public void setBookId(Long bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public int getStars() {
|
||||
return stars;
|
||||
}
|
||||
|
||||
public void setStars(int stars) {
|
||||
this.stars = stars;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.gateway.client.rating;
|
||||
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(value = "rating-service")
|
||||
public interface RatingsClient {
|
||||
@RequestMapping(method = RequestMethod.GET, value="/ratings")
|
||||
List<Rating> getRatingsByBookId(@RequestParam("bookId") Long bookId, @RequestHeader("Cookie") String session);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.spring.cloud.bootstrap.gateway.controller;
|
||||
|
||||
import com.baeldung.spring.cloud.bootstrap.gateway.client.book.Book;
|
||||
import com.baeldung.spring.cloud.bootstrap.gateway.client.book.BooksClient;
|
||||
import com.baeldung.spring.cloud.bootstrap.gateway.client.rating.Rating;
|
||||
import com.baeldung.spring.cloud.bootstrap.gateway.client.rating.RatingsClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/combined")
|
||||
public class CombinedController {
|
||||
|
||||
private final BooksClient booksClient;
|
||||
private final RatingsClient ratingsClient;
|
||||
|
||||
@Autowired
|
||||
public CombinedController(BooksClient booksClient, RatingsClient ratingsClient) {
|
||||
this.booksClient = booksClient;
|
||||
this.ratingsClient = ratingsClient;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Book getCombinedResponse(@RequestParam Long bookId, @CookieValue("SESSION") String session){
|
||||
Book book = booksClient.getBookById(bookId, "SESSION="+session);
|
||||
List<Rating> ratings = ratingsClient.getRatingsByBookId(bookId, "SESSION="+session);
|
||||
book.setRatings(ratings);
|
||||
return book;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user