Management blog SpringCloud learning 8: Hystrix service fusing, service degradation, Dashboard flow monitoring

Keywords: Spring Cloud Cloud Native dashboard

1, Concept: Service avalanche




2, What is Hystrix



Official website information

https://github.com/Netflix/Hystrix/wiki

3, Service fuse

summary

experiment

Step 1: create the service provider springcloud-provider-dept-hystrix-8001

Step 2: modify pom.xml

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

Step 3: modify application.yml

  instance:
    instance-id: springcloud-provider-dept-hystrix-8001  #Modify the default description information on Eureka
    prefer-ip-address: true  # If true, the ip address of the service can be displayed
    

Step 4: modify DeptController

Just add @ HystrixCommand to the method. For a demonstration, write a method to test

//Provide restful service!!
@RestController
public class DeptController {

    @Autowired
    private DeptServiceImpl deptService;


    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGetDept") //If it fails, the following alternative will be called
    public Dept getDept(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("id-->" + id + "The user does not exist or the information cannot be found");
        }
        return dept;
    }

    //alternative
    public Dept hystrixGetDept(@PathVariable("id") Long id) {
        return new Dept()
                .setDeptno(id)
                .setDname("id=>" + id + "No corresponding information, null---@hystrix")
                .setDb_source("not this database in MySQL");
    }

}


Step 5: modify the main class

Then add support for fusing on the main startup class and enable the circuit breaker deptproviderhystrix_ eight thousand and one

//Startup class
@SpringBootApplication
@EnableEurekaClient  //Automatically register with Eureka after the service starts
@EnableCircuitBreaker //Add support for fusing enable circuit breaker
public class DeptProviderHystrix_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProviderHystrix_8001.class, args);
    }
}

Start the cluster, start springcloud-provider-dept-hystrix-8001, and start the 80 project (normal, not feign's 80 project)

Access normal request http://localhost/consumer/dept/get/1

Abnormal access request, http://localhost/consumer/dept/get/10 Instead of displaying 500 errors, the returned results of our alternatives are displayed

4, Service degradation

Service degradation: when the server pressure increases sharply, some services and pages are degraded strategically according to the current business conditions and traffic, so as to release server resources to ensure the normal operation of core tasks. For example, for e-commerce platforms, some services do not occur or delay occurs in peak situations such as 618 and double 11.

Service fusing: for the server, fusing is caused when a service connection times out or is abnormal

Service degradation: for the client, considering the overall website request load, when a service is fused or closed, the service is no longer called. At this time, on the client, we can prepare a FallbackFactory and return a default value. The overall service level is reduced. At least it can be used, which is better than hanging up directly

Step 1: create a DeptClientServiceFallbackFactory under the service package of the spring cloud API project

DeptClientServiceFallbackFactory

//service degradation 
@Component
//Failed callback
public class DeptClientServiceFallbackFactory implements FallbackFactory {

    @Override
    public DeptClientService create(Throwable throwable) {
        return new DeptClientService() {
            @Override
            public Dept queryById(Long id) {
                return new Dept()
                        .setDeptno(id)
                        .setDname("id=>" + id + ",There is no corresponding information. The client provides degraded information. The service has been shut down now")
                        .setDb_source("no data");
            }

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

            @Override
            public List<Dept> queryAll() {
                //...
                return null;
            }

        };
    }
}


Then how to embody it? It's the same. Just add it on the DeptClientService interface of the spring cloud API project

Step 2: configure the service call

@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT", fallbackFactory = DeptClientServiceFallbackFactory.class)

Step 3: yml enable degradation

Step 4: Test

Start the 7001 project, start the 8001 project (normal, not the one of Hystrix), and then start the 80 project of feign

Normal access http://localhost/consumer/dept/get/1

Access a non-existent data

Close the 8001 service provider and visit again http://localhost/consumer/dept/get/1

What does it mean? It shows that the service is passive and the service degradation is manual. However, after the service degradation is enabled, the service is not closed. Accessing a nonexistent data will also return a client-defined return result. When the service is closed, any request for access is a client-defined result.

summary

5, Dashboard flow monitoring

experiment

Client applications

Step 1: create a maven project, spring cloud consumer hystrix dashborad

Step 2: add dependency

<dependencies>
    <!--dashboard Monitoring page-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<!--Eureka-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>
<!--hystrix-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>
 <!--Ribbon-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>
<!--Consumers only need entity classes  +  Web Just rely on-->
<dependencies>
    <dependency>
        <groupId>com.wu</groupId>
        <artifactId>springcloud-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--Hot deployment tool-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

Step 3: configure yml

server:
  port: 9001
hystrix:
  dashboard:
    proxy-stream-allow-list: "*"

Step 4: configure the main class deptconsumerdashboard_ nine thousand and one

@SpringBootApplication
//The server must have a monitoring dependent actor
@EnableHystrixDashboard  //Open the monitoring page http://localhost:9001/hystrix
public class DeptConsumerDashboard_9001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumerDashboard_9001.class, args);
    }
}

Step 5: configure service provider dependencies

Step 6: then start the 9001 project and visit directly http://localhost:9001/hystrix


Now the monitoring page is loaded

How do we monitor it? Monitoring what?

We monitor the classes that implement fuse support (the @ enablercircuitbreaker annotation is added to the main startup class). Here we happen to have a project springcloud-provider-dept-hystrix-8001. There is also a premise that the service class must add actor dependency

Step 7: write the main class of the service provider

Then we modify the main startup class of the springcloud provider Dept hystrix-8001 project and add a Servlet for monitoring

//Startup class
@SpringBootApplication
@EnableEurekaClient  //Automatically register with Eureka after the service starts
@EnableCircuitBreaker //Add support for fusing enable circuit breaker
public class DeptProviderHystrix_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProviderHystrix_8001.class, args);
    }

    //In order to cooperate with monitoring
    //Add a Servlet
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }
}

Start project 7001, start project 9001, start project 8001 of hystrix, and then visit http://localhost:8001/dept/get/1 , if there is any returned data

Then visit http://localhost:8001/actuator/hystrix.stream , you will get some data streams-

then http://localhost:9001/hystrix , fill in the following information







Posted by hammerslane on Wed, 10 Nov 2021 00:09:12 -0800