Somesh Vemula
Somesh Vemula

Reputation: 11

How can I implement the circuit breaker pattern and retry pattern at the same time using Resilience4j with Spring Boot and microservices?

I'm doing a project using Spring Boot and Micro-services architecture. I have two micro-services one calling the other to process a request from API gateway. I'm trying to implement the circuit breaker pattern and retry pattern at the same time using resillience4j but my micro-service is not retrying.

Note: If I disable(comment out) the circuit breaker annotation, my micro-service is retrying!!

Here is my code of the service layer of micro-service.

@Override
    @CircuitBreaker(name="${spring.application.name}", fallbackMethod = "getDefaultDepartment")
    @Retry(name="${spring.application.name}", fallbackMethod = "getDefaultDepartment")

    public ResponseDto getEmployee(Long id) {
        LOGGER.info("inside getEmployee by ID method");
        Employee employee = employeeRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Employee", "id", id));
//        ------------------ Communication using REST template -----------------------------
//        ResponseEntity<DepartmentDto> responseEntity = restTemplate.getForEntity("http://localhost:8080/api/departments/" + employee.getDepartmentCode(),
//                DepartmentDto.class);
//        DepartmentDto departmentDto = responseEntity.getBody();

//      ------------------ Communication using WebClient -----------------------------
//        DepartmentDto departmentDto = webClient.get()
//                .uri("http://localhost:8081/api/departments/" + employee.getDepartmentCode())
//                .retrieve()
//                .bodyToMono(DepartmentDto.class)
//                .block();

//      ----------------------- Communication using OpenFeign -----------------------------
        DepartmentDto departmentDto = apiClient.getDepartment(employee.getDepartmentCode());

        return new ResponseDto(modelMapper.map(employee, EmployeeDto.class), departmentDto);
    }

Here is my properties file

#------------------------------  Circuit breaker properties--------------------------------------
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.registerHealthIndicator=true
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.failureRateThreshold=50
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.minimumNumberOfCalls=5
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.automaticTransitionFromOpenToHalfOpenEnabled=true
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.waitDurationInOpenState=5s
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.permittedNumberOfCallsInHalfOpenState=3
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.slidingWindowSize=10
resilience4j.circuitbreaker.instances.EMPLOYEE-SERVICE.slidingWindowType=COUNT_BASED


#------------------------------  Retry Configuration  --------------------------------------
resilience4j.retry.instances.EMPLOYEE-SERVICE.registerHealthIndicator=true
resilience4j.retry.instances.EMPLOYEE-SERVICE.maxAttempts=10
resilience4j.retry.instances.EMPLOYEE-SERVICE.waitDuration=1s

I've tried multiple time, but didn't work :)

Upvotes: 1

Views: 557

Answers (0)

Related Questions