熔断限流负载均衡

master
shenzhuan 2 years ago
parent 6a393a3404
commit ad82c2e061

@ -2,13 +2,19 @@ package main
import ( import (
"context" "context"
"fmt"
"github.com/afex/hystrix-go/hystrix"
consul "github.com/asim/go-micro/plugins/registry/consul/v4" consul "github.com/asim/go-micro/plugins/registry/consul/v4"
"github.com/go-micro/plugins/v4/wrapper/select/roundrobin"
opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing" opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"go-micro.dev/v4/client"
"go-micro.dev/v4/web" "go-micro.dev/v4/web"
"goproduct/common" "goproduct/common"
"goproduct/proto" "goproduct/proto"
"log" "log"
"net"
"net/http"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -32,10 +38,24 @@ func main() {
defer io.Close() defer io.Close()
opentracing.SetGlobalTracer(t) opentracing.SetGlobalTracer(t)
//熔断器
hystrixStreamHandler := hystrix.NewStreamHandler()
hystrixStreamHandler.Start()
go func() {
err := http.ListenAndServe(net.JoinHostPort("192.168.100.1", "9096"), hystrixStreamHandler)
if err != nil {
log.Panic(err)
}
}()
rpcServer := micro.NewService( rpcServer := micro.NewService(
//micro.Name("shop-product-client"), //micro.Name("shop-product-client"),
micro.Registry(consulReg), //服务发现 micro.Registry(consulReg), //服务发现
micro.WrapClient(opentracing2.NewClientWrapper(opentracing.GlobalTracer())), micro.WrapClient(opentracing2.NewClientWrapper(opentracing.GlobalTracer())),
//加入熔断器
micro.WrapClient(NewClientHystrixWrapper()),
//负载均衡
micro.WrapClient(roundrobin.NewClientWrapper()),
) )
client := proto.NewAddCartService("shop-cart", rpcServer.Client()) client := proto.NewAddCartService("shop-cart", rpcServer.Client())
clientA := proto.NewShowProductDetailService("shop-product", rpcServer.Client()) clientA := proto.NewShowProductDetailService("shop-product", rpcServer.Client())
@ -86,3 +106,26 @@ func main() {
//启动服务 //启动服务
service.Run() service.Run()
} }
type clientWrapper struct {
client.Client
}
func (c clientWrapper) Call(ctx context.Context, req client.Request, resp interface{}, opts ...client.CallOption) error {
return hystrix.Do(req.Service()+"."+req.Endpoint(), func() error {
//正常执行
fmt.Println("call success ", req.Service()+"."+req.Endpoint())
return c.Client.Call(ctx, req, resp, opts...)
}, func(err error) error {
fmt.Println("call err :", err)
return err
})
}
func NewClientHystrixWrapper() client.Wrapper {
return func(i client.Client) client.Client {
return &clientWrapper{i}
}
}

@ -21,8 +21,10 @@ require (
) )
require ( require (
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/asim/go-micro/plugins/registry/consul/v4 v4.7.0 github.com/asim/go-micro/plugins/registry/consul/v4 v4.7.0
github.com/gin-gonic/gin v1.8.1 github.com/gin-gonic/gin v1.8.1
github.com/go-micro/plugins/v4/wrapper/ratelimiter/uber v1.1.0
github.com/go-micro/plugins/v4/wrapper/trace/opentracing v1.1.0 github.com/go-micro/plugins/v4/wrapper/trace/opentracing v1.1.0
github.com/opentracing/opentracing-go v1.2.0 github.com/opentracing/opentracing-go v1.2.0
github.com/spf13/viper v1.12.0 github.com/spf13/viper v1.12.0
@ -39,6 +41,7 @@ require (
github.com/Microsoft/go-winio v0.5.0 // indirect github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/armon/go-metrics v0.3.10 // indirect github.com/armon/go-metrics v0.3.10 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-semver v0.3.0 // indirect
@ -54,6 +57,7 @@ require (
github.com/go-git/gcfg v1.5.0 // indirect github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/go-micro/plugins/v4/wrapper/select/roundrobin v1.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect github.com/go-playground/validator/v10 v10.10.0 // indirect
@ -90,9 +94,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect github.com/nxadm/tail v1.4.8 // indirect
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492 // indirect
github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5 // indirect
github.com/openzipkin/zipkin-go v0.4.0 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml v1.9.5 // indirect
@ -118,6 +119,7 @@ require (
go.opencensus.io v0.23.0 // indirect go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.7.0 // indirect go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect go.uber.org/multierr v1.6.0 // indirect
go.uber.org/ratelimit v0.2.0 // indirect
go.uber.org/zap v1.17.0 // indirect go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect

File diff suppressed because it is too large Load Diff

@ -2,6 +2,7 @@ package main
import ( import (
consul "github.com/asim/go-micro/plugins/registry/consul/v4" consul "github.com/asim/go-micro/plugins/registry/consul/v4"
ratelimiter "github.com/go-micro/plugins/v4/wrapper/ratelimiter/uber"
opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing" opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
"go-micro.dev/v4" "go-micro.dev/v4"
@ -19,6 +20,7 @@ const (
consulStr = "http://192.168.100.131:8500" consulStr = "http://192.168.100.131:8500"
consulReistStr = "192.168.100.131:8500" consulReistStr = "192.168.100.131:8500"
fileKey = "mysql-product" fileKey = "mysql-product"
QPS = 100
) )
func main() { func main() {
@ -47,8 +49,10 @@ func main() {
micro.Address(":8084"), micro.Address(":8084"),
micro.Version("v1"), micro.Version("v1"),
micro.Registry(consulReist), micro.Registry(consulReist),
//引包 //链路追踪
micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())), micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
//server 限流
micro.WrapHandler(ratelimiter.NewHandlerWrapper(QPS)),
) )
//2.初始化db //2.初始化db
db, _ := common.GetMysqlFromConsul(consulConfig) db, _ := common.GetMysqlFromConsul(consulConfig)

Loading…
Cancel
Save