微服务SpringCloud开弓之Hystrix熔断器「八」

二维紧束缚模型求解程序 假如我们已经推导出了二维紧束缚模型的哈密顿量矩阵H(kx,ky)的解析形式,那么在整个布里渊区对角化矩阵就可以得到能带。从技术上说这里面有以下几个问题。 1. 缺少实空间和倒空间的坐标,使得能带难以和布里渊区高对称点对应。 2. 最容易处理的布里渊区是矩形,对于其他多边形,如果全布里渊区mesh画费米面有一定技术困难。 因此一个好的处理框架是首先给出实空间原子坐标,给出实空间哈密顿量,... 阅读详情

官网资料

1、概述

  • 分布式系统面临的问题

    分布式系统面临的问题
    复杂分布式体系结构中的应用程序 有数10个依赖关系,每个依赖关系在某些时候将不可避免地失败

在这里插入图片描述

  • 服务雪崩
    多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的"扇出"。
    如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应”.

    对于高流量的应用来说,单-的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。

    所以,通常当你发现一个模块下的某个实例失败后,这时候这个模块依然还会接收流量,然后这个有问题的模块还调用了其他的模块,这样就会发生级联故障,或者叫雪崩。

  • 是什么
    在这里插入图片描述

  • 作用

    服务降级

    服务熔断

    接近实时的监控

2、HyStrix重要概念

  • 服务降级(fallback)

    1、 服务器忙,请稍后再试,不让客户端等待并立刻返回一个友好提示,fallback

    2、哪些情况会发出降级

    程序运行异常
    超时
    服务熔断触发服务降级
    线程池/信号量也会导致服务降级
  • 服务熔断(break)

    类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示

    就是保险丝 : 服务的降级->进而熔断->恢复调用链路

  • 服务限流(flowlimit)

    秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行

3、hystrix案例

构建案例

1、新建cloud-provider-hystrix-payment8001

2、POM

  <!--hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--web-->
        <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>
        <!--引入自定义的api通用包,可用使用Payment支付Entity-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </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>

3、yml

server:
  port: 8001
spring:
  application:
    name: cloud-provider-hystrix-payment
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

4、主启动类

@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class, args);
    }
}

5、业务类

  • service

    @Service
    public class PaymentService {
        public String paymentInfo_OK(Integer id) {
            return "线程池:" + Thread.currentThread().getName() + " paymentInfo_OK,id:" + id + "\t" + "O(∩_∩)O哈哈~";
        }
    
        public String paymentInfo_TimeOut(Integer id) {
            int timeNumber = 3;
            try {
                // 暂停3秒钟
                TimeUnit.SECONDS.sleep(timeNumber);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,id:" + id + "\t" +
                    "O(∩_∩)O哈哈~  耗时(秒)" + timeNumber;
        }
    }
    
  • controller

    @RestController
    @Slf4j
    public class PaymentController {
        @Resource
        private PaymentService paymentService;
    
        @Value("${server.port}")
        private String servicePort;
    
        @GetMapping("/payment/hystrix/ok/{id}")
        public String paymentInfo_OK(@PathVariable("id") Integer id) {
            String result = paymentService.paymentInfo_OK(id);
            log.info("*****result:" + result);
            return result;
        }
    
        @GetMapping("/payment/hystrix/timeout/{id}")
        public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
            String result = paymentService.paymentInfo_TimeOut(id);
            log.info("*****result:" + result);
            return result;
    
        }
    }
    

6、测试

正常的方法

  • http://localhost:8001/payment/hystrix/ok/31

在这里插入图片描述
延时的方法

  • http://localhost:8001/payment/hystrix/timeout/31

在这里插入图片描述

在这里插入图片描述
解决办法

ribbon:
  # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

高并发的测试:

开启Jmeter,来20000个并发压死8001,20000个请求都去访问paymentInfo_TimeOut服务

在这里插入图片描述

http://localhost:8001/payment/hystrix/timeout/31

http://localhost:8001/payment/hystrix/ok/31

都在转圈,没延迟的也受到了高并发的影响

原因:

tomcat的默认工作线程数被打满了,没有多余的线程来分解压力和处理

结论

上面还只是服务提供者8001自己测试,假如此时外部的消费者80也来访问,那消费者只能干等,最终导致消费端80不满意,服务端8001直接被拖死

80新建加入

1、新建cloud-consumer-feign-hystrix-order80

2、POM

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <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>
        <!--热部署-->
        <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>

3、YML

server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

4、主启动

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableFeignClients
public class OrderHystrixMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderHystrixMain80.class, args);
    }
}

