zoukankan      html  css  js  c++  java
  • 19.熔断器学习,配置command,超时报错,设置最大超时时间

    ## 使用hystrix来实现监控超时
     
    ```go
    package main
     
    import (
        "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 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
            }, nil)
            if err != nil {
                fmt.Println(err)
            }
        }
    }
     
    ```
     
    ![](https://xiahualou.oss-cn-shanghai.aliyuncs.com/img/20191223153242.png)
     




  • 相关阅读:
    06springmvc文件上传
    07springmvc文件下载
    05springmvc-json-ajax使用
    04springmvc请路径参数
    03springmvc项目使用静态资源
    02springmvc注解入门
    springmvc入门
    20mybatis集成jndi
    19mybatis集成dbcp
    怎样用hibernate验证登陆界面的用户名和密码
  • 原文地址:https://www.cnblogs.com/hualou/p/12084048.html
Copyright © 2011-2022 走看看