-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProductBuilder.groovy
56 lines (46 loc) · 2.03 KB
/
ProductBuilder.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.nklmish.cs.catalogs.integration
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
import com.nklmish.cs.catalogs.model.Comment
import com.nklmish.cs.catalogs.model.Price
import com.nklmish.cs.catalogs.model.Product
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
@Component
@Slf4j
class ProductBuilder {
RestTemplate restTemplate
@Autowired
ProductBuilder(@LoadBalanced RestTemplate restTemplate) {
this.restTemplate = restTemplate
}
@HystrixCommand(fallbackMethod = "productFallback")
Product fetchProduct(int id) {
log.debug("fetching product {}", id)
return restTemplate.getForObject("http://product-service/product/$id", Product)
}
static Product productFallback(int productId) {
log.error("fallback method invoked, hystrix command failed to retrieve product via product service")
return new Product(name: "Demo Product")
}
@HystrixCommand(fallbackMethod = "priceFallback")
Price fetchPrice(int productId) {
log.debug("fetching price for product {}", productId)
return restTemplate.getForObject("http://price-service/price/$productId", Price)
}
static Price priceFallback(int productId) {
log.error("fallback method invoked, hystrix command failed to retrieve price via price service")
return null
}
@HystrixCommand(fallbackMethod = "commentFallback")
List<Comment> fetchComments(int productId) {
log.debug("fetching comments for product {}", productId)
return restTemplate.getForObject("http://comment-service/comment/$productId", List)
}
static List<Comment> commentFallback(int productId) {
log.error("fallback method invoked, hystrix command failed to retrieve comments via comment service")
return [] //return empty list of comments
}
}