From 5511f6c274c01cfa10c299fd2d6f2858a99c9553 Mon Sep 17 00:00:00 2001 From: jose luis sanchez romero Date: Fri, 17 Nov 2023 09:35:27 +0100 Subject: [PATCH] Changed bulkhead to threadpool --- pom.xml | 2 +- .../resources/PortfolioResource.java | 4 +- .../resources/StockPriceService.java | 12 +++--- .../src/main/resources/application.properties | 37 ++++++++++--------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 0dbc548..e88ad0a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,6 @@ org.springframework.boot spring-boot-starter-parent - 3.2.0-RC2 @@ -27,6 +26,7 @@ 17 + 6.1.0 2023.0.0-RC1 2.21.1 diff --git a/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/PortfolioResource.java b/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/PortfolioResource.java index 9ebda30..7e08c7f 100644 --- a/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/PortfolioResource.java +++ b/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/PortfolioResource.java @@ -32,7 +32,9 @@ public PortfolioResource(StockPriceService service, StatsResource stats) { @GetMapping("/{user}") public CompletableFuture get(@PathVariable("user") String user) { - return getOrCreatePortfolioContent(user).thenApply(this::getStockPrices).thenApply(this::fillPortfolio); + return getOrCreatePortfolioContent(user) + .thenApply(this::getStockPrices) + .thenApply(this::fillPortfolio); } private CompletableFuture> getOrCreatePortfolioContent(final String user) { diff --git a/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/StockPriceService.java b/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/StockPriceService.java index 0cd9a95..cb7de0f 100644 --- a/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/StockPriceService.java +++ b/portfolio/src/main/java/es/ing/spring/ftdemo/portfolio/resources/StockPriceService.java @@ -1,6 +1,9 @@ package es.ing.spring.ftdemo.portfolio.resources; +import io.github.resilience4j.bulkhead.annotation.Bulkhead; +import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; import io.github.resilience4j.retry.annotation.Retry; +import io.github.resilience4j.timelimiter.annotation.TimeLimiter; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -22,12 +25,11 @@ public StockPriceService(StockPriceClientExchange stockPriceClientExchange, Stat private final ConcurrentMap cache = new ConcurrentHashMap<>(); - // @Bulkhead(name = "bulk", type = Bulkhead.Type.THREADPOOL) - // @CircuitBreaker(name = "circuit") - // @Retry(name = "retry", fallbackMethod = "getPriceFallback") + // Retry ( CircuitBreaker ( RateLimiter ( TimeLimiter ( Bulkhead ( Function @Retry(name = "retry") - // @TimeLimiter(name = "limiter", fallbackMethod = "getPriceFallback") - // @TimeLimiter(name = "limiter") + @TimeLimiter(name = "limiter") + @CircuitBreaker(name = "circuitbreaker") + @Bulkhead(name = "bulkhead", type = Bulkhead.Type.THREADPOOL) public CompletableFuture getPrice(String ticker) { return CompletableFuture.supplyAsync( () -> { diff --git a/portfolio/src/main/resources/application.properties b/portfolio/src/main/resources/application.properties index 2614ee8..fffa149 100644 --- a/portfolio/src/main/resources/application.properties +++ b/portfolio/src/main/resources/application.properties @@ -2,26 +2,27 @@ management.endpoint.health.show-details=always management.endpoints.web.exposure.include=* management.health.circuitbreakers.enabled=true management.health.ratelimiters.enabled=true -resilience4j.bulkhead.configs.default.max-concurrent-calls=50 -resilience4j.bulkhead.configs.default.max-wait-duration=1s +#resilience4j.bulkhead.configs.default.max-concurrent-calls=30 +#resilience4j.bulkhead.configs.default.max-wait-duration=1s resilience4j.bulkhead.configs.default.writable-stack-trace-enabled=false -resilience4j.circuitbreaker.configs.default.automatic-transition-from-open-to-half-open-enabled=true -resilience4j.circuitbreaker.configs.default.event-consumer-buffer-size=10 -resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50 -resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=5 -resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=3 -resilience4j.circuitbreaker.configs.default.recordExceptions=org.springframework.web.client.HttpServerErrorException,java.util.concurrent.TimeoutException,java.io.IOException,org.springframework.web.client.ResourceAccessException,java.util.concurrent.ExecutionException,java.nio.channels.ClosedChannelException -resilience4j.circuitbreaker.configs.default.registerHealthIndicator=true -resilience4j.circuitbreaker.configs.default.slidingWindowSize=10 +#resilience4j.circuitbreaker.configs.default.automatic-transition-from-open-to-half-open-enabled=true +#resilience4j.circuitbreaker.configs.default.event-consumer-buffer-size=10 +#resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50 +#resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=5 +#resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=3 +#resilience4j.circuitbreaker.configs.default.recordExceptions=org.springframework.web.client.HttpServerErrorException,java.util.concurrent.TimeoutException,java.io.IOException,org.springframework.web.client.ResourceAccessException,java.util.concurrent.ExecutionException,java.nio.channels.ClosedChannelException +#resilience4j.circuitbreaker.configs.default.registerHealthIndicator=true +#resilience4j.circuitbreaker.configs.default.slidingWindowSize=10 resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=5s -resilience4j.retry.configs.default.max-attempts=1 -resilience4j.retry.configs.default.retryExceptions=org.springframework.web.client.HttpServerErrorException,java.util.concurrent.TimeoutException,java.io.IOException,org.springframework.web.client.ResourceAccessException,java.util.concurrent.ExecutionException,java.nio.channels.ClosedChannelException -resilience4j.retry.configs.default.wait-duration=2s -resilience4j.thread-pool-bulkhead.configs.default.core-thread-pool-size=10 -resilience4j.thread-pool-bulkhead.configs.default.max-thread-pool-size=20 -resilience4j.thread-pool-bulkhead.configs.default.queue-capacity=10 -resilience4j.timelimiter.configs.limiter.cancel-running-future=true -resilience4j.timelimiter.configs.limiter.timeout-duration=1s +#resilience4j.circuitbreaker.configs.default.ignore-exceptions +#resilience4j.retry.configs.default.max-attempts=1 +#resilience4j.retry.configs.default.retryExceptions=org.springframework.web.client.HttpServerErrorException,java.util.concurrent.TimeoutException,java.io.IOException,org.springframework.web.client.ResourceAccessException,java.util.concurrent.ExecutionException,java.nio.channels.ClosedChannelException +#resilience4j.retry.configs.default.wait-duration=2s +#resilience4j.thread-pool-bulkhead.configs.default.core-thread-pool-size=30 +#resilience4j.thread-pool-bulkhead.configs.default.max-thread-pool-size=40 +#resilience4j.thread-pool-bulkhead.configs.default.queue-capacity=10 +#resilience4j.timelimiter.configs.limiter.cancel-running-future=true +#resilience4j.timelimiter.configs.limiter.timeout-duration=1s server.port=7070 spring.application.name=ftdemo-portfolio spring.devtools.livereload.port=35730