18.服务容错,熔断器学习(1)入门、基本运行

简单来说就是浏览器想要拿到最终的结果需要需要经过A,B,C三个节点,A调用B,B调用C最终返回结果,但是如果C崩溃了,我们应该怎么做呢

先模拟一个延迟调用的例子

package main

import (
    "fmt"
    "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 * 3)
    }
    return Product{
        ID:    101,
        Title: "Golang从入门到精通",
        Price: 12,
    }, nil
}

func main() {
    rand.Seed(time.Now().UnixNano())
    for {
        p, _ := getProduct()
        fmt.Println(p)
        time.Sleep(time.Second)
    }
}




猜你喜欢

转载自www.cnblogs.com/hualou/p/12083940.html
今日推荐