Hystrix work step description and service monitoring hystrixDashboard

Table of contents

1. Step description and process analysis

2. Service monitoring hystrixDashboard


Official website: How it Works Netflix/Hystrix Wiki GitHub

1. Step description and process analysis

 

 

1     Create a HystrixCommand (used when the dependent service returns a single operation result) or HystrixObserableCommand (used when the dependent service returns multiple operation results) object.
2    command execution. Among them, HystrixCommand implements the following two execution methods:

execute() : Executes synchronously, returning a single result object from dependent services, or throwing exceptions in case of errors. queue() : Asynchronous execution, directly returns a Future object, which contains a single result object to be returned when the service execution ends.

And HystrixObservableCommand implements the following two execution methods:

observe() : Returns an Observable object, which represents multiple results of the operation. It is a Hot Observable (regardless of whether the "event source" has a "subscriber", the event will be published after creation, so for each Hot Observable A "subscriber" may start halfway through the "event source", and may only see part of the entire operation). toObservable() : It will also return an Observable object, which also represents multiple results of the operation, but it returns a Cold Observable (events will not be published when there is no "subscriber", but wait until there is a "subscriber" subscribers" before publishing events, so for Cold Observable subscribers, it is guaranteed to see the entire process of the entire operation from the beginning).
3     If the request cache function of the current command is enabled and the command cache hits, the cached result will be returned immediately in the form of an Observable object.
4     Check that the circuit breaker is on. If the circuit breaker is open, then Hystrix will not execute the command, but transfer to the fallback processing logic (step 8); if the circuit breaker is closed, check whether there are available resources to execute the command (step 5).
5     Whether the thread pool/request queue/semaphore is full. If the command depends on the dedicated thread pool and request queue of the service, or the semaphore (when not using the thread pool) is already full, then Hystrix will not execute the command, but will transfer to the fallback processing logic (step 8) .
6     HystrixHystrixCommand.run() : Returns a single result, or throws an exception. HystrixObservableCommand.construct(): Return an Observable object to emit multiple results, or send error notifications through onError.
7     Hystrix will report "success", "failure", "rejection", "timeout" and other information to the circuit breaker, and the circuit breaker will maintain a set of counters to count these data. The circuit breaker will use these statistics to decide whether to open the circuit breaker to "fuse/short circuit" the request of a dependent service.
8     When the command execution fails, Hystrix will enter fallback to try to roll back processing. We usually call this operation "service downgrade". The conditions that can cause service degradation processing are as follows: Step 4: The current command is in the "fuse/short circuit" state, and the circuit breaker is open. Step 5: When the thread pool, request queue or semaphore of the current command is full. Step 6: When HystrixObservableCommand.construct() or HystrixCommand.run() throws an exception.
9     When the Hystrix command is successfully executed, it will return the processing result directly or in the form of Observable.


Tips: If we don't implement the downgrade logic for the command or throw an exception in the downgrade processing logic, Hystrix will still return an Observable object, but it will not emit any result data, but notify the command to interrupt the request immediately through the onError method, and The exception that caused the command to fail is sent to the caller via the onError() method.

2. Service monitoring hystrixDashboard

Note: The new version of Hystrix needs to specify the monitoring path in the main startup class MainAppHystrix8001

Otherwise, Unable to connect to Command Metric Stream. error will appear

 

/**
 *此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
 *ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
 *只要在自己的项目里配置上下面的servlet就可以了
 */
@Bean
public ServletRegistrationBean getServlet() {
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
}

 9001 Pom file

    <dependencies>
        //引入dashboard依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

9001 monitoring 8001

Note that the monitored end needs to introduce actuator dependencies

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

 

 

how to see

 

It is also possible to monitor multiple services to reach the following states

 

 

Guess you like

Origin blog.csdn.net/m0_62946761/article/details/128794373