5、业务类

  • PaymentHystrixService

    @Component
    @FeignClient(value = "cloud-provider-hystrix-payment")
    public interface PaymentHystrixService {
    
        @GetMapping("/payment/hystrix/ok/{id}")
        String paymentInfo_OK(@PathVariable("id") Integer id);
    
    
    
        @GetMapping("/payment/hystrix/timeout/{id}")
        String paymentInfo_TimeOut(@PathVariable("id") Integer id);
    
    }
    
    
  • OrderHysrixController

    @RestController
    @Slf4j
    public class OrderHysrixController {
    
        @Resource
        private PaymentHystrixService paymentHystrixService;
    
        @GetMapping(value ="/consumer/payment/hystrix/ok/{id}")
        public String paymentInfoOK(@PathVariable("id") Integer id){
            return paymentHystrixService.paymentInfo_OK(id);
        }
    
        @GetMapping(value ="/consumer/payment/hystrix/timeout/{id}")
        public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
            return paymentHystrixService.paymentInfo_TimeOut(id);
        }
    

6、正常测试

http://localhost/consumer/payment/hystrix/ok/32 快速正常访问

7、高并发测试

  • 消费者80微服务再去访问的OK服务8001地址

  • 开启Jmeter,来20000个并发压死8001

  • 访问http://localhost/consumer/payment/hystrix/ok/32

    结果:

    • 要么转圈圈

    • 要么报错

在这里插入图片描述

故障和导致现象
  • 8001同一层次的其他接口被困死,因为tomcat线程池里面的工作线程已经被挤占完毕
  • 80此时调用8001,客户端访问响应缓慢,转圈圈
上述结论
  • 正因为有上述故障或不佳表现 才有我们的降级/容错/限流等技术诞生
如何解决?解决的要求
  • 超时导致服务器变慢(转圈)

    超时不再等待

  • 出错(宕机或程序运行出错)

    超时不再等待

  • 解决

    1、对方服务(8001)超时了,调用者(80)不能一直卡死等待,必须有服务降级

    2、对方服务(8001)down机了,调用者(80)不能一直卡死等待,必须有服务降级

    3、对方服务(8001)ok,调用者(80)自己有故障或有自我要求(自己的等待时间小于服务提供者)

4、服务降级

降级配置
  • @HystrixCommand
8001先从自身找问题
  • 设置自身调用超时时间的峰值,峰值内可以正常运行, 超过了需要有兜底的方法处理,做服务降级fallback
8001fallback
  • 业务类启用

    @HystrixCommand报异常后如何处理

    PaymentService

      @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
                @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
        })
        public String paymentInfo_TimeOut(Integer id) {
            int timeNumber = 5;
            try {
                // 暂停3秒钟
                TimeUnit.SECONDS.sleep(timeNumber);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "线程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,id:" + id + "\t" +
                    "O(∩_∩)O哈哈~  耗时(秒)" + timeNumber;
        }
    
        public String  paymentInfo_TimeOutHandler(Integer id){
            return "程序运行繁忙或报错,请稍后再试*****"+"当前线程: "+Thread.currentThread().getName()+id+"\t "+"orz!";
        }
    
    

    分析:

    一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbckMethod调用类中的指定方法

  • 主启动类激活

    @EnableCircuitBreaker

    @SpringBootApplication
    @EnableEurekaClient
    @EnableCircuitBreaker
    public class PaymentHystrixMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentHystrixMain8001.class, args);
        }
    }
    

    结果:

    访问http://localhost:8001/payment/hystrix/timeout/31 超时走自定义回调函数
    在这里插入图片描述

80fallback

我们自己配置过的热部署方式对java代码的改动明显,但对@HystrixCommand内属性的修改建议重启微服务

  • pom

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

    server:
      port: 80
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: true
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    feign:
      hystrix:
        enaled: true
    
  • 主启动 @EnableHystrix

    @SpringBootApplication
    @EnableEurekaClient
    @EnableHystrix
    @EnableFeignClients
    public class OrderHystrixMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderHystrixMain80.class, args);
        }
    }
    
  • 业务类

    controller进行业务降级

    @RestController
    @Slf4j
    public class OrderHysrixController {
        @Resource
        private PaymentHystrixService paymentHystrixService;
    
        @GetMapping(value ="/consumer/payment/hystrix/ok/{id}")
        public String paymentInfoOK(@PathVariable("id") Integer id){
            return paymentHystrixService.paymentInfo_OK(id);
        }
    
        @GetMapping(value ="/consumer/payment/hystrix/timeout/{id}")  @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {         @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "1500")   })
        @HystrixCommand
        public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
            return paymentHystrixService.paymentInfo_TimeOut(id);
        }
    
        public  String  paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
            return "我是消费者80,对方支付系统繁忙"+"当前线程: "+Thread.currentThread().getName()+id+"\t "+"mgd!";
    
        }
    }
    
  • 测试结果
    在这里插入图片描述

