Hystrix service degradation

The so-called degradation means that when a service has an exception, the server will no longer be called. At this time, the server can prepare a local fallback callback and return a default value.
In this way, although the service level drops, it's better to be available for good or ill than to hang up directly. Of course, it depends on the appropriate business scenario.

Startup class:

package com.wangfajun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker //Open the circuit breaker
@SpringCloudApplication
public class FajunClientTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(FajunClientTestApplication.class, args);
    }
}

Server code demo (if the server is down or an exception is thrown in the serverMethod when the client requests the serverMethod method, the defaultFallback method will be called):

package com.odao.client.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.*;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class FeignClientTestController {

    @HystrixCommand
    @GetMapping(value = "serverMethod")
    public String serverMethod() {
/ * throw new RuntimeException("exception")*/
    return null; 
  }

  public String defaultFallback() {
    return "It's too crowded";
  }

pom:

<!--hystrix-->
 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

Tags: Spring

Posted on Sat, 25 Apr 2020 08:19:35 -0700 by johnh2009