环境准备
- github.com/afex/hystrix-go/hystrix
- 本工程源码地址
在装饰器出构建熔断降级逻辑
package wapper
import (
"context"
"fmt"
"github.com/afex/hystrix-go/hystrix"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/metadata"
"go-micro-service/service/client"
)
func NewLogWrapper(c client.Client) client.Client {
return &LogWrapper{
c}
}
func NewTimeoutCatchWrapper(c client.Client) client.Client {
return &TimeoutCatchWrapper{
c}
}
type LogWrapper struct {
client.Client
}
type TimeoutCatchWrapper struct {
client.Client
}
func (logWrapper *LogWrapper) Call(ctx context.Context, req client.Request, rsp interface{
}, opts ...client.CallOption) error {
md, _ := metadata.FromContext(ctx)
fmt.Printf("[Info] ctx: %v service: %s method: %s\n", md, req.Service(), req.Endpoint(