存在的问题
  • 每个业务方法对应一个兜底的方法,代码膨胀
  • 统一和自定义的分开
解决办法
  • 每个方法配置一个造成的冗余

    1、feign接口系列

    2、@DefaultProperties(defaultFallback="")

在这里插入图片描述
3、controller配置

@RestController
@Slf4j
@DefaultProperties(defaultFallback="paymentTimeOutFallbackMethod_Global")
public class OrderHysrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value ="/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id){
        return paymentHystrixService.paymentInfo_OK(id);
    }

    @GetMapping(value ="/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }

    public  String  paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "我是消费者80,对方支付系统繁忙"+"当前线程: "+Thread.currentThread().getName()+id+"\t "+"mgd!";

    }

    public  String  paymentTimeOutFallbackMethod_Global(){
        return "我是全局异常"+"当前线程: "+Thread.currentThread().getName()+"\t "+"mgd!";

    }
}
  • 和业务逻辑混在一起造成代码混乱

    服务降级,客户端去调用服务端,碰上服务端宕机或关闭

    本次案例服务降级处理是在客户端80实现完成,与服务端8001没有关系 只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦

​ 1、修改cloud-consumer-feign-hystrix-order80

​ PaymentFeignClientService接口

@Component
@FeignClient(value = "cloud-provider-hystrix-payment" ,fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {

 @GetMapping("/payment/hystrix/ok/{id}")
 String paymentInfo_OK(@PathVariable("id") Integer id);

 @GetMapping("/payment/hystrix/timeout/{id}")
 String paymentInfo_TimeOut(@PathVariable("id") Integer id);

}

2、根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现接口,统一为接口里面的方法进行异常处理

3、PaymentFallbackService类实现PaymentFeginService接口

import org.springframework.stereotype.Component;

@Component
public class PaymentFallbackService implements PaymentHystrixService{
    @Override
    public String paymentInfo_OK(Integer id) {
        return "=========================PaymentFallbackService fall  back-paymentInfo_ok=======";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "=========================PaymentFallbackService fall  back-paymentInfo_TimeOut=======";
    }
}

4、测试

正常访问http://localhost/consumer/payment/hystrix/ok/32

故意关闭微服务8001

结果如图所示

在这里插入图片描述

5、服务熔断

论文地址

说明
  • 断路器

    一句话就是家里的保险丝

  • 熔断是什么

在这里插入图片描述

实操

1、 修改cloud-provider-hystrix-payment8001

2、PaymentService

@HystrixCommand(fallbackMethod="paymentCircuitBreakerFallback", commandProperties={
            @HystrixProperty(name = "circuitBreaker.enabled" ,value = "true"),//是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//时间窗口期 "
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")//失败率达到多少后跳闸
            })
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        if (id<0){
            throw new RuntimeException();
        }
        String serialNumber = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"\t "+"调用成功,流水号: "+serialNumber;
    }
    public String paymentCircuitBreakerFallback(@PathVariable("id") Integer id){
        return "id不能为负数,请稍后再试~ id: "+ id;
    }

3、PaymentController

    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String circuitBreaker = paymentService.paymentCircuitBreaker(id);
        log.info("******result: "+circuitBreaker);
        return circuitBreaker;
    }

结果

自测cloud-provider-hystrix-payment8001

http://localhost:8001/payment/circuit/31
在这里插入图片描述

http://localhost:8001/payment/circuit/-31
在这里插入图片描述

http://localhost:8001/payment/circuit/1
在这里插入图片描述

参数说明

源码:

public abstract class HystrixCommandProperties {
    private static final Logger logger = LoggerFactory.getLogger(HystrixCommandProperties.class);

