-
Notifications
You must be signed in to change notification settings - Fork 448
新手指南
Eric Zhao edited this page Feb 12, 2020
·
9 revisions
欢迎来到 Sentinel 的世界!这篇新手指南将指引您快速入门 Sentinel。
使用 Sentinel 的 Entry API 将业务逻辑封装起来,这一步称为“埋点”。每个埋点都有一个资源名称(resource),代表触发了这个资源的调用或访问。
import (
sentinel "github.com/sentinel-group/sentinel-golang/api"
)
func foo() {
err := sentinel.InitDefault()
if err != nil {
log.Fatal(err)
}
_, err = flow.LoadRules([]*flow.FlowRule{
{
ID: 1,
Resource: "some-test",
LimitOrigin: "default",
MetricType: flow.QPS,
Count: 10,
RelationStrategy: flow.Direct,
ControlBehavior: flow.Reject,
RefResource: "",
WarmUpPeriodSec: 0,
MaxQueueingTimeMs: 0,
ClusterMode: false,
ClusterConfig: flow.ClusterRuleConfig{},
},
})
if err != nil {
fmt.Println(err)
return
}
ch := make(chan struct{})
for i := 0; i < 10; i++ {
go func() {
for {
e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound))
if b != nil {
// 请求被拒绝,在此处进行处理
time.Sleep(time.Duration(rand.Uint64() % 10) * time.Millisecond)
} else {
// 请求允许通过,此处编写业务逻辑
fmt.Println(util.CurrentTimeMillis(), "Passed")
time.Sleep(time.Duration(rand.Uint64() % 10) * time.Millisecond)
// 务必保证业务结束后调用 Exit
e.Exit()
}
}
}()
}
<-ch
}-
文档
-
Documents (EN)