Cloud computing

Introduction

Architecture de Spring Cloud

Eureka Config Serve Zuul Consul Hystrix Resilience4J

Spring Boot (BackEnd) TPs

Creation,Dépendance,Configuration Exemple Video RestController

Produit Rest API

Entity et repository Ajouter Afficher Liste Produit Détails,Supprimer,Vider Modifier

Angular (FrontEnd)

Angular Rappel CLient CRUD

Spring Security

User Auth

CRUD

Vente CRUD

To be Continued...

Middlewares Orientés Messages

Communication Synchrone vs. Asynchrone API JMS : Java Message Service JMS avec ActiveMQ et HornetQ KAFKA

Spring Batch

Spring Batch

Stream Processing

Kafka Streams

Architectures Serverless

Architectures Serverless Résumé



Hystrix

Hystrix

Hystrix is a fault-tolerant library that helps prevent cascading failures by implementing the circuit breaker pattern.

  1. Latency and fault tolerance management
  2. Implements the circuit breaker pattern
  3. Isolates service failures to avoid system-wide issues
  4. Ensures graceful degradation in case of failures
<

Example

Fallback Method: In Service B, the @HystrixCommand annotation is applied to the serviceB method. If the call to Service A fails or takes too long, Hystrix will invoke the fallbackServiceA method instead, returning a predefined response.

Service B


@GetMapping("/serviceB")
@HystrixCommand(fallbackMethod = "fallbackServiceA")
public String serviceB() {
    String response = restTemplate.getForObject("http://service-a/serviceA", String.class);
    return "Service B calls " + response;
}

public String fallbackServiceA() {
    return "Fallback response from Service B";
}

Service C

Fallback Method: In Service C, the @HystrixCommand annotation is used in a similar way. It wraps the call to Service B. If this call fails, Hystrix will call the fallbackServiceB method.

@GetMapping("/serviceC")
@HystrixCommand(fallbackMethod = "fallbackServiceB")
public String serviceC() {
    String response = restTemplate.getForObject("http://service-b/serviceB", String.class);
    return "Service C calls " + response;
}

public String fallbackServiceB() {
    return "Fallback response from Service C";
}

Summary of Hystrix Usage

Service B:


Primary Method: serviceB()
Fallback Method: fallbackServiceA()
If the call to Service A fails, the system falls back to returning "Fallback response from Service B".

Service C:


Primary Method: serviceC()
Fallback Method: fallbackServiceB()
If the call to Service B fails, the system falls back to returning "Fallback response from Service C".

Benefits of Using Hystrix

  • Fault Isolation: If one service fails (like Service A), it does not affect the others (Service B and Service C).
  • Fallback Mechanism: Provides an alternative response, which can improve user experience by avoiding failures.
  • Prevent Cascading Failures: Helps prevent failures from propagating through the system, allowing the application to remain functional even when some services are down.