    /* defaults */
    /* package */ static final Integer default_metricsRollingStatisticalWindow = 10000;// default => statisticalWindow: 10000 = 10 seconds (and default of 10 buckets so each bucket is 1 second)
    private static final Integer default_metricsRollingStatisticalWindowBuckets = 10;// default => statisticalWindowBuckets: 10 = 10 buckets in a 10 second window so each bucket is 1 second
    private static final Integer default_circuitBreakerRequestVolumeThreshold = 20;// default => statisticalWindowVolumeThreshold: 20 requests in 10 seconds must occur before statistics matter
    private static final Integer default_circuitBreakerSleepWindowInMilliseconds = 5000;// default => sleepWindow: 5000 = 5 seconds that we will sleep before trying again after tripping the circuit
    private static final Integer default_circuitBreakerErrorThresholdPercentage = 50;// default => errorThresholdPercentage = 50 = if 50%+ of requests in 10 seconds are failures or latent then we will trip the circuit
    private static final Boolean default_circuitBreakerForceOpen = false;// default => forceCircuitOpen = false (we want to allow traffic)
    /* package */ static final Boolean default_circuitBreakerForceClosed = false;// default => ignoreErrors = false 
    private static final Integer default_executionTimeoutInMilliseconds = 1000; // default => executionTimeoutInMilliseconds: 1000 = 1 second
    private static final Boolean default_executionTimeoutEnabled = true;
    private static final ExecutionIsolationStrategy default_executionIsolationStrategy = ExecutionIsolationStrategy.THREAD;
    private static final Boolean default_executionIsolationThreadInterruptOnTimeout = true;
    private static final Boolean default_executionIsolationThreadInterruptOnFutureCancel = false;
    private static final Boolean default_metricsRollingPercentileEnabled = true;
    private static final Boolean default_requestCacheEnabled = true;
    private static final Integer default_fallbackIsolationSemaphoreMaxConcurrentRequests = 10;
    private static final Boolean default_fallbackEnabled = true;
    private static final Integer default_executionIsolationSemaphoreMaxConcurrentRequests = 10;
    private static final Boolean default_requestLogEnabled = true;
    private static final Boolean default_circuitBreakerEnabled = true;
    private static final Integer default_metricsRollingPercentileWindow = 60000; // default to 1 minute for RollingPercentile 
    private static final Integer default_metricsRollingPercentileWindowBuckets = 6; // default to 6 buckets (10 seconds each in 60 second window)
    private static final Integer default_metricsRollingPercentileBucketSize = 100; // default to 100 values max per bucket
    private static final Integer default_metricsHealthSnapshotIntervalInMilliseconds = 500; // default to 500ms as max frequency between allowing snapshots of health (error percentage etc)
    @SuppressWarnings("unused") private final HystrixCommandKey key;
    private final HystrixProperty<Integer> circuitBreakerRequestVolumeThreshold; // number of requests that must be made within a statisticalWindow before open/close decisions are made using stats
    private final HystrixProperty<Integer> circuitBreakerSleepWindowInMilliseconds; // milliseconds after tripping circuit before allowing retry
    private final HystrixProperty<Boolean> circuitBreakerEnabled; // Whether circuit breaker should be enabled.
    private final HystrixProperty<Integer> circuitBreakerErrorThresholdPercentage; // % of 'marks' that must be failed to trip the circuit
    private final HystrixProperty<Boolean> circuitBreakerForceOpen; // a property to allow forcing the circuit open (stopping all requests)
    private final HystrixProperty<Boolean> circuitBreakerForceClosed; // a property to allow ignoring errors and therefore never trip 'open' (ie. allow all traffic through)
    private final HystrixProperty<ExecutionIsolationStrategy> executionIsolationStrategy; // Whether a command should be executed in a separate thread or not.
    private final HystrixProperty<Integer> executionTimeoutInMilliseconds; // Timeout value in milliseconds for a command
    private final HystrixProperty<Boolean> executionTimeoutEnabled; //Whether timeout should be triggered
    private final HystrixProperty<String> executionIsolationThreadPoolKeyOverride; // What thread-pool this command should run in (if running on a separate thread).
    private final HystrixProperty<Integer> executionIsolationSemaphoreMaxConcurrentRequests; // Number of permits for execution semaphore
    private final HystrixProperty<Integer> fallbackIsolationSemaphoreMaxConcurrentRequests; // Number of permits for fallback semaphore
    private final HystrixProperty<Boolean> fallbackEnabled; // Whether fallback should be attempted.
    private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnTimeout; // Whether an underlying Future/Thread (when runInSeparateThread == true) should be interrupted after a timeout
    private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnFutureCancel; // Whether canceling an underlying Future/Thread (when runInSeparateThread == true) should interrupt the execution thread
    private final HystrixProperty<Integer> metricsRollingStatisticalWindowInMilliseconds; // milliseconds back that will be tracked
    private final HystrixProperty<Integer> metricsRollingStatisticalWindowBuckets; // number of buckets in the statisticalWindow
    private final HystrixProperty<Boolean> metricsRollingPercentileEnabled; // Whether monitoring should be enabled (SLA and Tracers).
    private final HystrixProperty<Integer> metricsRollingPercentileWindowInMilliseconds; // number of milliseconds that will be tracked in RollingPercentile
    private final HystrixProperty<Integer> metricsRollingPercentileWindowBuckets; // number of buckets percentileWindow will be divided into
    private final HystrixProperty<Integer> metricsRollingPercentileBucketSize; // how many values will be stored in each percentileWindowBucket
    private final HystrixProperty<Integer> metricsHealthSnapshotIntervalInMilliseconds; // time between health snapshots
    private final HystrixProperty<Boolean> requestLogEnabled; // whether command request logging is enabled.
    private final HystrixProperty<Boolean> requestCacheEnabled; // Whether request caching is enabled.

在这里插入图片描述
在这里插入图片描述

涉汲到断路器的三个重要参数:快照时间窗、请求总数阀值、错误百分比阀值**。

