Skip to content

Commit

Permalink
Merge pull request douyu#70 from douyu/feat/sentinel-integration
Browse files Browse the repository at this point in the history
import sentinel v0.4.0

Former-commit-id: 4abcee5
  • Loading branch information
gorexlv authored Jun 23, 2020
2 parents 646651f + d74f814 commit fc8e008
Show file tree
Hide file tree
Showing 26 changed files with 614 additions and 206 deletions.
2 changes: 2 additions & 0 deletions example/all/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
type = "const"
param = 0.001

[jupiter.reliability.sentinel]
appName = "demo"
58 changes: 38 additions & 20 deletions example/all/internal/app/demo/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ package demo
import (
"time"

sentinel_echo "github.com/alibaba/sentinel-golang/adapter/echo"
"github.com/alibaba/sentinel-golang/core/flow"
"github.com/douyu/jupiter"
"github.com/douyu/jupiter/example/all/internal/app/greeter"
"github.com/douyu/jupiter/pkg/sentinel"
"github.com/douyu/jupiter/pkg/server/xecho"
"github.com/douyu/jupiter/pkg/server/xgrpc"
"github.com/douyu/jupiter/pkg/util/xgo"
"github.com/douyu/jupiter/pkg/worker/xcron"
"github.com/douyu/jupiter/pkg/xlog"
"github.com/labstack/echo/v4"
"google.golang.org/grpc/examples/helloworld/helloworld"

sentinel_grpc "github.com/alibaba/sentinel-golang/adapter/grpc"
)

type Engine struct {
Expand All @@ -36,9 +41,8 @@ func NewEngine() *Engine {
eng := &Engine{}

if err := eng.Startup(
eng.loadConfig,
// eng.printLogs,
// eng.startJobs,
eng.startJobs,
eng.initSentinel,
xgo.ParallelWithError(
eng.serveHTTP,
eng.serveGRPC,
Expand All @@ -47,24 +51,17 @@ func NewEngine() *Engine {
xlog.Panic("startup engine", xlog.Any("err", err))
}

// flush logger before exit application
eng.Defer(eng.flushLogger)
return eng
}

func (eng *Engine) loadConfig() error {
// todo something
return nil
}

func (eng *Engine) startCacheMonitor() error {
// todo something
return nil
}

func (eng *Engine) flushLogger() error {
// todo something
return nil
func (eng *Engine) initSentinel() error {
var config = sentinel.DefaultConfig()
config.FlowRules = append(config.FlowRules, &flow.FlowRule{
Resource: "GET:/ping",
MetricType: flow.QPS,
Count: 1,
})
return config.InitSentinelCoreComponent()
}

func (eng *Engine) startJobs() error {
Expand All @@ -75,21 +72,42 @@ func (eng *Engine) startJobs() error {

func (eng *Engine) serveHTTP() error {
server := xecho.StdConfig("http").Build()
server.Use(
sentinel_echo.SentinelMiddleware(
// customize resource extractor if required
// method_path by default
sentinel_echo.WithResourceExtractor(func(ctx echo.Context) string {
return ctx.Request().Method + ":" + ctx.Path()
}),
// customize block fallback if required
// abort with status 429 by default
sentinel_echo.WithBlockFallback(func(ctx echo.Context) error {
return ctx.JSON(400, map[string]interface{}{
"err": "too many requests; the quota used up",
"code": 10222,
})
}),
),
)
server.GET("/ping", func(ctx echo.Context) error {
return ctx.JSON(200, "pong")
})
server.GET("/panic", func(ctx echo.Context) error {
panic("panic")
})
//this is a demo: support proxy for http to grpc controller
// this is a demo: support proxy for http to grpc controller
g := greeter.Greeter{}
server.GET("/grpc", xecho.GRPCProxyWrapper(g.SayHello))
server.POST("/grpc-post", xecho.GRPCProxyWrapper(g.SayHello))
return eng.Serve(server)
}

func (eng *Engine) serveGRPC() error {
server := xgrpc.StdConfig("grpc").Build()
server := xgrpc.StdConfig("grpc").
WithUnaryInterceptor(sentinel_grpc.NewUnaryServerInterceptor()).
WithStreamInterceptor(sentinel_grpc.NewStreamServerInterceptor()).
Build()

helloworld.RegisterGreeterServer(server.Server, new(greeter.Greeter))
return eng.Serve(server)
}
Expand Down
5 changes: 3 additions & 2 deletions example/client/etcd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package main

import (
"context"
"time"

"github.com/coreos/etcd/clientv3"
"github.com/douyu/jupiter"
"github.com/douyu/jupiter/pkg/client/etcdv3"
"github.com/douyu/jupiter/pkg/xlog"
"go.etcd.io/etcd/clientv3"
"time"
)

func main() {
Expand Down
1 change: 1 addition & 0 deletions example/http/gin/gin.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
de8fd896ddd03628eae8a250d1820254f2aa6720
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ go 1.14

require (
github.com/BurntSushi/toml v0.3.1
github.com/alibaba/sentinel-golang v0.4.0
github.com/apache/rocketmq-client-go v0.0.0-20191211114916-85ee94b43cef
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/etcd v3.3.22+incompatible
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4 // indirect
github.com/fatih/structtag v1.2.0
github.com/flosch/pongo2 v0.0.0-20200518135938-dfb43dbdc22a
github.com/fsnotify/fsnotify v1.4.9
Expand All @@ -19,8 +20,8 @@ require (
github.com/go-resty/resty/v2 v2.2.0
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/jinzhu/gorm v1.9.12
github.com/json-iterator/go v1.1.9
Expand All @@ -39,7 +40,6 @@ require (
github.com/tidwall/pretty v1.0.1
github.com/uber/jaeger-client-go v2.23.1+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
go.etcd.io/etcd v0.5.0-alpha.5.0.20200401174654-e694b7bb0875
go.uber.org/automaxprocs v1.3.0
go.uber.org/multierr v1.5.0
go.uber.org/zap v1.15.0
Expand All @@ -48,7 +48,5 @@ require (
golang.org/x/tools v0.0.0-20200527183253-8e7acdbce89d // indirect
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55
google.golang.org/grpc v1.26.0
sigs.k8s.io/yaml v1.2.0 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)

// replace github.com/coreos/bbolt => go.etcd.io/bbolt v1.3.3
Loading

0 comments on commit fc8e008

Please sign in to comment.