Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filter): add sentinel samples. #706

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions filter/sentinel/go-client/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"context"
"dubbo.apache.org/dubbo-go/v3/client"
"dubbo.apache.org/dubbo-go/v3/common/constant"
_ "dubbo.apache.org/dubbo-go/v3/imports"
"github.com/alibaba/sentinel-golang/core/flow"
greet "github.com/apache/dubbo-go-samples/filter/proto"
"github.com/dubbogo/gost/log/logger"
"sync"
"sync/atomic"
)

func main() {
cli, err := client.NewClient(
client.WithClientURL("127.0.0.1:20000"),
)
if err != nil {
panic(err)
}

svc, err := greet.NewGreetService(cli, client.WithFilter(constant.SentinelConsumerFilterKey))
if err != nil {
panic(err)
}

// Limit the client's request to GreetService to 200QPS
_, err = flow.LoadRules([]*flow.Rule{
{
Resource: greet.GreetService_ServiceInfo.InterfaceName + "::",
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
Threshold: 200,
RelationStrategy: flow.CurrentResource,
StatIntervalInMs: 1000,
},
})
if err != nil {
panic(err)
}
wg := sync.WaitGroup{}
wg.Add(10)
pass := int64(0)
block := int64(0)
for i := 0; i < 10; i++ {
go func() {
for j := 0; j < 30; j++ {
resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
if err == nil {
atomic.AddInt64(&pass, 1)
logger.Info(resp.Greeting)
} else {
atomic.AddInt64(&block, 1)
logger.Error(err)
}
}
wg.Done()
}()
}
wg.Wait()
logger.Info("success:", pass, "fail:", block)
}
73 changes: 73 additions & 0 deletions filter/sentinel/go-server/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"context"
"dubbo.apache.org/dubbo-go/v3/common/constant"
_ "dubbo.apache.org/dubbo-go/v3/imports"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/server"
"github.com/alibaba/sentinel-golang/core/flow"
greet "github.com/apache/dubbo-go-samples/filter/proto"
"github.com/dubbogo/gost/log/logger"
)

type GreetTripleServer struct {
}

func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
resp := &greet.GreetResponse{Greeting: req.Name}
return resp, nil
}

func main() {
srv, err := server.NewServer(
server.WithServerProtocol(
protocol.WithPort(20000),
protocol.WithTriple(),
),
)
if err != nil {
panic(err)
}

if err = greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{},
server.WithFilter(constant.SentinelProviderFilterKey),
); err != nil {
panic(err)
}

// Limit the request flow processed by GreetService to 100QPS
_, err = flow.LoadRules([]*flow.Rule{
{
Resource: greet.GreetService_ServiceInfo.InterfaceName + "::",
// MetricType: flow.QPS,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
Threshold: 100,
RelationStrategy: flow.CurrentResource,
},
})
if err != nil {
panic(err)
}
if err := srv.Serve(); err != nil {
logger.Error(err)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
dubbo.apache.org/dubbo-go/v3 v3.1.1-0.20240202021041-198504bb0029
github.com/SkyAPM/go2sky v1.5.0
github.com/SkyAPM/go2sky-plugins/dubbo-go v0.0.0-20220718123631-cb8f743b16cf
github.com/alibaba/sentinel-golang v1.0.4
github.com/apache/dubbo-go-hessian2 v1.12.2
github.com/dubbogo/gost v1.14.0
github.com/dubbogo/grpc-go v1.42.10
Expand Down
Loading