  • 1、快照时间窗:断路器确定是否打开需要统计一些请求和错误数据,而统计的时间范围就是快照时间窗,默认为最近的10秒。
  • 2、请求总数阀值:在快照时间窗内,必须满足请求总数阀值才有资格熔断。默认为20, 意味着在10秒内,如果该hystrix命令的调用次数不足20次,即使所有的请求都超时或其他原因失败,断路器都不会打开。
  • 3、错误百分比阀值:当请求总、在快照时间窗内超过了阀值,比如发生了30次调用,如果在这30次调用中,有15次发生了超时异常,也就是超过50%的错误百分比,在默认设定50%阀值情况下,这时候就会将断路器打开。

原来的主逻辑要如何恢复呢?

对于这一-问题,hystrix也为我们实现了自动恢复功能。

  • 当断路器打开,对主逻辑进行熔断之后,hystrix会启动一个休眠时间窗在这个时间窗内,降级逻辑是临时的成为主逻辑,
  • 当休眠时间窗到期,断路器将进入半开状态,释放一次请求到原来的主逻辑上,如果此次请求正常返回,那么断路器将继续闭合,
  • 主逻辑恢复,如果这次请求依然有问题,断路器继续进入打开状态,休眠时间窗重新计时。
原理/小总结

在这里插入图片描述

官网原理图

在这里插入图片描述

1、官网步骤

在这里插入图片描述

2、断路器开启或者关闭的条件

  • 当满足一定的阈值的时候(默认10秒钟超过20个请求次数)
  • 当失败率达到一定的时候(默认10秒内超过50%的请求次数)
  • 到达以上阈值,断路器将会开启
  • 当开启的时候,所有请求都不会进行转发
  • 一段时间之后(默认5秒),这个时候断路器是半开状态,会让其他一个请求进行转发. 如果成功,断路器会关闭,若失败,继续开启.重复4和5

6、服务限流

后面高级篇讲解alibaba的Sentinel说明

7、hystrix工作流程

官网地址

图例

在这里插入图片描述

步骤说明
在这里插入图片描述

8、服务监控hystrixDashboard

在这里插入图片描述

  • 概述

在这里插入图片描述

  • 仪表盘9001搭建

    1、新建cloud-consumer-hystrix-dashboard9001

    2、POM

      <dependencies>
            <!--hystrix 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>
    

    3、YML

    server:
      port: 9001
    

    4、HystrixDashboardMain9001+新注解@EnableHystrixDashboard

    @SpringBootApplication
    @EnableHystrixDashboard
    public class HystrixDashboardMain9001 {
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashboardMain9001.class,args);
        }
    }
    

    5、测试

启动一个eureka或者3个eureka集群均可

  • 9001监控8001

    填写监控地址:http://localhost:8001/hystrix.stream

  • 测试地址

    http://localhost:8001/payment/circuit/31

    http://localhost:8001/payment/circuit/-31

    先访问正确地址,再访问错误地址,再正确地址,会发现图标断路器都是慢慢放开的.

  • 监控结果,成功

在这里插入图片描述

  • 测试结果,失败

在这里插入图片描述

6、如何看?

  • 七色
    在这里插入图片描述

  • 一圈

    实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减。

    该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。

  • 一线

    一条曲线可以观察到流量的上升与下降的趋势

图片参数说明

在这里插入图片描述

在这里插入图片描述

最终搞懂的集群图例

在这里插入图片描述

