Add a Circuit Breaker to Your Feign Client
What happens if the service is not available rather than having exceptions for a REST API call?
{"timestamp": "2021-09-12T15:57:58.685+00:00","status": 500,"error": "Internal Server Error","trace": "feign.FeignException$ServiceUnavailable: [503] during [GET] to [http://score-segment/score-segment/3654118644] [ScoreSegmentProxy#retrieveExchangeValue(BigInteger)]: [Load balancer does not contain an instance for the service score-segment]\n\tat feign.FeignException.serverErrorStatus(FeignException.java:237)\n\tat feign.FeignException.errorStatus(FeignException.java:180)\n\tat feign.FeignException.errorStatus(FeignException.java:169)\n\tat feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92)\n\tat feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96)\n\tat feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)\n\tat feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)\n\tat feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)\n\tat jdk.proxy3/jdk.proxy3.$Proxy154.retrieveExchangeValue(Unknown Source)\n\tat com.nils.microservices.scorecalculator.service.ScoreCalculatorService.getScoreSegment(ScoreCalculatorService.java:50)...","message": "[503] during [GET] to [http://score-segment/score-segment/3654118644] [ScoreSegmentProxy#retrieveExchangeValue(BigInteger)]: [Load balancer does not contain an instance for the service score-segment]","path": "/calculate-score"}
To handle this error, you should add enable circuit breaker for Feign to your application.properties in your project that calls this service.
feign.circuitbreaker.enabled=true
Update your Proxy Feign Interface by adding a callback class (here, it is named as “ScoreSegmentFallback”):
You should override the method in proxy class in your fallback class that implements the proxy:
You can visit my Microservices project (implemented with Spring Cloud) GitHub url for sample code:
Happy Coding!