Lets create a sample products application which has a rest endpoint which send a string message in the response. Then we will create another service called circuit breaker which will have circuit breaker dependency. This service will call our products service endpoint, and if the products service is down, instead of sending 500 Internal server error in response, we will get a default message.
Products Service
We can create a new spring project from site: start.spring.io
Lets create a sample endpoint which will send a message in response. And lets run this application on 9001 port.
Lets test if the endpoint is working
Eureka Service
We can create a new spring project from site: start.spring.io with following dependency:
Lets add the aop dependency manually in the application.
Lets create a endpoint which will call the products service endpoint and annotate it with @CircuitBreaker annotation, provide a name and declare and define a fallback method.
Now lets expose the health endpoint and configure our circuit breaker:
Now run both the applications
Test:
Keep both the circuit breaker and products applications UP and call the endpoint of circuit breaker service which will inturn call products service
We see that the response is received. Now Stop the products service and call the endpoint again.
This time our default method got called and we got the default response.