颠覆性创新的测量框架与跨领域实证分析 颠覆性创新作为科技发展的重要驱动力,其核心在于实现知识体系的范式转换。从技术原理看,这类创新通常具备知识替代、网络重构和跨领域渗透三大特征,通过引用网络分析和文本挖掘等方法可量化评估其影响力。在工程实践中,CD指数、DI指数等测量工具帮助研究者识别具有突破潜力的成果,而深度学习等新兴技术进一步提升了评估精度。当前AI、半导体等领域的热门技术发展表明,模块化架构和产业协同能有效促进颠覆性创新。面对全球范围内创新颠覆性下降的趋势,优化科研评价体系、构建跨学科协作网络成为关键应对策略。 阅读详情

相关推荐

80MHz~100MHz频谱分析仪设计

由理论分析可知,经过乘法器后输出的信号为频率为w1+w2的高频信号和w1-w2的低频信号,除了想要采样的直流信号外,最小的信号频率为100kHz。因此,经过一系列推导可以得到该滤波器的元件C1,C2,C3,R1,R2的近似值分别为:C1=2.36nF,C2=32.1nF,C3=1.07nF,R1=1.39k,R2=2.84k。AK,此时的频率w2即为被测信号的频率,待测信号源电压幅度为,因此为了能达到直流采样的设计,我们将本振源的频率输出范围做成80~110 MHz,提高了题目的频率要求。

qq_48771816的博客 5242

Hystrix系列-3-Hystrix的配置-通过代码构造配置

一、首先我们来实现一个HystrixCommand,示例代码如下: package com.example.demo.hystrix.command; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.cli

牛奋lch 2104

Orale EBS R12 AP应付模块 发票导入接口(Interface)

ap_interface_groups_s.nextval; ap_invoices_interface_s.nextval; ap_invoice_lines_interface_s.nextval; SELECT * FROM ap_invoices_interface ih; SELECT * FROM ap_invoice_lines_interface il; SELECT * FROM...

qq_41206741的博客 3316

SpringCloud微服务项目实战 - 限流、熔断、降级处理

我们知道,在分布式微服务项目体系中,一个系统是由若干个子服务模块组成,这若干个子服务相互调用协同工作,对外输出服务使得整个系统运作。由于服务之间的相互协作调用,所以要保证整个系统完整运行...

u011134780的博客 8214

JSP期末考试复习习题及答案

以下习题是对于平时课堂小测的整理,如有错误敬请指正! 欢迎一起交流~ 预祝各位小伙伴们考试顺利! Test1 一. 单选题 – 1.(单选题)当用户请求jsp页面时,JSP引擎就会执行该页面的字节码文件响应客户的请求,执行字节码文件的结果是() A. 发送一个JSP源文件到客户端 B. 发送一个Java文件到客户端 C. 发送一个HTML页面到客户端 D. 什么都不做 – 2.(单选题)下列动态网页和静态网页的根本区别描述错误的是( D) A. 静态网页服务器端返回的HTML文件是事先存储好的 B. 动.

技术小白 2万+

matlab 点云均值、中值、高斯滤波【2024最新版】

点云均值滤波、中值滤波、高斯滤波的matlab自带函数实现。博客长期更新,本文最近一次更新时间为:2024年11月9日。

点云侠的博客 5259

微服务SpringCloud开弓之SpringCloud Alibaba Sentinel实现熔断与限流「十四」

文章目录SpringCloud Alibaba Sentinel实现熔断与限流1、Sentinel2、安装Sentinel控制台3、初始化演示工程4、流控规则5、降级规则6、热点key限流1、基本介绍2、参数例外项7、系统规则8、@SentinelResource1、按资源名称限流+后续处理2、按照Url地址限流+后续处理3、客户自定义限流处理逻辑4、更多注解属性说明9、服务熔断功能Ribbon系列Feign系列熔断框架比较10、规则持久化 SpringCloud Alibaba Sentinel实现熔断与

python_start的博客 869

AI颠覆市场调研:生成式智能体如何重构商业决策?

AI正在将市场调研从"成本中心"转变为"战略武器"。当企业能够用生成式智能体实时模拟百万级虚拟用户时,传统调研的样本偏差、时间滞后、成本高昂等问题将不复存在。对于开发者而言,这不仅是技术的狂欢,更是参与商业规则重构的历史机遇。行动建议立即体验Simile/Aaru的智能体平台,验证虚拟样本效果参与LLM持续性记忆架构的开源项目(如Memory Llama)关注AI调研API的标准化进程(如Hugging Face的Market Research Toolkit)

这里汇聚了前沿的技术分享与实用的开发技巧,带你探索从创意到企业的技术创业之路。 614

微服务SpringCloud开弓之SpringCloud Alibaba Seata处理分布式事务「十五」

