The most complete introductory SpringCloud tutorial ever, taking you deep from scratch ♂ Learning - Hystrix Components: Service Degradation and Dashboard Flow Monitoring

Posted by deathrider on Mon, 10 Jan 2022 18:32:17 +0100

SpringCloud (10) - Hystrix Components: Service Degradation and Dashboard Flow Monitoring

service degradation

Service downgrade means that when server pressure increases sharply, some services and pages are not handled strategically according to actual business conditions and traffic, or handled in a simple way, thereby releasing server resources to ensure the normal or efficient operation of core business. To put it plainly, try to give system resources to high priority services as much as possible.

Resources are limited and requests are unlimited. If you do not downgrade the service during the concurrent peak period, on the one hand, it will certainly affect the performance of the overall service. Severe cases may cause downtime of some important services unavailable. Therefore, in the rush hour, in order to ensure the availability of core function services, some services should be downgraded. For example, when Double 11 was active, downgraded all non-transactional service systems, such as viewing ants in the forest, viewing historical orders, and so on.

What are the scenarios in which service downgrades are mainly used? When the overall load of the entire micro-service architecture exceeds the preset upper threshold or when upcoming traffic is expected to exceed the preset threshold, some non-essential or non-urgent services or tasks can be delayed or suspended in order to ensure that important or basic services can function properly.

Depending on the business, the downgrade can delay services, such as adding credits to users, but putting them in a cache and waiting for the service to stabilize before executing. Or shut down services at a granular scale, such as turning off recommendations from related articles.

Turn off some edge services in the micro-service system to ensure that the system core services are functioning properly
Receiving Data

Use Service Demotion

1. Add a service downgrade class FallbackFactory to the springcloud-api

package com.study.springcloud.service;

import com.study.springcloud.pojo.Dept;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory {
    @Override
    public Object create(Throwable throwable) {
        return new DeptClientService() {
            @Override
            public Dept queryById(Long id) {
                return new Dept()
                        .setDeptno(id)
                        .setDname("this id=>"+id+"There is no corresponding information, the client provides degraded information, the service has been shut down")
                        .setDb_source("no data");
            }
			//Jiaqun 1025684353 Chat with Blowwater-->
            @Override
            public List<Dept> queryAll() {
                return null;
            }

            @Override
            public boolean addDept(Dept dept) {
                return false;
            }
        };
    }
}

2. Specify the downgraded configuration class DeptClientServiceFallBackFactory in DeptClientService

3. Configure appllication in consumer springcloud-consumer-dept-openfeign. Yaml configuration fuse downgrade

server:
  port: 80

#Turn on demoted openfeign.hystrix
feign:
  hystrix:
    enabled: true
# Jiaqun 1025684353 Chat with Blowwater-->
#EurekaClient Configuration
eureka:
  client:
    register-with-eureka: false #Unlike registering yourself in eureka
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

Receiving Data

4. Running tests

1. Launch the Eureka cluster registry springcloud-eureka-7001 and springcloud-eureka-7002
2. Start service provider springcloud-provider-dept-8001
3. Start consumer springcloud-consumer-dept-openfeign

4. Turn off service provider springcloud-provider-dept-8001

The difference between service breakdown and downgrade

  • Service Fuse --> Server: A service timed out or abnormal, causing a fuse ~, similar to a fuse (self-fuse)

  • Service downgrade - > Client: Considering the load requested from the overall website, when a service is broken or shut down, the service will no longer be invoked. At this time, on the client side, we can prepare a FallBackFactory and return a default value (default value). This can cause overall service degradation, but it's better to use it for good than to hang it up directly.

  • The trigger causes are different. Service outage is usually caused by a service (downstream service) failure, while service downgrade is generally considered from the overall load; The hierarchy of management objectives is different. Fusion is actually a framework-level process, with each microservice requiring (no hierarchy) and downgrading typically requiring a hierarchy of businesses (for example, downgrading typically starts with the most peripheral service).

  • The implementation is different, and service downgrades are code-invasive (completed by the controller or automatically downgraded), with a fuse commonly referred to as self-fusing.

Dashboard Stream Monitoring

One of the main advantages of Hystrix Dashboard is that it collects a set of measures for each HystrixCommand. Hystrix dashboard shows the status of each breaker in an efficient way

1. New consumer module springcloud-sonsumer-hystrix-dashboard

Receiving Data

2. Add Related Dependencies

 <dependencies>
        <!--Hystrix rely on-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--Introduce hystrix dashboard rely on-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.study</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
         <!--Jiaqun 1025684353 Chat with Blowwater-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

3. Write application.yaml profile

server:
  port: 9001

hystrix:
  dashboard:
    proxy-stream-allow-list: localhost

4. Write the main startup class, add dashboard monitoring comment

package com.study.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard//Turn on dashboard monitoring
public class DeptConsumerDashboard_9001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumerDashboard_9001.class,args);
    }
}

5. Check whether service providers add monitoring information dependencies

6. Start testing

7. Add hystrix dependencies and monitoring to the main startup class under the service provider to be monitored (springcloud-provider-dept-hystrix-8001 module)

Receiving Data
Add hystrix dependencies

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

Add monitoring

package com.study.springcloud;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;

//Startup Class
@SpringBootApplication
@EnableEurekaClient//Automatically register in Eureka after service startup!
@EnableDiscoveryClient //Service Discovery
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
     //Jiaqun 1025684353 Chat with Blowwater-->

    //Add a Servlet
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        //Visiting this page is the monitoring page
        servletRegistrationBean.addUrlMappings("/actuator/hystrix.stream");
        return servletRegistrationBean;
    }
}

9. Start the Eureka cluster springclou-eureka-7001, then start the service provider springcloud-provider-dept-hystrix-8001

1,

2,

Receiving Data
3,

4,

5,

6,

Finally, I wish you all early learning and success, get a satisfactory offer, rapid promotion and pay raise, and reach the peak of your life. Give me three consecutive support if you can. See you next time.

Receiving Data

Topics: Java Spring Spring Cloud Microservices microservice