-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
83 lines (72 loc) · 2.33 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"github.com/afex/hystrix-go/hystrix"
"net"
"net/http"
"time"
)
type Handle struct{}
func (h *Handle) ServeHTTP(r http.ResponseWriter, request *http.Request) {
path := request.URL.Path
fmt.Println("请求的路径是", path)
switch path {
case "/foo":
h.async(r, request)
default:
h.synchronize(r, request)
}
}
func (h *Handle) async(r http.ResponseWriter, request *http.Request) {
output := make(chan bool, 1)
errors := hystrix.Go("myCommand", func() error {
// talk to other services
output <- true
return nil
}, nil)
select {
case out := <-output:
_ = out
msg := "异步success"
r.Write([]byte(msg))
// success
case err := <-errors:
msg := "同步failure" + err.Error()
// failure
r.Write([]byte(msg))
}
}
func (h *Handle) synchronize(r http.ResponseWriter, request *http.Request) {
msg := "同步success"
// 同步的方法
_ = hystrix.Do("myCommand", func() error {
_, err := http.Get("https://www.baidu.com")
if err != nil {
fmt.Printf("请求失败:%v", err)
return err
}
return nil
}, func(err error) error {
fmt.Printf("handle error:%v\n", err)
msg = "异步error"
return nil
})
r.Write([]byte(msg))
}
func main() {
// 配置策略-mycommand为全局
hystrix.ConfigureCommand("myCommand", hystrix.CommandConfig{
Timeout: int(3 * time.Second), // 执行 command 的超时时间。
MaxConcurrentRequests: 10, // MaxConcurrentRequests:command 的最大并发量
SleepWindow: 5000, // 当熔断器被打开后,SleepWindow 的时间就是控制过多久后去尝试服务是否可用了。
RequestVolumeThreshold: 20, // 一个统计窗口 10 秒内请求数量。达到这个请求数量后才去判断是否要开启熔断
ErrorPercentThreshold: 30, // 错误百分比,请求数量大于等于 RequestVolumeThreshold 并且错误率到达这个百分比后就会启动熔断
})
// dashboard 可视化 hystrix 的上报信息
hystrixStreamHandler := hystrix.NewStreamHandler()
hystrixStreamHandler.Start()
go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)
http.ListenAndServe(":8090", &Handle{})
}
// dashboard我使用的是`docker`版
// docker run -d -p 8888:9002 --name hystrix-dashboard mlabouardy/hystrix-dashboard:latest