-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
254 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ce/src/main/java/dev/azdanov/orderservice/clients/catalog/CatalogServiceClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dev.azdanov.orderservice.clients.catalog; | ||
|
||
import dev.azdanov.orderservice.ApplicationProperties; | ||
import java.time.Duration; | ||
import org.springframework.boot.web.client.ClientHttpRequestFactories; | ||
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Configuration | ||
class CatalogServiceClientConfig { | ||
|
||
@Bean | ||
RestClient restClient(ApplicationProperties properties) { | ||
return RestClient.builder() | ||
.baseUrl(properties.catalogServiceUrl()) | ||
.requestFactory(ClientHttpRequestFactories.get(ClientHttpRequestFactorySettings.DEFAULTS | ||
.withConnectTimeout(Duration.ofSeconds(5)) | ||
.withReadTimeout(Duration.ofSeconds(5)))) | ||
.build(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
order-service/src/main/java/dev/azdanov/orderservice/clients/catalog/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dev.azdanov.orderservice.clients.catalog; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public record Product(String code, String name, String description, String imageUrl, BigDecimal price) {} |
39 changes: 39 additions & 0 deletions
39
...-service/src/main/java/dev/azdanov/orderservice/clients/catalog/ProductServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dev.azdanov.orderservice.clients.catalog; | ||
|
||
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; | ||
import io.github.resilience4j.retry.annotation.Retry; | ||
import java.util.Optional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Component | ||
public class ProductServiceClient { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ProductServiceClient.class); | ||
|
||
private static final String CATALOG_SERVICE = "catalog-service"; | ||
|
||
private final RestClient restClient; | ||
|
||
ProductServiceClient(RestClient restClient) { | ||
this.restClient = restClient; | ||
} | ||
|
||
@CircuitBreaker(name = CATALOG_SERVICE) | ||
@Retry(name = CATALOG_SERVICE, fallbackMethod = "findProductByCodeFallback") | ||
public Optional<Product> findProductByCode(String code) { | ||
log.info("Fetching product for code: {}", code); | ||
return Optional.ofNullable(fetchProductByCode(code)); | ||
} | ||
|
||
private Product fetchProductByCode(String code) { | ||
return restClient.get().uri("/api/v1/products/{code}", code).retrieve().body(Product.class); | ||
} | ||
|
||
Optional<Product> findProductByCodeFallback(String code, Throwable t) { | ||
log.warn("Fallback triggered for getProductByCode. Code: {}, Error: {}", code, t.getMessage()); | ||
return Optional.empty(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
order-service/src/main/java/dev/azdanov/orderservice/domain/InvalidOrderException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dev.azdanov.orderservice.domain; | ||
|
||
public class InvalidOrderException extends RuntimeException { | ||
|
||
public InvalidOrderException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
order-service/src/main/java/dev/azdanov/orderservice/domain/OrderValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.azdanov.orderservice.domain; | ||
|
||
import dev.azdanov.orderservice.clients.catalog.Product; | ||
import dev.azdanov.orderservice.clients.catalog.ProductServiceClient; | ||
import dev.azdanov.orderservice.domain.models.CreateOrderRequest; | ||
import dev.azdanov.orderservice.domain.models.OrderItem; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
class OrderValidator { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(OrderValidator.class); | ||
|
||
private final ProductServiceClient client; | ||
|
||
OrderValidator(ProductServiceClient client) { | ||
this.client = client; | ||
} | ||
|
||
void validate(CreateOrderRequest request) { | ||
request.items().forEach(this::validateOrderItem); | ||
} | ||
|
||
private void validateOrderItem(OrderItem item) { | ||
Product product = getProductByCode(item.code()); | ||
validateProductPrice(item, product); | ||
} | ||
|
||
private Product getProductByCode(String code) { | ||
return client.findProductByCode(code) | ||
.orElseThrow(() -> new InvalidOrderException("Invalid product code: " + code)); | ||
} | ||
|
||
private void validateProductPrice(OrderItem item, Product product) { | ||
if (item.price().compareTo(product.price()) != 0) { | ||
log.error( | ||
"Product price not matching. Actual price: {}, received price: {}", product.price(), item.price()); | ||
throw new InvalidOrderException("Product price not matching"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.