文章目录SpringCloud Alibaba Seata处理分布式事务1、分布式事务问题2、Seata-Server安装3、订单/库存/账户业务数据库准备4、订单/库存/账户业务微服务准备5、Seata之原理简介再次回顾 SpringCloud Alibaba Seata处理分布式事务 1、分布式事务问题 分布式前 单机单库没这个问题 从1:1 -> 1:N -> N: N 分布式之后 一句话 一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用,就会产生分布式事务问题

python_start的博客 498

YOLO11改进|注意力机制篇|引入ELA注意力机制

YOLO11添加ELA注意力机制

A1983Z的博客 3647

[27] cuda 应用之 核函数实现图像通道变换

[27] cuda 应用之 核函数实现图像通道变换

yohnyang的博客 201

微服务Springcloud开弓之框架搭建《一》

微服务体系 服务注册与发现 配置中心管理 服务调用 服务网关 服务熔断 服务监控 负载均衡 全链路跟踪 服务降级 自动化构建部署 服务消息队列 服务定时任务调度操作 对微服务的实现 Eureka : 服务注册与发现 Netelix Ribbon: 服务负载与调用 Netelix Feign: 服务负载与调用 Hystrix : 服务熔断与降级 Netelix Zuul : 服...

python_start的博客 481

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

微服务SpringCloud开弓之Consul服务注册与发现《五》

目录1、 Consul简介2、安装并运行Consul3、服务提供者4、服务消费者5、三个注册中心异同点 1、 Consul简介 是什么 https://www.consul.io/intro/index.html 能干嘛 服务发现 提供HTTP/DN 健康检测 支持多种方式,HTTP、TCP、Docker、shell脚本定制化 KV存储 Key、Value的存储方式 ...

python_start的博客 471

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

微服务SpringCloud开弓之Gateway新一代网关「九」

文章目录Gateway新一代网关1、概述简介2、三大核心概念3、Gateway工作流程4、入门配置5、通过服务名实现动态6、Predicate7、Filter的使用 Gateway新一代网关 1、概述简介 官网 上一代zuul 1.x 当前gateway 技术选型 1、Zuul1.x模型 2、我们为什么选择Gateway? 1.netflix不太靠谱,zuul2.0一直跳票,迟迟不发布 SpringCloud Gateway具有如下特性 基于Spring Framework 5,

python_start的博客 443

微服务SpringCloud开弓之Eureka服务注册与发现《三》

目录1、Eureka基础知识2、单机Eureka构建步骤3、集群Eureka构建步骤4、支付服务提供者8001集群环境搭建5、actuator微服务信息完善6、eureka自我保护 1、Eureka基础知识 什么是服务治理 什么是服务注册 Eureka两组件 2、单机Eureka构建步骤 IDEA生成EurekaServer端服务注册中心 建Module cloud-e...

python_start的博客 414

微服务SpringCloud开弓之 Rest微服务工程搭建《二》

Rest微服务工程搭建 这里写目录标题Rest微服务工程搭建构建步骤1. Cloud-provider-payment8001 微服务提供者Module模块2.热部署3.cloud-consumer-order80 微服务消费者订单Module模块4. 工程重构 构建步骤 1. Cloud-provider-payment8001 微服务提供者Module模块 建module 改POM ...

python_start的博客 403

微服务SpringCloud开弓之OpenFeign服务接口调用「七」

文章目录1、概述2、OpenFeign使用步骤3、OpenFeign超时控制 1、概述 OpenFeign是什么 官网地址 Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需 创建一个接口并在接口上添加注解即可 GitHub Feign和OpenFeign两者区别 2、OpenFeign使用步骤 接口+注解 微服务调用接口+@FeignClient 新建cloud-consumer-feign-order80 Feign在消费端使用 POM

python_start的博客 369

微服务SpringCloud开弓之SpringCloud Alibaba Nacos服务注册和配置中心「十三」

文章目录SpringCloud Alibaba Nacos服务注册和配置中心1、Nacos简介2、Nacos作为服务注册中心演示3、服务注册中心对比4、Nacos作为配置中心--基础配置5、Nacos作为配置中心--分类配置6、Nacos集群和持久化配置(重要)7、Linux版nacos+mysql生产环境配置 SpringCloud Alibaba Nacos服务注册和配置中心 1、Nacos简介 地址 官方文档 为什么叫Nacos 前四个字母分别为naming和Configuration的前两个字母

python_start的博客 366

微服务SpringCloud开弓之Ribbon负载均衡调用《六》

