springcloud Hystrix应用

1.hystrix作用

  • 对通过第三方客户端库访问的依赖项的延迟和故障进行自我保护和控制
  • 在复杂的分布式系统中阻止级联故障(服务雪崩)
  • 快速失败,快速恢复
  • 回退,优雅降级

参考SpringCloud之Hystrix容错保护原理及配置

2. hystrix如何使用

只需要在pom中添加spring-cloud-starter-netflix-hystrix依赖,即可开始使用

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

这里自动装配一个RestTemplate供后续使用:

package com.gupaoedu.example.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfiguration {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){
        return restTemplateBuilder.build();
    }
}

那么hystrix如何触发降级呢?在hystrix中有三种降级方案:超时触发降级, 熔断触发降级,feign客户端调用触发降级

2.1 请求超时触发降级

添加如下代码,重启user-service,order-service不启动:

  @HystrixCommand(fallbackMethod = "timeoutFallback")
    @GetMapping("/hystrix/timeout")
    public String queryOrderTimeout(){
        return restTemplate.getForObject("http://localhost:8082/orders/", String.class);
    }
    public String timeoutFallback(){
        return "请求超时";
    }

order-service未启动,那么 restTemplate.getForObject会请求失败,触发降级,进入timeoutFallback方法。
在这里插入图片描述
hystrix默认的超时时间是1s , 在HystrixCommandProperties 的构造方法中有默认配置:

 this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);

在这里插入图片描述
把这个超时时间修改为3000ms,order-service中加2s睡眠:

@RestController
public class OrderServiceImpl implements OrderService {
    @Override
    public String orders() throws InterruptedException {
        Thread.sleep(2000);
        return "Return All Orders";
    }

    @Override
    public int insert(OrderDto dto) {
        return 1;
    }
}

启动order-service,重启user-service,服务再次请求,可以看到控制台上面显示3s后返回请求超时的降级结果:

    @HystrixCommand(fallbackMethod = "timeoutFallback",
      commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})

在这里插入图片描述

2.2 请求被熔断触发降级

package com.gupaoedu.example.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.naming.Name;

/**
 * Hystrix服务熔断测试
 */
@RestController
public class HystrixController {
    @Autowired
    public RestTemplate restTemplate;

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // 熔断的最小请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),   // 时间窗口 5s
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "20")   // 触发熔断的请求失败比例,这几个条件综合起来就是熔断策略
            }, fallbackMethod = "fallback")
    @GetMapping("/hystrix/order/{num}")   // 要在启动类上面加上注解@EnableCircuitBreaker 开启熔断
    public String getOrder(@PathVariable("num") int num) {
        // 测试正常请求与请求熔断 当发生熔断后,正常请求也会走fallback,熔断恢复后,正常请求恢复
        // 启动zookeeper,kafka, eurekaserver,configserver,user-service,分别请求 http://localhost:9091/hystrix/order/1 http://localhost:9091/hystrix/order/2
        // 可以看到num=2的请求正常,num=1的请求
        if (num % 2 == 0) {
            return "正常请求";
        }
        return restTemplate.getForObject("http://localhost:8082/orders/", String.class);
    }

    public String fallback(int num) {
        return "服务繁忙,稍后再试";
    }
    //execution.isolation.thread.timeoutInMilliseconds 修改超时时间为3s
    @HystrixCommand(fallbackMethod = "timeoutFallback",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
    @GetMapping("/hystrix/timeout")
    public String queryOrderTimeout(){
        return restTemplate.getForObject("http://localhost:8082/orders/", String.class);
    }
    public String timeoutFallback(){
        return "请求超时,稍后重试";
    }
}

在启动类上面加上注解@EnableCircuitBreaker 开启熔断

  • 服务熔断演示
    http://localhost:8081/hystrix/order/1 这个请求由于order-service未启动,服务不可达,会走fallback逻辑,http://localhost:8081/hystrix/order/2 这个请求返回“正常请求”给调用者。当多次请求http://localhost:8081/hystrix/order/1 后,发现原本正常响应的请求http://localhost:8081/hystrix/order/2 也走了fallback逻辑,也就是说, 熔断开启之后,后续的正常请求也走了降级逻辑 ,然后过了几秒钟,http://localhost:8081/hystrix/order/2 这个请求又可以正常访问了,也就是熔断自动恢复了。

