SpringCloud uses Hystrix to implement Trading curb
To implement the Trading curb using Hystrix in Spring Cloud, you need to add the following Maven coordinates of the dependent class library:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
This library is part of the Spring Cloud Netflix project and provides integration with Netflix's Hystrix library. Hystrix is a library used to handle latency and faults, which can help us build resilient and resilient distributed systems.
The following is an example of using Hystrix to implement a simple circuit breaker logic. Next, we will introduce the service providers, service consumers, and Hystrix configurations in the sample separately.
Firstly, the service provider implements:
@RestController
public class ProviderController {
@GetMapping("/hello")
public String hello() {
//Assuming there is a remote call that may fail here
//Assuming an exception is thrown when the call fails
if (Math.random() < 0.5) {
Throw new RuntimeException ("call failed");
}
return "Hello, World!";
}
@GetMapping("/fallback")
public String fallback() {
Return "Service downgrade return";
}
}
The service provider includes two interfaces:
-'/hello': A simple interface used to showcase the functionality of a service provider. In the example, we assume that the interface calls a remote service that may fail.
-'/fallback': A downgrade interface used to return a fixed response in the event of a fuse.
Next is the implementation of service consumers:
@RestController
public class ConsumerController {
@Autowired
private ProviderService providerService;
@GetMapping("/invoke")
public String invoke() {
return providerService.hello();
}
}
The service consumer encapsulates the service provider as a service class (ProviderService), and calls the method of this service class in the 'invoke' interface.
Finally, we need to configure Hystrix for service consumers.
Firstly, add the annotation '@ Enable Hystrix' on the startup class of the application to enable Hystrix functionality:
@SpringBootApplication
@EnableHystrix
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Then, add a '@ HystrixCommand' annotation on the method of the service class (ProviderService) to specify the demotion logic when the service call fails or times out:
@Service
public class ProviderService {
@HystrixCommand(fallbackMethod = "fallback")
public String hello() {
//Assuming there is a remote call that may fail here
//Assuming an exception is thrown when the call fails
if (Math.random() < 0.5) {
Throw new RuntimeException ("call failed");
}
return "Hello, World!";
}
public String fallback() {
Return "Service downgrade return";
}
}
In the annotation '@ HystrixCommand', we specify a downgrade method 'fallback()', which will be executed when the 'hello()' method call fails.
So far, we have completed a basic example of using Hystrix to implement Trading curb. To summarize:
-Hystrix is a library used to handle latency and faults, which can help us build resilient and resilient distributed systems.
-Using Hystrix in Spring Cloud requires adding a 'spring cloud starter netflix hybrid' dependency.
-The service provider simulates a call failure by throwing an exception or returning a fixed response, and provides a downgrade interface.
-Service consumers specify the downgrade logic by encapsulating the service provider as a service class and adding a '@ HystrixCommand' annotation on the calling method.
-Add a '@ Enable Hystrix' annotation on the startup class of the application to enable Hystrix functionality.