目录1、概述2、Ribbon负载均衡演示3、Ribbon核心组件IRule4、Ribbon负载均衡算法 1、概述 是什么 官网资料 ​ 地址 Ribbon目前也进入维护模式 用途 LB(负载均衡) - 集中式LB - 进程内LB 2、Ribbon负载均衡演示 架构说明 总结: Ribbon其实就是一个软负载均衡的客户端组件, 他可以和其他所需请...

python_start的博客 304

微服务SpringCloud开弓之Zookeeper服务注册与发现《四》

目录SpringCloud整合Zookeeper替代Eureka1、注册中心Zookeeper2、服务提供者3、服务消费者 Eureka停止更新了 地址 SpringCloud整合Zookeeper替代Eureka 1、注册中心Zookeeper 关闭Linux服务器防火墙后启动Zookeeper服务器 //临时关闭 service iptables stop //禁止开机启动 chkconf...

python_start的博客 269

微服务SpringCloud开弓之SpringCloud config分布式配置中心「十」

文章目录1、概述2、Config服务端配置与测试3、Config客户端配置与测试4、Config客户端手动动态刷新 1、概述 地址 分布式系统面临的问题–配置问题 是什么 ​ Springcloud config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供一个中心化的外部配置。 能干嘛 ​ 1、集中管理配置文件 ​ 2、不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release

python_start的博客 263

微服务SpringCloud开弓之SpringCloud Bus消息总线「十一」

文章目录SpringCloud Bus消息总线1、针对问题:2、概述3、RabbitMQ环境配置4、SpringCloud Bus动态刷新全局广播5、Springcloud Bus动态刷新定点通知 SpringCloud Bus消息总线 1、针对问题: 每次微服务都要执行一次post请求,手动刷新 可否广播?一次通知,处处生效 我们想大范围的自动刷新 2、概述 1、分布式自动刷新配置功能 2、SpringCloud Bus配合Springcloud Config使用可以实现配置的动态刷新 是什么 s

python_start的博客 259

微服务SpringCloud开弓之SpringCloud Stream消息驱动「十二」

文章目录SpringCloud Stream消息驱动1、消息驱动概述2、案例说明3、消息驱动之生产者4、消息驱动之消费者5、分组消费与持久化 SpringCloud Stream消息驱动 1、消息驱动概述 是什么 官网地址 文档 一句话:屏蔽底层消息中间件的差异,降低切换成本,统一消息的编程模型 设计思想 1、标准MQ 生产者/消费者之间靠消息媒介传递信息内容:Message 消息必须走特定的通道:MessageChannel 消息通道里的消息如何被消费呢?谁负责收发。处理消息通道Message

python_start的博客 223

Spring AI + GPT-5.5 + Ollama 配置教程:Base URL、超时与流式调用

由于不同 Spring AI、Spring Boot 小版本之间仍可能存在差异,创建项目时应检查 Spring Initializr、Spring AI BOM 和官方兼容矩阵,不要直接复制旧教程中的 Starter 名称和版本号。多人项目最好隔离不同客户端,并在脱敏前提下记录最终 URL、状态码、请求耗时、请求 ID 和模型 ID。Spring AI 接入模型 API,至少要同时确认 Starter、Base URL、请求路径、API Key、模型 ID、连接超时、读取超时和流式客户端这 8 项。

2601_95893075的博客 215

V8 机制、手写核心函数与 TypeScript 类型体操

或固定初始化(如数组先定长、对象先给全字段)保持类型与形状稳定;这些都比"换框架"更基础。只能处理纯数据(丢函数、Date、RegExp、循环引用报错)。生产级深拷贝要处理:原始值短路、循环引用、V8(Chrome / Node 的 JS 引擎)不是"解释一行跑一行"那么简单,它用。图:V8 执行流程——解释执行 + 热点优化,优化失败会 Deopt 回退。JS 内存自动管理,但"自动"不等于"不会漏"。是"固定间隔最多执行一次"。手写 Promise 是"懂异步"的试金石。是"停下后才执行最后一次",

qq_38468358的博客 325

【JVM】对象内存布局

HotSpot JVM 中,Java 对象内存布局由对象头(12字节,含Mark Word与压缩Klass Pointer)、实例数据及对齐填充构成,总大小为8字节倍数。对象头支持锁状态动态复用,指针压缩使引用仅4字节,但堆超32GB时失效。实例字段按类型优先级重排序以减少间隙,最小对象大小为16字节。通过JOL工具可精准验证布局,理解锁机制、GC、伪共享等底层行为。

2301_79732484的博客 140
上一篇: 微服务SpringCloud开弓之OpenFeign服务接口调用「七」
下一篇: 微服务SpringCloud开弓之Gateway新一代网关「九」
漂吧
博客等级 码龄8年 15粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值