xxxxxxxxxx
the Circuit Breaker design pattern is used to stop the process of request and response if a service is not working.
So, for example, let’s say a client is sending a request to retrieve data from multiple services.
But, due to some issues, one of the services is down.
Now, there are mainly two problems you will face: first, since the client will not have any knowledge about a particular service being down, the request will be continuously sent to that service.
The second problem is that the network resources will be exhausted with low performance and bad user experience.
So, to avoid such problems, you can use the Circuit Breaker Design Pattern. With the help of this pattern, the client will invoke a remote service via a proxy.
This proxy will basically behave as a circuit barrier.
So, when the number of failures crosses the threshold number, the circuit breaker trips for a particular time period.
Then, all the attempts to invoke the remote service will fail in this timeout period.
Once that time period is finished, the circuit breaker will allow a limited number of tests to pass through and if those requests succeed, the circuit breaker resumes back to the normal operation. Else, if there is a failure, then the time out period begins again.
xxxxxxxxxx
import java.net.URI;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class StudentService {
private final RestTemplate restTemplate;
public StudentService(RestTemplate rest) {
this.restTemplate = rest;
}
@HystrixCommand(fallbackMethod = "reliable")
public String studentMethodCalling() {
URI uri = URI.create("http://localhost:8000/student");
return this.restTemplate.getForObject(uri, String.class);
}
public String reliable() {
return "This is calling if the studentMethod is falling to respond on time";
}
}
xxxxxxxxxx
It is located at proxy microservice between client microservice and server microservice
xxxxxxxxxx
Applies to point to point API calls.
We set a threshhol for failing API calls eg. 3
If failing 3, we reject the API calls
Circuit breaker microser acts as proxy
3 states Open Closed Half-open(sometimes we send, sometimes we reject, close to threshhold)
xxxxxxxxxx
@Component
class RegistrationServiceProxy @Autowired()(restTemplate: RestTemplate) extends RegistrationService {
@Value("${user_registration_url}")
var userRegistrationUrl: String = _
@HystrixCommand(commandProperties=Array(new HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="800")))
override def registerUser(emailAddress: String, password: String): Either[RegistrationError, String] = {
try {
val response = restTemplate.postForEntity(userRegistrationUrl,
RegistrationBackendRequest(emailAddress, password),
classOf[RegistrationBackendResponse])
response.getStatusCode match {
case HttpStatus.OK =>
Right(response.getBody.id)
}
} catch {
case e: HttpClientErrorException if e.getStatusCode == HttpStatus.CONFLICT =>
Left(DuplicateRegistrationError)
}
}
}