feature: add resilience4j retry

This commit is contained in:
Alexander
2022-04-15 12:30:38 +03:00
parent 995b1c5457
commit 087998745b
3 changed files with 41 additions and 1 deletions

View File

@@ -17,6 +17,11 @@
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>

View File

@@ -3,6 +3,8 @@ package com.eventsourcing.bankAccount.commands;
import com.eventsourcing.bankAccount.domain.BankAccountAggregate;
import com.eventsourcing.es.EventStoreDB;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.sleuth.annotation.NewSpan;
@@ -15,9 +17,12 @@ import org.springframework.stereotype.Service;
public class BankAccountCommandHandler implements BankAccountCommandService {
private final EventStoreDB eventStoreDB;
private static final String SERVICE_NAME = "microservice";
@Override
@NewSpan
@Retry(name = SERVICE_NAME)
@CircuitBreaker(name = SERVICE_NAME)
public String handle(@SpanTag("command") CreateBankAccountCommand command) {
final var aggregate = new BankAccountAggregate(command.aggregateID());
aggregate.createBankAccount(command.email(), command.address(), command.userName());
@@ -29,6 +34,8 @@ public class BankAccountCommandHandler implements BankAccountCommandService {
@Override
@NewSpan
@Retry(name = SERVICE_NAME)
@CircuitBreaker(name = SERVICE_NAME)
public void handle(@SpanTag("command") ChangeEmailCommand command) {
final var aggregate = eventStoreDB.load(command.aggregateID(), BankAccountAggregate.class);
aggregate.changeEmail(command.newEmail());
@@ -38,6 +45,8 @@ public class BankAccountCommandHandler implements BankAccountCommandService {
@Override
@NewSpan
@Retry(name = SERVICE_NAME)
@CircuitBreaker(name = SERVICE_NAME)
public void handle(@SpanTag("command") ChangeAddressCommand command) {
final var aggregate = eventStoreDB.load(command.aggregateID(), BankAccountAggregate.class);
aggregate.changeAddress(command.newAddress());
@@ -47,6 +56,8 @@ public class BankAccountCommandHandler implements BankAccountCommandService {
@Override
@NewSpan
@Retry(name = SERVICE_NAME)
@CircuitBreaker(name = SERVICE_NAME)
public void handle(@SpanTag("command") DepositAmountCommand command) {
final var aggregate = eventStoreDB.load(command.aggregateID(), BankAccountAggregate.class);
aggregate.depositBalance(command.amount());

View File

@@ -43,4 +43,28 @@ management.endpoints.web.exposure.include=health,prometheus,info
spring.sleuth.propagation.type=w3c,b3
spring.sleuth.opentracing.enabled=true
spring.zipkin.base-url=http://localhost:9411
spring.zipkin.base-url=http://localhost:9411
resilience4j.retry.instances.microservice.max-attempts=3
resilience4j.retry.instances.microservice.waitDuration=1s
resilience4j.retry.instances.microservice.enableExponentialBackoff=true
resilience4j.retry.instances.microservice.exponentialBackoffMultiplier=2
resilience4j.circuitbreaker.instances.microservice.registerHealthIndicator=true
resilience4j.circuitbreaker.instances.microservice.slidingWindowSize=5
resilience4j.circuitbreaker.instances.microservice.permittedNumberOfCallsInHalfOpenState=3
resilience4j.circuitbreaker.instances.microservice.slidingWindowType=TIME_BASED
resilience4j.circuitbreaker.instances.microservice.minimumNumberOfCalls=20
resilience4j.circuitbreaker.instances.microservice.waitDurationInOpenState=50s
resilience4j.circuitbreaker.instances.microservice.failureRateThreshold=50
resilience4j.circuitbreaker.instances.microservice.eventConsumerBufferSize=10
resilience4j.thread-pool-bulkhead.instances.microservice.maxThreadPoolSize=1
resilience4j.thread-pool-bulkhead.instances.microservice.coreThreadPoolSize=1
resilience4j.thread-pool-bulkhead.instances.microservice.queueCapacity=1
resilience4j.timelimiter.instances.microservice.timeoutDuration=3s
resilience4j.timelimiter.instances.microservice.cancelRunningFuture=true