在这里插入图片描述

  • 熔断触发的条件
    10s内发送了20次请求,失败率超过50%,5s后熔断恢复(hystrix默认值,发生熔断后,后续5s内的任何请求都走降级)
  • 熔断会自动恢复(5s后尝试发起请求到远程服务器,如果发现远程服务能够正常响应,则关闭熔断,否则继续熔断)
  • 熔断策略(定义在HystrixCommandProperties.java中)
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),  // 开启熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // 触发熔断的最小请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),   // 熔断持续时间
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "20")   // 触发熔断的请求失败比例

2.3 openfeign集成hystrix

上面是通过restemplate来进行请求,多数情况下,会使用openfeign来做RPC请求:
在order-service-api中添加feigiclient代码,然后install到本地:

package com.gupaoedu.springcloud.clients;

import com.gupaoedu.springcloud.OrderService;
import com.gupaoedu.springcloud.dto.OrderDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;

//@FeignClient("order-service")
// 使用openfeign的场景下配置熔断
@FeignClient(value = "order-service",
        fallback = OrderServiceFeignClient.OrderServiceFeiginClientFallback.class)
public interface OrderServiceFeignClient extends OrderService {

    @Component // 这个类也需要被spring加载
    class OrderServiceFeiginClientFallback implements OrderServiceFeignClient {

        @Override
        public String orders() throws InterruptedException {
            return "查询订单失败";
        }

        @Override
        public int insert(OrderDto dto) {
            System.out.println("insert失败");
            return -1;
        }
    }
}

user-service中添加order-servie-api的pom依赖,整合openfeign与hystrix:

package com.gupaoedu.example.controller;

