This is the 16th day of my participation in the Gwen Challenge.

1. An overview of the

Hystrix, Netflix’s open-source fault-tolerant component for distributed systems, is used extensively in Netflix’s projects. Hystrix means “porcupine”, covered in quills to protect itself.

Hystrix is a delay and fault tolerant library designed to isolate remote systems, services, and third-party libraries, preventing cascading failures, and enabling resilience in complex distributed systems.

Designed to

  1. Protects and controls delays and failures of dependencies accessed through third-party client libraries, usually over a network.
  2. Preventing cascading failures in complex distributed systems.
  3. Fail fast, recover fast.
  4. Step back and demote as gracefully as possible.
  5. Enable near real time monitoring, alerts, and operational control.

Problem solved

In distributed system environment, similar dependencies between services are very common. A business invocation usually depends on multiple underlying services. As shown in the figure below, for synchronous invocation, when the inventory service is unavailable, the thread of commodity service request will be blocked. When the inventory service is called with a large number of requests, the whole commodity service resource may be exhausted and the service cannot be provided externally. And this unavailability can travel up the request invocation chain, a phenomenon known as the avalanche effect.

In such cases of service-to-service or system-to-system dependencies, a mechanism is needed to handle delays and failures and keep the entire system in a usable and stable state, and that’s where Hystrix comes in.

2. Introduction to Hystrix

Create project Cloud-Hystrix-Practice

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring.cloud-version>Hoxton.SR3</spring.cloud-version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
Copy the code

Creating a registry

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
Copy the code

The configuration file

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
Copy the code

Start the class

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code

Create the module cloud-order-service

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
Copy the code

The configuration file

server:
  port: 8888
spring:
  application:
    name: cloud-order-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
Copy the code

Control layer code

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @GetMapping("getOrder")
    public String getOrder(@RequestParam String name) {
        returnorderService.getOrder(name); }}Copy the code

Service layer code

public interface IOrderService {
    String getOrder(String name);
}
Copy the code
@Service
public class OrderServiceImpl implements IOrderService {
    // Service fuses: Fuses are enabled - within 10 seconds -10 requests -60% of requests fail - Fuses are triggered
    @HystrixCommand(fallbackMethod = "defaultFallBack", CommandProperties = {// Whether to enable circuitBreaker @hystrixProperty (name = "circuitBreaker. Enabled ", value = "true"), After / / the number of requests to calculate @ HystrixProperty (name = "circuitBreaker. RequestVolumeThreshold", value = "10"), / / @ HystrixProperty sleep time window (name = "circuitBreaker. SleepWindowInMilliseconds", value = "10000"), / / what is the error rate trip, Fusing @ HystrixProperty (name = "circuitBreaker errorThresholdPercentage", value = "60"),})
    @Override
    public String getOrder(String name) {
        if ("hystrix".equalsIgnoreCase(name)) {
            return "Correct access";
        } else {
            throw new RuntimeException("Incorrect access"); }}public String defaultFallBack(String name) {
        return "this is defaultFallBack method!"; }}Copy the code

Start the class

@SpringBootApplication
@EnableDiscoveryClient
/ / open Hystrix
@EnableHystrix
public class OrderServiceApplication {

    public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); }}Copy the code

Start the registry and cloud-order-service

During a visit to http://localhost:8888/order/getOrder? If name=hystrix, the parameter check is correct and the system returns to normal. If an exception occurs when the parameter passed is not required in the code, the defaultFallBack method is automatically called and a friendly message is returned.

So much for getting started with Hystrix

1. @enablehystrix Enable the Hystrix circuit breaker

HystrixCommand(fallbackMethod = “defaultFallBack”) defines the fallback method