zoukankan      html  css  js  c++  java
  • 20.熔断器学习,超时情况下的服务降级写法

    在hystrix的超时回调函数中处理超时推荐其他商品

    package main
    
    import (
        "errors"
        "fmt"
        "github.com/afex/hystrix-go/hystrix"
        "math/rand"
        "time"
    )
    
    type Product struct {
        ID    int
        Title string
        Price int
    }
    
    func getProduct() (Product, error) {
        r := rand.Intn(10)
        if r < 6 { //模拟api卡顿和超时效果
            time.Sleep(time.Second * 4)
        }
        return Product{
            ID:    101,
            Title: "Golang从入门到精通",
            Price: 12,
        }, nil
    }
    
    func RecProduct() (Product, error) {
        return Product{
            ID:    999,
            Title: "推荐商品",
            Price: 120,
        }, nil
    
    }
    
    func main() {
        rand.Seed(time.Now().UnixNano())
        configA := hystrix.CommandConfig{ //创建一个hystrix的config
            Timeout: 3000, //command运行超过3秒就会报超时错误
        }
        hystrix.ConfigureCommand("get_prod", configA) //hystrix绑定command
        for {
            err := hystrix.Do("get_prod", func() error { //使用hystrix来讲我们的操作封装成command
                p, _ := getProduct() //这里会随机延迟0-4秒
                fmt.Println(p)
                return nil
            }, func(e error) error {
                fmt.Println(RecProduct()) //超时后调用回调函数返回推荐商品
                return errors.New("my timeout")
            })
            if err != nil {
                //如果降级也失败了,在这里定义业务逻辑
            }
        }
    }
    




  • 相关阅读:
    Django踩坑记录2
    Django踩坑记录1
    pycharm2020激活破解和汉化
    python 读写文件
    python截取视频制作动态表情包+文字
    jenkins运行错误解决办法
    python读取xlsx文件
    python读取csv文件
    python+requests之接口测试
    win10安装jenkins忘记密码的解决方法
  • 原文地址:https://www.cnblogs.com/hualou/p/12084189.html
Copyright © 2011-2022 走看看