Give it a try

Spring Cloud B2B2C e-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine. Before we start implementing circuit breakers using Spring Cloud Hystrix, we’ll start with some of the things we implemented earlier, including:

  • eureka-serverProject: Service registry, port: 1001
  • eureka-clientProject: service provider with two instance startup ports of 2001

Here we can copy a service consumer we implemented earlier: Eureka-consumer-ribbon, named Eureka-consumer-ribbon-Hystrix. Let’s start to change it in:

Add the spring-cloud-starter-hystrix dependency to the dependencies section of PUM. XML:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>Copy the code

Step 2: EnableHystrix use with @enablecircuitbreaker or @enablehystrix annotation in the main application class:

@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args); }}Copy the code

Note: We can also use the Spring Cloud application here@SpringCloudApplicationAn annotation to modify the application main class, as defined below. We can see that this annotation includes the three annotations we referenced above, which means that a Spring Cloud standard application should include service discovery and circuit breakers.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}Copy the code

Step 3: Change the way the service is consumed, add the ConsumerService class, and migrate the logic in the Controller. Finally, add the @hystrixCommand annotation to the function that executes the logic to specify the service degradation method, such as:

@RestController
public class DcController {

    @Autowired
    ConsumerService consumerService;

    @GetMapping("/consumer")
    public String dc() {
        return consumerService.consumer();
    }

    class ConsumerService {

        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "fallback")
        public String consumer() {
            return restTemplate.getForObject("http://eureka-client/dc", String.class);
        }

        public String fallback() {
            return "fallback"; }}}Copy the code

Let’s examine some of the basic features Hystrix brings to the table above. We start all the Services involved and then go to localhost:2101/consumer to get a normal return, such as: Services: [Eureka-consumer-ribbon-hystrix, eureka-client].

To trigger the service degradation logic, we can add some delay to the service provider Eureka-client’s logic, for example:

@GetMapping("/dc")
public String dc() throws InterruptedException {
    Thread.sleep(5000L);
    String services = "Services: " + discoveryClient.getServices();
    System.out.println(services);
    return services;
}Copy the code

After restarting eureka-Client and attempting to access localhost:2101/consumer, we will get fallback. From the Eureka-Client console, we can see that the service provider outputs the result that it was intended to return, but since there was a 5 second delay before returning, and the service consumer triggered the service request timeout exception, the service consumer executed it using the degrade logic specified in the HystrixCommand annotation. The result of the request therefore returns a fallback. This mechanism provides basic protection for its own services, and also provides an automatic service degradation switchover mechanism for abnormal situations.

E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine