Skip to content

Commit

Permalink
add hystrix middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pingguoxueyuan committed Nov 10, 2019
1 parent 9fa13c5 commit 162068f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 11 deletions.
38 changes: 38 additions & 0 deletions example/hystrix_example/main.go
Original file line number Diff line number Diff line change
@@ -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一下,不然看不到效果
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
27 changes: 27 additions & 0 deletions middleware/hystrix.go
Original file line number Diff line number Diff line change
@@ -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
}
}
Binary file not shown.
5 changes: 4 additions & 1 deletion rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
Expand Down
27 changes: 19 additions & 8 deletions tools/koala/client_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}
4 changes: 2 additions & 2 deletions tools/koala/output/conf/test/hello.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
port: 8080
port: 8090
prometheus:
switch_on: true
port: 8081
port: 8091
service_name: hello
register:
switch_on: true
Expand Down

0 comments on commit 162068f

Please sign in to comment.