-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fa13c5
commit 162068f
Showing
8 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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一下,不然看不到效果 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters