How to implement a recovery mechanism when a microservice is temporarily unavailable in Spring Boot?

Viewed 297

I'm learning about microservices communication, and I developed a financial transactions app with these technologies: Spring Boot, Spring MVC, Spring Data JPA and Hibernate Validator.

There are 2 microservices, the first microservice is for validation, it is a REST API with POST methods. And the second microservice is for persistence. And the communication between the 2 microservice is asynchronous and I'm using JMS and ActiveMQ for it.

After that I need to develop a recovery mechanism for the case when one of the microservices is temporarily unavailable. Can someone tell me how can I do this recovery mechanism? Thank you in advance!

1 Answers

You can use Hystrix framework for using circuit breaker. Its simply wrapping your class with proxy , if your method considered "not working" based on your configuration its re-direct your call to fallback method. pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

MainApp

@EnableCircuitBreaker
public class AppName {}

Class that run your business method

@Service
public class ClassName {
...
@HystrixCommand(fallbackMethod="fallbackMethodName")
<method signature> {
  }
…

<method signature> fallbackMethodName(...) {
        ...
    }
}
Related