Skip to content

Commit

Permalink
refactor:重构grpc-go-polaris的相关版本设计 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Jun 21, 2024
1 parent b3e25c0 commit 3b9ea3e
Show file tree
Hide file tree
Showing 14 changed files with 1,221 additions and 241 deletions.
6 changes: 0 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
if options.Config != nil {
setPolarisConfig(options.Config)
}
if options.SDKContext != nil {
setPolarisContext(options.SDKContext)
}

if !strings.HasPrefix(target, prefix) {
// not polaris target, go through gRPC resolver
Expand Down Expand Up @@ -80,9 +77,6 @@ func BuildTarget(target string, opts ...DialOption) (string, error) {
if options.Config != nil {
setPolarisConfig(options.Config)
}
if options.SDKContext != nil {
setPolarisContext(options.SDKContext)
}

endpoint := base64.URLEncoding.EncodeToString(jsonStr)
target = fmt.Sprintf(prefix+"%s?%s=%s", target, optionsKey, endpoint)
Expand Down
2 changes: 1 addition & 1 deletion client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func WithPolarisConfig(polarisCfg config.Configuration) DialOption {
// WithPolarisContext set polaris SDKContext
func WithPolarisContext(sdkContext api.SDKContext) DialOption {
return newFuncDialOption(func(options *dialOptions) {
options.SDKContext = sdkContext
setPolarisContext(sdkContext)
})
}

Expand Down
2 changes: 1 addition & 1 deletion examples/circuitbreak/consumer/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
2 changes: 1 addition & 1 deletion examples/circuitbreak/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
21 changes: 11 additions & 10 deletions examples/quickstart/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"net"
"time"

"google.golang.org/grpc"

polaris "github.com/polarismesh/grpc-go-polaris"
"github.com/polarismesh/grpc-go-polaris/examples/common/pb"
)
Expand All @@ -45,23 +43,26 @@ func (h *EchoQuickStartService) Echo(ctx context.Context, req *pb.EchoRequest) (
}

func main() {
srv := grpc.NewServer()
address := fmt.Sprintf("0.0.0.0:%d", listenPort)
listen, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("Failed to addr %s: %v", address, err)
}
pb.RegisterEchoServerServer(srv, &EchoQuickStartService{

srv, err := polaris.NewServer(polaris.WithServiceName("QuickStartEchoServerGRPC"),
polaris.WithServerHost("127.0.0.1"),
polaris.WithDelayRegisterEnable(&polaris.WaitDelayStrategy{WaitTime: 10 * time.Second}),
polaris.WithGracefulStopEnable(10*time.Second))
if err != nil {
log.Fatalf("Failed to addr %s: %v", address, err)
}

pb.RegisterEchoServerServer(srv.Server, &EchoQuickStartService{
actualPort: listen.Addr().(*net.TCPAddr).Port,
})

// 启动服务
if err := polaris.Serve(srv, listen,
polaris.WithServiceName("QuickStartEchoServerGRPC"),
polaris.WithServerHost("127.0.0.1"),
polaris.WithDelayRegisterEnable(&polaris.WaitDelayStrategy{WaitTime: 10 * time.Second}),
polaris.WithGracefulStopEnable(10*time.Second),
); nil != err {
if err := srv.Serve(listen); nil != err {
log.Printf("listen err: %v", err)
}
}
23 changes: 13 additions & 10 deletions examples/ratelimit/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"log"
"net"

"google.golang.org/grpc"

polaris "github.com/polarismesh/grpc-go-polaris"
"github.com/polarismesh/grpc-go-polaris/examples/common/pb"
)
Expand All @@ -48,15 +46,20 @@ func main() {
if err != nil {
log.Fatalf("Failed to addr %s: %v", address, err)
}
listenAddr := listen.Addr().String()
fmt.Printf("listen address is %s\n", listenAddr)
interceptor := polaris.NewRateLimitInterceptor().WithServiceName("RateLimitEchoServerGRPC")
srv := grpc.NewServer(grpc.UnaryInterceptor(interceptor.UnaryInterceptor))
pb.RegisterEchoServerServer(srv, &EchoRateLimitService{})

srv, err := polaris.NewServer(polaris.WithServiceName("RateLimitEchoServerGRPC"),
polaris.WithServerHost("127.0.0.1"),
// 开启限流能力
polaris.WithPolarisRateLimit(),
)
if err != nil {
log.Fatalf("Failed to addr %s: %v", address, err)
}

pb.RegisterEchoServerServer(srv.Server, &EchoRateLimitService{})

// 启动服务
if err := polaris.Serve(srv, listen,
polaris.WithServiceName("RateLimitEchoServerGRPC"),
); nil != err {
if err := srv.Serve(listen); nil != err {
log.Printf("listen err: %v", err)
}
}
2 changes: 1 addition & 1 deletion examples/ratelimit/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
- 127.0.0.1:8091
2 changes: 1 addition & 1 deletion examples/routing/consumer/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
2 changes: 1 addition & 1 deletion examples/routing/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
27 changes: 14 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ module github.com/polarismesh/grpc-go-polaris
go 1.15

require (
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.3.0
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/polarismesh/polaris-go v1.5.2
github.com/polarismesh/specification v1.3.2
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/polarismesh/polaris-go v1.5.6
github.com/polarismesh/specification v1.5.1
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/net v0.14.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.26.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
)
Loading

0 comments on commit 3b9ea3e

Please sign in to comment.