From b32eb302123b9fbec711d9e5e845a8f07bc287a1 Mon Sep 17 00:00:00 2001 From: houseme Date: Mon, 20 Mar 2023 09:52:58 +0800 Subject: [PATCH] feat: improve polaris register and upgrade polaris-go version v1.3.0 (#2524) --- contrib/registry/polaris/go.mod | 2 +- contrib/registry/polaris/go.sum | 2 ++ contrib/registry/polaris/polaris.go | 15 ++++++++++----- contrib/registry/polaris/polaris_registrar.go | 17 ++++++++++------- contrib/registry/polaris/polaris_service.go | 6 ++++++ example/go.mod | 2 +- example/go.sum | 3 ++- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/contrib/registry/polaris/go.mod b/contrib/registry/polaris/go.mod index 40fef153d..60016124d 100644 --- a/contrib/registry/polaris/go.mod +++ b/contrib/registry/polaris/go.mod @@ -4,7 +4,7 @@ go 1.15 require ( github.com/gogf/gf/v2 v2.0.0 - github.com/polarismesh/polaris-go v1.2.0-beta.3 + github.com/polarismesh/polaris-go v1.3.0 ) replace github.com/gogf/gf/v2 => ../../../ diff --git a/contrib/registry/polaris/go.sum b/contrib/registry/polaris/go.sum index d4128b6d6..d599a334a 100644 --- a/contrib/registry/polaris/go.sum +++ b/contrib/registry/polaris/go.sum @@ -229,6 +229,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/polarismesh/polaris-go v1.2.0-beta.3 h1:sqN50VGign37xVFp9nrN1RoLXacsB0QfRYUtuCWMJGI= github.com/polarismesh/polaris-go v1.2.0-beta.3/go.mod h1:HsN0ierETIujHpmnnYJ3qkwQw4QGAECuHvBZTDaw1tI= +github.com/polarismesh/polaris-go v1.3.0 h1:KZKX//ow4OPPoS5+s7h07ptprg+2AcNVGrN6WakC9QM= +github.com/polarismesh/polaris-go v1.3.0/go.mod h1:HsN0ierETIujHpmnnYJ3qkwQw4QGAECuHvBZTDaw1tI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= diff --git a/contrib/registry/polaris/polaris.go b/contrib/registry/polaris/polaris.go index 289c62f00..36df775ae 100644 --- a/contrib/registry/polaris/polaris.go +++ b/contrib/registry/polaris/polaris.go @@ -10,11 +10,12 @@ package polaris import ( "time" + "github.com/polarismesh/polaris-go" + "github.com/polarismesh/polaris-go/pkg/config" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gsvc" "github.com/gogf/gf/v2/os/glog" - "github.com/polarismesh/polaris-go" - "github.com/polarismesh/polaris-go/pkg/config" ) var ( @@ -46,16 +47,19 @@ type options struct { // To show service is healthy or not. Default value is True. Healthy bool - // Heartbeat enable .Not in polaris . Default value is True. + // Deprecated: Use RegisterInstance instead. + // Service registration is performed synchronously, + // and heartbeat reporting is automatically performed + // Heartbeat enable .Not in polaris. Default value is True. Heartbeat bool // To show service is isolate or not. Default value is False. Isolate bool - // TTL timeout. if node needs to use heartbeat to report,required. If not set,server will throw ErrorCode-400141 + // TTL timeout. if the node needs to use a heartbeat to report, required. If not set,server will throw ErrorCode-400141 TTL int - // optional, Timeout for single query. Default value is global config + // Timeout for the single query. Default value is global config // Total is (1+RetryCount) * Timeout Timeout time.Duration @@ -123,6 +127,7 @@ func WithRetryCount(retryCount int) Option { } // WithHeartbeat with the Heartbeat option. +// Deprecated remove in v2.4.0 func WithHeartbeat(heartbeat bool) Option { return func(o *options) { o.Heartbeat = heartbeat } } diff --git a/contrib/registry/polaris/polaris_registrar.go b/contrib/registry/polaris/polaris_registrar.go index 56f760a4c..0efb02c89 100644 --- a/contrib/registry/polaris/polaris_registrar.go +++ b/contrib/registry/polaris/polaris_registrar.go @@ -10,16 +10,17 @@ import ( "context" "time" + "github.com/polarismesh/polaris-go" + "github.com/polarismesh/polaris-go/pkg/model" + "github.com/gogf/gf/v2/net/gsvc" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gconv" - "github.com/polarismesh/polaris-go" - "github.com/polarismesh/polaris-go/pkg/model" ) // Register the registration. func (r *Registry) Register(ctx context.Context, service gsvc.Service) (gsvc.Service, error) { - // Replace input service to custom service type. + // Replace input service to custom service types. service = &Service{ Service: service, } @@ -47,8 +48,9 @@ func (r *Registry) Register(ctx context.Context, service gsvc.Service) (gsvc.Ser rmd[k] = v } } - // Register - registeredService, err := r.provider.Register( + // Register RegisterInstance Service registration is performed synchronously, + // and heartbeat reporting is automatically performed + registeredService, err := r.provider.RegisterInstance( &polaris.InstanceRegisterRequest{ InstanceRegisterRequest: model.InstanceRegisterRequest{ Service: service.GetPrefix(), @@ -72,7 +74,7 @@ func (r *Registry) Register(ctx context.Context, service gsvc.Service) (gsvc.Ser return nil, err } if r.opt.Heartbeat { - r.doHeartBeat(ctx, registeredService.InstanceID, service, endpoint) + // r.doHeartBeat(ctx, registeredService.InstanceID, service, endpoint) } ids = append(ids, registeredService.InstanceID) } @@ -83,7 +85,7 @@ func (r *Registry) Register(ctx context.Context, service gsvc.Service) (gsvc.Ser // Deregister the registration. func (r *Registry) Deregister(ctx context.Context, service gsvc.Service) error { - r.c <- struct{}{} + // r.c <- struct{}{} var ( err error split = gstr.Split(service.(*Service).ID, instanceIDSeparator) @@ -111,6 +113,7 @@ func (r *Registry) Deregister(ctx context.Context, service gsvc.Service) error { return nil } +// Deprecated . func (r *Registry) doHeartBeat(ctx context.Context, instanceID string, service gsvc.Service, endpoint gsvc.Endpoint) { go func() { ticker := time.NewTicker(time.Second * time.Duration(r.opt.TTL)) diff --git a/contrib/registry/polaris/polaris_service.go b/contrib/registry/polaris/polaris_service.go index 63307fd80..28daea577 100644 --- a/contrib/registry/polaris/polaris_service.go +++ b/contrib/registry/polaris/polaris_service.go @@ -1,3 +1,9 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package polaris import ( diff --git a/example/go.mod b/example/go.mod index 478f75fc7..1a9b431b9 100644 --- a/example/go.mod +++ b/example/go.mod @@ -18,7 +18,7 @@ require ( github.com/gogo/protobuf v1.3.2 github.com/golang/protobuf v1.5.2 github.com/nacos-group/nacos-sdk-go v1.1.2 - github.com/polarismesh/polaris-go v1.2.0-beta.3 + github.com/polarismesh/polaris-go v1.3.0 google.golang.org/grpc v1.49.0 google.golang.org/protobuf v1.28.1 k8s.io/client-go v0.25.2 diff --git a/example/go.sum b/example/go.sum index c7b5e316e..b66b86615 100644 --- a/example/go.sum +++ b/example/go.sum @@ -442,8 +442,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/polaris-go v1.2.0-beta.3 h1:sqN50VGign37xVFp9nrN1RoLXacsB0QfRYUtuCWMJGI= github.com/polarismesh/polaris-go v1.2.0-beta.3/go.mod h1:HsN0ierETIujHpmnnYJ3qkwQw4QGAECuHvBZTDaw1tI= +github.com/polarismesh/polaris-go v1.3.0 h1:KZKX//ow4OPPoS5+s7h07ptprg+2AcNVGrN6WakC9QM= +github.com/polarismesh/polaris-go v1.3.0/go.mod h1:HsN0ierETIujHpmnnYJ3qkwQw4QGAECuHvBZTDaw1tI= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=