diff --git a/example/hystrix_example/main.go b/example/hystrix_example/main.go new file mode 100644 index 0000000..e2d01f5 --- /dev/null +++ b/example/hystrix_example/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "net/http" + "time" + + "github.com/afex/hystrix-go/hystrix" +) + +func main() { + hystrix.ConfigureCommand("koala_rpc", hystrix.CommandConfig{ + Timeout: 1000, + MaxConcurrentRequests: 100, + ErrorPercentThreshold: 25, + }) + + for { + err := hystrix.Do("get_baidu", func() error { + // talk to other services + _, err := http.Get("https://www.baidu.com/") + if err != nil { + fmt.Println("get error") + return err + } + return nil + }, func(err error) error { + fmt.Println("get an error, handle it, err:", err) + return err + }) + if err == nil { + fmt.Printf("request success\n") + } + time.Sleep(time.Millisecond * 10) + } + + time.Sleep(2 * time.Second) // 调用Go方法就是起了一个goroutine,这里要sleep一下,不然看不到效果 +} diff --git a/go.mod b/go.mod index e3caf5f..60bdf12 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/ibinarytree/koala go 1.13 require ( + github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect github.com/coreos/bbolt v1.3.3 // indirect github.com/coreos/etcd v3.3.17+incompatible diff --git a/go.sum b/go.sum index f5d193b..b1517e2 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 h1:rFw4nCn9iMW+Vajsk51NtYIcwSTkXr+JGrMd36kTDJw= +github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= diff --git a/middleware/hystrix.go b/middleware/hystrix.go new file mode 100644 index 0000000..c3f90a4 --- /dev/null +++ b/middleware/hystrix.go @@ -0,0 +1,27 @@ +package middleware + +import ( + "context" + + "github.com/afex/hystrix-go/hystrix" + "github.com/ibinarytree/koala/meta" +) + +func HystrixMiddleware(next MiddlewareFunc) MiddlewareFunc { + return func(ctx context.Context, req interface{}) (interface{}, error) { + + rpcMeta := meta.GetRpcMeta(ctx) + var resp interface{} + + hystrixErr := hystrix.Do(rpcMeta.ServiceName, func() (err error) { + resp, err = next(ctx, req) + return err + }, nil) + + if hystrixErr != nil { + return nil, hystrixErr + } + + return resp, hystrixErr + } +} diff --git "a/ppt/21. rpc \347\206\224\346\226\255\344\270\255\351\227\264\344\273\266\345\274\200\345\217\221/rpc\347\206\224\346\226\255\344\270\255\351\227\264\344\273\266\345\274\200\345\217\221.pdf" "b/ppt/21. rpc \347\206\224\346\226\255\344\270\255\351\227\264\344\273\266\345\274\200\345\217\221/rpc\347\206\224\346\226\255\344\270\255\351\227\264\344\273\266\345\274\200\345\217\221.pdf" new file mode 100644 index 0000000..65eee09 Binary files /dev/null and "b/ppt/21. rpc \347\206\224\346\226\255\344\270\255\351\227\264\344\273\266\345\274\200\345\217\221/rpc\347\206\224\346\226\255\344\270\255\351\227\264\344\273\266\345\274\200\345\217\221.pdf" differ diff --git a/rpc/client.go b/rpc/client.go index fcb24a3..e517bb1 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -71,10 +71,13 @@ func (k *KoalaClient) getCaller(ctx context.Context) string { func (k *KoalaClient) buildMiddleware(handle middleware.MiddlewareFunc) middleware.MiddlewareFunc { var mids []middleware.Middleware + mids = append(mids, middleware.HystrixMiddleware) mids = append(mids, middleware.NewDiscoveryMiddleware(k.register)) + mids = append(mids, middleware.NewLoadBalanceMiddleware(k.balance)) mids = append(mids, middleware.ShortConnectMiddleware) - m := middleware.Chain(mids[0], mids...) + + m := middleware.Chain(mids[0], mids[1:]...) return m(handle) } diff --git a/tools/koala/client_example/main.go b/tools/koala/client_example/main.go index ffe971a..94486d8 100644 --- a/tools/koala/client_example/main.go +++ b/tools/koala/client_example/main.go @@ -4,6 +4,7 @@ package main import ( "context" + "time" "github.com/ibinarytree/koala/logs" "github.com/ibinarytree/koala/tools/koala/client_example/generate/client/helloc" @@ -17,14 +18,24 @@ const ( func main() { client := helloc.NewHelloClient("hello") - ctx := context.Background() - resp, err := client.SayHello(ctx, &hello.HelloRequest{Name: "test my client"}) - if err != nil { - logs.Error(ctx, "could not greet: %v", err) - logs.Stop() - return - } + var count int + for { + count++ + ctx := context.Background() + resp, err := client.SayHello(ctx, &hello.HelloRequest{Name: "test my client"}) + if err != nil { + if count%100 == 0 { + logs.Error(ctx, "could not greet: %v", err) + } + time.Sleep(10 * time.Millisecond) + continue + } + + if count%100 == 0 { + logs.Info(ctx, "Greeting: %s", resp.Reply) + } - logs.Info(ctx, "Greeting: %s", resp.Reply) + time.Sleep(100 * time.Millisecond) + } logs.Stop() } diff --git a/tools/koala/output/conf/test/hello.yaml b/tools/koala/output/conf/test/hello.yaml index 6be9587..4232ac1 100644 --- a/tools/koala/output/conf/test/hello.yaml +++ b/tools/koala/output/conf/test/hello.yaml @@ -1,7 +1,7 @@ -port: 8080 +port: 8090 prometheus: switch_on: true - port: 8081 + port: 8091 service_name: hello register: switch_on: true