-
Notifications
You must be signed in to change notification settings - Fork 110
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
1 parent
3c08649
commit 8f89796
Showing
42 changed files
with
465 additions
and
76 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
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
30 changes: 8 additions & 22 deletions
30
...ting/src/main/java/io/spring/cloud/samples/brewery/aggregating/IngredientsAggregator.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 |
---|---|---|
@@ -1,49 +1,35 @@ | ||
package io.spring.cloud.samples.brewery.aggregating; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import io.spring.cloud.samples.brewery.aggregating.model.Ingredients; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import io.spring.cloud.samples.brewery.aggregating.model.Ingredient; | ||
import io.spring.cloud.samples.brewery.aggregating.model.Order; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
class IngredientsAggregator { | ||
|
||
private final IngredientsProperties ingredientsProperties; | ||
private final MaturingServiceUpdater maturingUpdater; | ||
private final IngredientWarehouse ingredientWarehouse; | ||
private final Integer threshold; | ||
private final IngredientsCollector ingredientsCollector; | ||
|
||
IngredientsAggregator(IngredientsProperties ingredientsProperties, | ||
IngredientWarehouse ingredientWarehouse, | ||
MaturingServiceClient maturingServiceClient, RestTemplate restTemplate) { | ||
MaturingServiceUpdater maturingServiceUpdater, | ||
IngredientsCollector ingredientsCollector) { | ||
this.ingredientWarehouse = ingredientWarehouse; | ||
this.threshold = ingredientsProperties.getThreshold(); | ||
this.maturingUpdater = new MaturingServiceUpdater(ingredientsProperties, | ||
ingredientWarehouse, maturingServiceClient, restTemplate); | ||
this.ingredientsCollector = ingredientsCollector; | ||
this.maturingUpdater = maturingServiceUpdater; | ||
this.ingredientsProperties = ingredientsProperties; | ||
} | ||
|
||
// TODO: Consider simplifying the case by removing the DB (always matches threshold) | ||
Ingredients fetchIngredients(Order order, String processId) { | ||
log.info("Fetching ingredients for order [{}] , processId [{}] and threshold [{}]", | ||
order, processId, threshold); | ||
ingredients(order).stream() | ||
log.info("Fetching ingredients for order [{}] , processId [{}]",order, processId); | ||
ingredientsCollector.collectIngredients(order, processId).stream() | ||
.filter(ingredient -> ingredient != null) | ||
.forEach(ingredientWarehouse::addIngredient); | ||
Ingredients ingredients = ingredientWarehouse.getCurrentState(); | ||
return maturingUpdater.updateIfLimitReached(ingredients, processId); | ||
} | ||
|
||
private List<Ingredient> ingredients(Order order) { | ||
return order.getItems() | ||
.stream() | ||
.map(ingredientType -> new Ingredient(ingredientType, ingredientsProperties.getReturnedIngredientsQuantity())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...ating/src/main/java/io/spring/cloud/samples/brewery/aggregating/IngredientsCollector.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,54 @@ | ||
package io.spring.cloud.samples.brewery.aggregating; | ||
|
||
import io.spring.cloud.samples.brewery.aggregating.model.Ingredient; | ||
import io.spring.cloud.samples.brewery.aggregating.model.Order; | ||
import io.spring.cloud.samples.brewery.common.TestConfigurationHolder; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.spring.cloud.samples.brewery.common.TestConfigurationHolder.TestCommunicationType.FEIGN; | ||
import static io.spring.cloud.samples.brewery.common.TestRequestEntityBuilder.requestEntity; | ||
|
||
public class IngredientsCollector { | ||
|
||
private final RestTemplate restTemplate; | ||
private final IngredientsProxy ingredientsProxy; | ||
|
||
public IngredientsCollector(RestTemplate restTemplate, IngredientsProxy ingredientsProxy) { | ||
this.restTemplate = restTemplate; | ||
this.ingredientsProxy = ingredientsProxy; | ||
} | ||
|
||
List<Ingredient> collectIngredients(Order order, String processId) { | ||
switch (TestConfigurationHolder.TEST_CONFIG.get().getTestCommunicationType()) { | ||
case FEIGN: | ||
return callViaFeign(order, processId); | ||
default: | ||
return callViaRestTemplate(order, processId); | ||
} | ||
} | ||
|
||
private List<Ingredient> callViaFeign(Order order, String processId) { | ||
return order.getItems() | ||
.stream() | ||
.map(item -> ingredientsProxy.ingredients(item, processId, FEIGN.name())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<Ingredient> callViaRestTemplate(Order order, String processId) { | ||
return order.getItems() | ||
.stream() | ||
.map(item -> | ||
restTemplate.exchange(requestEntity() | ||
.processId(processId) | ||
.serviceName("zuul") | ||
.url(item.name()) | ||
.httpMethod(HttpMethod.POST) | ||
.build(), Ingredient.class).getBody() | ||
) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
...ting/src/main/java/io/spring/cloud/samples/brewery/aggregating/IngredientsProperties.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 |
---|---|---|
@@ -1,38 +1,13 @@ | ||
package io.spring.cloud.samples.brewery.aggregating; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import io.spring.cloud.samples.brewery.aggregating.model.IngredientType; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import lombok.Data; | ||
import io.spring.cloud.samples.brewery.aggregating.model.Order; | ||
|
||
@ConfigurationProperties("ingredients") | ||
@Data | ||
public class IngredientsProperties { | ||
|
||
private Map<IngredientType, String> serviceNames = ImmutableMap.<IngredientType, String>builder() | ||
.put(IngredientType.WATER, IngredientType.WATER.name().toLowerCase()) | ||
.put(IngredientType.MALT, IngredientType.MALT.name().toLowerCase()) | ||
.put(IngredientType.HOP, IngredientType.HOP.name().toLowerCase()) | ||
.put(IngredientType.YEAST, IngredientType.YEAST.name().toLowerCase()) | ||
.build(); | ||
private Integer threshold = 2000; | ||
|
||
/** | ||
* Set to 1000 to correlate with what comes back from config-server | ||
*/ | ||
private Integer returnedIngredientsQuantity = 1000; | ||
|
||
public List<String> getListOfServiceNames(Order order) { | ||
return serviceNames.entrySet() | ||
.stream() | ||
.filter((entry -> order.getItems().contains(entry.getKey()))) | ||
.map((Map.Entry::getValue)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
aggregating/src/main/java/io/spring/cloud/samples/brewery/aggregating/IngredientsProxy.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,21 @@ | ||
package io.spring.cloud.samples.brewery.aggregating; | ||
|
||
import static io.spring.cloud.samples.brewery.common.TestConfigurationHolder.TEST_COMMUNICATION_TYPE_HEADER_NAME; | ||
|
||
import org.springframework.cloud.netflix.feign.FeignClient; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import io.spring.cloud.samples.brewery.aggregating.model.Ingredient; | ||
import io.spring.cloud.samples.brewery.aggregating.model.IngredientType; | ||
|
||
@FeignClient("zuul") | ||
public interface IngredientsProxy { | ||
|
||
@RequestMapping(value = "/{ingredient}", method = RequestMethod.POST) | ||
Ingredient ingredients(@PathVariable("ingredient") IngredientType ingredientType, | ||
@RequestHeader("PROCESS-ID") String processId, | ||
@RequestHeader(TEST_COMMUNICATION_TYPE_HEADER_NAME) String testCommunicationType); | ||
} |
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
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.