24.把熔断器整合到我们的客户端代码中(初步)
Posted hualou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了24.把熔断器整合到我们的客户端代码中(初步)相关的知识,希望对你有一定的参考价值。
熔断器一般部署在客户端或者网关里面
封装到服务端
package main
import (
"fmt"
"github.com/afex/hystrix-go/hystrix"
"gomicro2/util"
"log"
"time"
)
func main() {
configA := hystrix.CommandConfig{
Timeout: 2000,
MaxConcurrentRequests: 5,
RequestVolumeThreshold: 3,
SleepWindow: int(time.Second * 10),
ErrorPercentThreshold: 20,
}
hystrix.ConfigureCommand("getuser", configA)
err := hystrix.Do("getuser", func() error {
res, err := util.GetUser() //调用方法
fmt.Println(res)
return err
}, func(e error) error {
fmt.Println("降级用户")
return e
})
if err != nil {
log.Fatal(err)
}
}
Util.GetUser
package util
import (
"context"
"fmt"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/sd"
"github.com/go-kit/kit/sd/consul"
"github.com/go-kit/kit/sd/lb"
httptransport "github.com/go-kit/kit/transport/http"
consulapi "github.com/hashicorp/consul/api"
"gomicro2/Services"
"io"
"net/url"
"os"
"time"
)
func GetUser() (string, error) {
//第一步创建client
{
config := consulapi.DefaultConfig() //初始化consul的配置
config.Address = "localhost:8500" //consul的地址
api_client, err := consulapi.NewClient(config) //根据consul的配置初始化client
if err != nil {
return "", err
}
client := consul.NewClient(api_client) //根据client创建实例
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stdout)
var Tag = []string{"primary"}
instancer := consul.NewInstancer(client, logger, "userservice", Tag, true) //最后的true表示只有通过健康检查的服务才能被得到
{
factory := func(service_url string) (endpoint.Endpoint, io.Closer, error) { //factory定义了如何获得服务端的endpoint,这里的service_url是从consul中读取到的service的address我这里是192.168.3.14:8000
tart, _ := url.Parse("http://" + service_url) //server ip +8080真实服务的地址
return httptransport.NewClient("GET", tart, Services.GetUserInfo_Request, Services.GetUserInfo_Response).Endpoint(), nil, nil //我再GetUserInfo_Request里面定义了访问哪一个api把url拼接成了http://192.168.3.14:8000/v1/user/{uid}的形式
}
endpointer := sd.NewEndpointer(instancer, factory, logger)
endpoints, err := endpointer.Endpoints() //获取所有的服务端当前server的所有endpoint函数
if err != nil {
return "", err
}
fmt.Println("服务有", len(endpoints), "条")
mylb := lb.NewRandom(endpointer, time.Now().UnixNano()) //使用go-kit自带的轮询
for {
getUserInfo, err := mylb.Endpoint() //写死获取第一个
ctx := context.Background() //第三步:创建一个context上下文对象
//第四步:执行
res, err := getUserInfo(ctx, Services.UserRequest{Uid: 101})
if err != nil {
return "", err
}
//第五步:断言,得到响应值
userinfo := res.(Services.UserResponse)
return userinfo.Result, nil
}
}
}
}
}
以上是关于24.把熔断器整合到我们的客户端代码中(初步)的主要内容,如果未能解决你的问题,请参考以下文章