golang的熔断包hystrix的用例

栏目: Go · 发布时间: 5年前

内容简介:服务器的熔断机制是个大话题,可以参考这篇文章一窥究竟:Netflix的熔断系统Hystrix很有效,并提供了golang的包:如上,RequestVolumeThreshold 为1,则第二次调用开始探测,由于所有调用全部失败,则熔断立马被触发,后续调用全部不能触达

服务器的熔断机制是个大话题,可以参考这篇文章一窥究竟: 熔断,限流,降级

Netflix的熔断系统Hystrix很有效,并提供了golang的包: https://github.com/afex/hystrix-go 。来看看参数如何使用,代码中的默认配置定义为:

var (
	// DefaultTimeout is how long to wait for command to complete, in milliseconds
	DefaultTimeout = 1000
	// DefaultMaxConcurrent is how many commands of the same type can run at the same time
	DefaultMaxConcurrent = 10
	// DefaultVolumeThreshold is the minimum number of requests needed before a circuit can be tripped due to health
	DefaultVolumeThreshold = 20
	// DefaultSleepWindow is how long, in milliseconds, to wait after a circuit opens before testing for recovery
	DefaultSleepWindow = 5000
	// DefaultErrorPercentThreshold causes circuits to open once the rolling measure of errors exceeds this percent of requests
	DefaultErrorPercentThreshold = 50
)

RequestVolumeThreshold 指明某个command在多少次调用之后开始探测熔断

package main

import (
	"errors"
	"log"
	"math/rand"
	"third/hystrix"
	"time"
)

func init() {
	hystrix.ConfigureCommand("seckill", hystrix.CommandConfig{
		Timeout:                1, //cmd的超时时间,一旦超时则返回失败
		MaxConcurrentRequests:  1, //最大并发请求数
		RequestVolumeThreshold: 1, //熔断探测前的调用次数
		SleepWindow:            1000, //熔断发生后的等待恢复时间
		ErrorPercentThreshold:  10,//失败占比
	})
}

var Gcount, Gerror int

func testHystrix() error {

	query := func() error {
		var err error
		r := rand.Float64()
		Gcount++
		if r < 1 {
			err = errors.New("bad luck")
			Gerror++
			return err
		} else {
			time.Sleep(20 * time.Millisecond)
		}

		return nil
	}

	service := "seckill"
	var err error
	err = hystrix.Do(service, func() error {
		err = query()
		return err
	}, nil)

	return err
}

func main() {
	for i := 0; i < 10; i++ {
		err := testHystrix()
		if err != nil {
			log.Printf("testHystrix error:%v", err)
		}
	}

	log.Printf("Gcount:%d Gerror:%d", Gcount, Gerror)
}

如上,RequestVolumeThreshold 为1,则第二次调用开始探测,由于所有调用全部失败,则熔断立马被触发,后续调用全部不能触达 query 函数。

2019/05/09 15:15:15 testHystrix error:bad luck
2019/05/09 15:15:15 hystrix-go: opening circuit seckill
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 15:15:15 testHystrix error:hystrix: circuit open
2019/05/09 16:02:56 Gcount:1 Gerror:1

Process finished with exit code 0

最后的结果为调用了一次,失败一次。

SleepWindow 指明在熔断发生后何时开始探测其是否恢复,单位毫秒

修改参数为:

	hystrix.ConfigureCommand("seckill", hystrix.CommandConfig{
		Timeout:                1, //cmd的超时时间,一旦超时则返回失败
		MaxConcurrentRequests:  1, //最大并发请求数
		RequestVolumeThreshold: 1, //熔断探测前的调用次数
		SleepWindow:            1, //熔断发生后的等待恢复时间
		ErrorPercentThreshold:  10,//失败占比
	})

熔断发生后1毫秒后就开始探测是否恢复,当然结果还是继续失败:

2019/05/09 16:36:21 testHystrix error:bad luck
2019/05/09 16:36:21 hystrix-go: opening circuit seckill
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 testHystrix error:hystrix: circuit open
2019/05/09 16:36:21 hystrix-go: allowing single test to possibly close circuit seckill
2019/05/09 16:36:21 testHystrix error:hystrix: timeout
2019/05/09 16:36:21 Gcount:2 Gerror:2

Process finished with exit code 0

最后的结果为调用了两次,失败两次。

Timeout 指明command的执行超时时间,单位毫秒

参数为原始参数。修改 query 函数:

	query := func() error {
		var err error
		r := rand.Float64()
		Gcount++
		if r < 0 {
			err = errors.New("bad luck")
			Gerror++
			return err
		} else {
			time.Sleep(20 * time.Millisecond)
		}

		return nil
	}

所有调用都超时,结果为调用了一次,失败0次。

2019/05/09 16:49:43 testHystrix error:hystrix: timeout
2019/05/09 16:49:43 hystrix-go: opening circuit seckill
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 testHystrix error:hystrix: circuit open
2019/05/09 16:49:43 Gcount:1 Gerror:0

Process finished with exit code 0

ErrorPercentThreshold 指明熔断的触发条件:失败调用占总调用次数的比例

参数为原始参数。修改 query 函数:

	query := func() error {
		var err error
		r := rand.Float64()
		Gcount++
		if r < 0.1 {
			err = errors.New("bad luck")
			Gerror++
			return err
		} else {
			time.Sleep(20 * time.Millisecond)
		}

		return nil
	}

测试100次,结果太长不贴了,核心就是失败次数占总次数的比例,触发熔断之后清空,待恢复之后重新计算。

MaxConcurrentRequests 指明最大并发数,很好理解了


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

从问题到程序

从问题到程序

裘宗燕 / 机械工业出版社 / 2011-5 / 39.00元

《从问题到程序:程序设计与C语言引论(第2版)》以C作为工具语言,讨论了基本程序设计的各方面内容,详细解释了与c语言和程序设计有关的问题。在新版中,特别加强了针对近年日益受到业界和学术界广泛重视的问题的讨论,并通过详细地分析和讨论大量符合C99标准的实例,给出了分析和分解问题、找出解决问题的主要步骤、确定函数抽象、找出循环、选择语言结构直至最后做出所需程序的完整过程。 《从问题到程序:程序设......一起来看看 《从问题到程序》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具