import com.gupaoedu.springcloud.clients.OrderServiceFeignClient;
import com.gupaoedu.springcloud.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HystrixFeignController {

    @Autowired
    private OrderServiceFeignClient orderServiceFeignClient;
    @GetMapping("/hystrix/feign/order")
    public String queryOrder(){
        try {
            return orderServiceFeignClient.orders();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "failed";
    }

    @PostMapping("/hystrix/feign/order")
    public String insertOrder(OrderDto orderDto){
        return orderServiceFeignClient.insert(orderDto) > 0 ? "SUCCESS" : "FAILED";
    }
}

配置文件中开启feignclent降级策略:

feign:
  hystrix:
    enabled: true

注意:user-servcie中启动类中要扫描feignclient所在的包: “com.gupaoedu.springcloud.clients” ,启动类中也要开启feign客户端:@EnableFeignClients(basePackages = “com.gupaoedu.springcloud.clients”)

使用openfeign时,无法在接口上面通过注解配置hystrix的一些属性,只能放到配置文件中:

spring:
  application:
    name: user-service
  cloud:
    bus:
      enabled: true
      trace:
        enabled: true
        refresh:
          enabled: true
      kafka:
        bootstrap-servers: localhost:9092 #kafka地址
        consumer:
          group-id: user-service # 分组,不同的组可以收到相同的消息
server:
  port: 8081
# 开启dashboard监控:hystris.stream,然后重启user-service
management:
  endpoints:
    web:
      exposure:
        include: ["health","info","refresh","hystrix.stream"] # refresh,hystrix.stream
  # 打开feignclent降级策略
feign:
  hystrix:
    enabled: true

# openfeign只能通过配置文件设置hystrix参数,接口中无法通过HystrixProperty注解方式配置属性
hystrix:
  command:
    default: #default为全局配置,对某个方法,
      execution:
        timeout:
          enable: true
        isolation: #这里isolation是 hystrix.command.default.execution.isolation ,位置配置错误,会导致feign调接口失败!!!!
          thread:
            timeoutInMilliseconds: 3000 #hystrix超时时间3s,使用feignclient时,ribbon会有一个默认超时时间2s,因此这里需要同时设置ribbond的超时时间ribbon.ReadTimeout
    OrderServiceFeignClient#orders():
      execution:
        isolation:
          strategy: SEMAPHORE
          semaphore:
            maxConcurrentRequests: 50
    OrderServiceFeignClient#insert():
      execution:
        isolation:
          strategy: THREAD
  threadpool: # 线程池隔离时,需要配置线程池资源
    order-service: # 针对order-service配置线程池资源
      coreSize: 2
      maxQueueSize: 1000
      queueSizeRejectionThreshold: 800 # 超过最大线程数开始拒绝请求
ribbon:
  ReadTimeout: 10000  # Ribbon的超时时间一定要大于hystrix的超时时间,这样才能测试出来hystrix是否起作用
  ConnectionTimeout: 10000 #单位ms

  # 刷新外部配置
  # 1.添加springboot-starter-actuator依赖;
  # 2.开放自动刷新端点,同时还需要在刷新配置的地方加上@Refresh注解
  # 3.通过调用/actuator/refresh接口刷新配置
#management:
#  endpoints:
#    web:
#      exposure:
#        include: refresh
env: lchtest

2.4 资源隔离触发降级

隔离的方式有很多种,包括平台隔离,部署隔离,业务隔离,服务隔离,资源隔离,系统中的资源无非CPU, 内存,线程,这里资源隔离是指线程池资源,如下图,在高流量的情况下,一个后端依赖项的延迟可能导致所有服务器上的所有资源在数秒内被快速消耗完,
在这里插入图片描述
此时就需要针对不同的请求分配不同的线程池来进行请求处理来实现隔离:
在这里插入图片描述
上图中除了线程池隔离,还有信号量隔离,信号量隔离主要是通过控制并发请求量,防止请求线程大面积阻塞,从而达到限流和防止雪崩的目的。
由于Hystrix默认使用线程池做线程隔离,使用信号量隔离需要显示地将属性execution.isolation.strategy设置为ExecutionIsolationStrategy.SEMAPHORE,同时配置信号量个数,默认为10。客户端需向依赖服务发起请求时,首先要获取一个信号量才能真正发起调用,由于信号量的数量有限,当并发请求量超过信号量个数时,后续的请求都会直接拒绝,进入fallback流程。
下面演示一下针对不同方法设置线程池隔离和信号量隔离:

package com.gupaoedu.example.controller;

import com.gupaoedu.springcloud.clients.OrderServiceFeignClient;
import com.gupaoedu.springcloud.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HystrixFeignController {

    @Autowired
    private OrderServiceFeignClient orderServiceFeignClient;
    @GetMapping("/hystrix/feign/order")
    public String queryOrder(){
        try {
            return orderServiceFeignClient.orders();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "failed";
    }

    @PostMapping("/hystrix/feign/order")
    public String insertOrder(OrderDto orderDto){
        return orderServiceFeignClient.insert(orderDto) > 0 ? "SUCCESS" : "FAILED";
    }
}

在这里插入图片描述

2.5 hystrix dashboard监测请求

  • 新建一个hystrix dashboard项目:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>spring-cloud-order-service-8082</artifactId>
        <groupId>com.gupaoedu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.gupaoedu.example</groupId>
    <artifactId>spring-cloud-hystrix-dashboard-9092</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-hystrix-dashboard-9092</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
    </properties>
    <dependencies>
        <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 启动类加上@EnableHystrixDashboard注解开启dashboard
    在这里插入图片描述
  • 在被监控的项目中引入hystrix-dashboard依赖和spring-boot-starter-actuator的依赖
  <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <!-- 要通过dashboard监控user-service,必须要引入actuator的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  • 被监控服务开启hystrix.trim这个端点:
# 开启dashboard监控:hystris.stream,然后重启user-service
management:
  endpoints:
    web:
      exposure:
        include: ["health","info","refresh","hystrix.stream"] # refresh,hystrix.stream

启动eureka-server, configserver, dashboard项目,user-service order-service项目,访问http://localhost:8081/actuator/hystrix.stream,会看到hystrix-dashboard监控收集到的服务信息:
在这里插入图片描述
浏览器访问http://localhost:9093/hystrix(dashboard项目),会看到hystrix监控面板如下:
在这里插入图片描述
在地址栏输入被监控服务的hystrix端点地址: http://localhost:8081/actuator/hystrix.stream,点击monitor stream按钮就会进入hystrix dashborad监控面板:
在这里插入图片描述

通过jemter来对GET http://localhost:8081/hystrix/feign/order 接口进行压测,来验证openfeign整合hystrix后服务降级:
可以看到,当请求数量达到阈值时,Circuit立即变成Open状态,随后又变成closed状态
在这里插入图片描述

参考文章:
Hystrix原理与实战

https://blog.youkuaiyun.com/lifupingcn/article/details/88030431?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-6&spm=1001.2101.3001.4242

配套代码:
springcloud hystrix应用

遇到的坑:

  1. dashboard项目没有配置hystrix.dashboard.proxy-stream-allow-list=localhost, 导致查看监控时报错:
http://localhost:8081/actuator/hystrix.stream is not in the allowed list of proxy host names.  If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.

在这里插入图片描述
2. hystrix超时配置项配错,导致feignclient调用order-servcie时直接走了服务降级,应该是使用了hytstrix的默认超时时间(1s), 而设置的ribbon超时时间是10s, order-servcie 对应方法中又sleep了2s,导致在直接走服务降级。错误配置如下:
在这里插入图片描述
正确配置:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值