commit
61bea8493d
@ -44,6 +44,10 @@
|
|||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-zipkin</artifactId>
|
<artifactId>spring-cloud-starter-zipkin</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-feign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.spring.cloud.bootstrap.gateway;
|
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.appinfo.InstanceInfo;
|
||||||
import com.netflix.discovery.EurekaClient;
|
import com.netflix.discovery.EurekaClient;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
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.RibbonClientSpecification;
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
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.ZipkinProperties;
|
||||||
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter;
|
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter;
|
||||||
import org.springframework.context.annotation.Bean;
|
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 org.springframework.web.client.RestTemplate;
|
||||||
import zipkin.Span;
|
import zipkin.Span;
|
||||||
|
|
||||||
@ -25,6 +30,7 @@ import java.util.List;
|
|||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableZuulProxy
|
@EnableZuulProxy
|
||||||
@EnableEurekaClient
|
@EnableEurekaClient
|
||||||
|
@EnableFeignClients
|
||||||
public class GatewayApplication {
|
public class GatewayApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(GatewayApplication.class, args);
|
SpringApplication.run(GatewayApplication.class, args);
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -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);
|
||||||
|
List<Rating> ratings = ratingsClient.getRatingsByBookId(bookId, "SESSION="+session);
|
||||||
|
book.setRatings(ratings);
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.spring.cloud.bootstrap.gateway;
|
package com.baeldung.spring.cloud.bootstrap.gateway;
|
||||||
|
|
||||||
|
import com.baeldung.spring.cloud.bootstrap.gateway.client.book.Book;
|
||||||
|
import com.baeldung.spring.cloud.bootstrap.gateway.client.rating.Rating;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
import io.restassured.authentication.FormAuthConfig;
|
import io.restassured.authentication.FormAuthConfig;
|
||||||
@ -80,7 +82,9 @@ public class LiveTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenAddnewRating_thenSuccess() {
|
public void whenAddnewRating_thenSuccess() {
|
||||||
|
|
||||||
final Rating rating = new Rating(1L, 4);
|
final Rating rating = new Rating();
|
||||||
|
rating.setBookId(1L);
|
||||||
|
rating.setStars(4);
|
||||||
|
|
||||||
// request the protected resource
|
// request the protected resource
|
||||||
final Response ratingResponse = RestAssured.given()
|
final Response ratingResponse = RestAssured.given()
|
||||||
@ -98,7 +102,9 @@ public class LiveTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAddnewBook_thenSuccess() {
|
public void whenAddnewBook_thenSuccess() {
|
||||||
final Book book = new Book("Baeldung", "How to spring cloud");
|
final Book book = new Book();
|
||||||
|
book.setTitle("How to spring cloud");
|
||||||
|
book.setAuthor("Baeldung");
|
||||||
|
|
||||||
// request the protected resource
|
// request the protected resource
|
||||||
final Response bookResponse = RestAssured.given()
|
final Response bookResponse = RestAssured.given()
|
||||||
@ -115,83 +121,17 @@ public class LiveTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@Test
|
||||||
public static class Book {
|
public void accessCombinedEndpoint() {
|
||||||
|
final Response response = RestAssured.given()
|
||||||
private Long id;
|
.auth()
|
||||||
private String author;
|
.form("user", "password", formConfig)
|
||||||
private String title;
|
.get(ROOT_URI + "/combined?bookId=1");
|
||||||
|
Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode());
|
||||||
public Book() {
|
Assert.assertNotNull(response.getBody());
|
||||||
|
final Book result = response.as(Book.class);
|
||||||
|
Assert.assertEquals(new Long(1), result.getId());
|
||||||
|
Assert.assertNotNull(result.getRatings());
|
||||||
|
Assert.assertTrue(result.getRatings().size() > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Book(String author, String title) {
|
|
||||||
this.author = author;
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
public static class Rating {
|
|
||||||
private Long id;
|
|
||||||
private Long bookId;
|
|
||||||
private int stars;
|
|
||||||
|
|
||||||
public Rating() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Rating(Long bookId, int stars) {
|
|
||||||
this.bookId = bookId;
|
|
||||||
this.stars = 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user