From 8f8f514fa3cc955e417431f8148f26d78218a271 Mon Sep 17 00:00:00 2001 From: liaochuntao Date: Thu, 11 May 2023 21:16:29 +0800 Subject: [PATCH] refactor: remove providers and add prefix when use polaris (#2274) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:issue #2216 * fix:issue #2216 * fix:注册服务信息不带前缀 * refactor:remove providers: prefix when use polaris * refactor:remove providers: prefix when use polaris --- cluster/router/polaris/router.go | 2 +- common/constant/default.go | 4 +++- filter/polaris/limit/limiter.go | 2 +- protocol/dubbo3/dubbo3_invoker.go | 3 ++- protocol/dubbo3/dubbo3_protocol.go | 3 ++- protocol/grpc/client.go | 1 + protocol/grpc/client_test.go | 3 ++- protocol/grpc/server.go | 1 + registry/polaris/registry.go | 19 +++++++++++-------- registry/polaris/utils.go | 28 ---------------------------- tools/dubbogo-cli/cmd/show.go | 3 ++- 11 files changed, 26 insertions(+), 43 deletions(-) diff --git a/cluster/router/polaris/router.go b/cluster/router/polaris/router.go index d29636dd7f..1b9e731eca 100644 --- a/cluster/router/polaris/router.go +++ b/cluster/router/polaris/router.go @@ -153,7 +153,7 @@ func getService(url *common.URL) string { } } - service := "providers:" + url.Service() + service := url.Interface() if applicationMode { service = config.GetApplicationConfig().Name } diff --git a/common/constant/default.go b/common/constant/default.go index 8c0f4ffbd3..49b764565f 100644 --- a/common/constant/default.go +++ b/common/constant/default.go @@ -17,7 +17,9 @@ package constant -import "math" +import ( + "math" +) const ( Dubbo = "dubbo" diff --git a/filter/polaris/limit/limiter.go b/filter/polaris/limit/limiter.go index 4d68660f6c..e4bdbc262f 100644 --- a/filter/polaris/limit/limiter.go +++ b/filter/polaris/limit/limiter.go @@ -83,7 +83,7 @@ func (pl *polarisTpsLimiter) buildQuotaRequest(url *common.URL, invoaction proto } } - svc := "providers:" + url.Service() + svc := url.Interface() method := invoaction.MethodName() if applicationMode { svc = config.GetApplicationConfig().Name diff --git a/protocol/dubbo3/dubbo3_invoker.go b/protocol/dubbo3/dubbo3_invoker.go index 29f2af03fa..327b58d3bd 100644 --- a/protocol/dubbo3/dubbo3_invoker.go +++ b/protocol/dubbo3/dubbo3_invoker.go @@ -28,13 +28,14 @@ import ( import ( "github.com/dubbogo/gost/log/logger" - "github.com/dustin/go-humanize" "github.com/dubbogo/grpc-go/metadata" tripleConstant "github.com/dubbogo/triple/pkg/common/constant" triConfig "github.com/dubbogo/triple/pkg/config" "github.com/dubbogo/triple/pkg/triple" + + "github.com/dustin/go-humanize" ) import ( diff --git a/protocol/dubbo3/dubbo3_protocol.go b/protocol/dubbo3/dubbo3_protocol.go index ea5a9ece83..66caf28362 100644 --- a/protocol/dubbo3/dubbo3_protocol.go +++ b/protocol/dubbo3/dubbo3_protocol.go @@ -26,7 +26,6 @@ import ( import ( "github.com/dubbogo/gost/log/logger" - "github.com/dustin/go-humanize" "github.com/dubbogo/grpc-go" "github.com/dubbogo/grpc-go/metadata" @@ -34,6 +33,8 @@ import ( tripleConstant "github.com/dubbogo/triple/pkg/common/constant" triConfig "github.com/dubbogo/triple/pkg/config" "github.com/dubbogo/triple/pkg/triple" + + "github.com/dustin/go-humanize" ) import ( diff --git a/protocol/grpc/client.go b/protocol/grpc/client.go index fdb127858b..2d6ad4da7d 100644 --- a/protocol/grpc/client.go +++ b/protocol/grpc/client.go @@ -25,6 +25,7 @@ import ( import ( "github.com/dubbogo/gost/log/logger" + "github.com/dustin/go-humanize" "github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc" diff --git a/protocol/grpc/client_test.go b/protocol/grpc/client_test.go index 4dc48689e8..0b87c12838 100644 --- a/protocol/grpc/client_test.go +++ b/protocol/grpc/client_test.go @@ -20,11 +20,12 @@ package grpc import ( "context" "fmt" - "github.com/dustin/go-humanize" "testing" ) import ( + "github.com/dustin/go-humanize" + "github.com/stretchr/testify/assert" ) diff --git a/protocol/grpc/server.go b/protocol/grpc/server.go index f1538630c7..5efd3171d3 100644 --- a/protocol/grpc/server.go +++ b/protocol/grpc/server.go @@ -27,6 +27,7 @@ import ( import ( "github.com/dubbogo/gost/log/logger" + "github.com/dustin/go-humanize" "github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc" diff --git a/registry/polaris/registry.go b/registry/polaris/registry.go index d156e8ae0b..dad5ea0588 100644 --- a/registry/polaris/registry.go +++ b/registry/polaris/registry.go @@ -92,8 +92,11 @@ type polarisRegistry struct { // Register will register the service @url to its polaris registry center. func (pr *polarisRegistry) Register(url *common.URL) error { + if getCategory(url) != "providers" { + return nil + } - serviceName := getServiceName(url) + serviceName := url.Interface() request := createRegisterParam(url, serviceName) request.Namespace = pr.namespace resp, err := pr.provider.RegisterInstance(request) @@ -115,11 +118,11 @@ func (pr *polarisRegistry) Register(url *common.URL) error { } // UnRegister returns nil if unregister successfully. If not, returns an error. -func (pr *polarisRegistry) UnRegister(conf *common.URL) error { - request := createDeregisterParam(conf, getServiceName(conf)) +func (pr *polarisRegistry) UnRegister(url *common.URL) error { + request := createDeregisterParam(url, url.Interface()) request.Namespace = pr.namespace if err := pr.provider.Deregister(request); err != nil { - return perrors.WithMessagef(err, "register(conf:%+v)", conf) + return perrors.WithMessagef(err, "fail to deregister(conf:%+v)", url) } return nil } @@ -135,7 +138,7 @@ func (pr *polarisRegistry) Subscribe(url *common.URL, notifyListener registry.No defer timer.Stop() for { - serviceName := getSubscribeName(url) + serviceName := url.Interface() watcher, err := pr.createPolarisWatcher(serviceName) if err != nil { logger.Warnf("getwatcher() = err:%v", perrors.WithStack(err)) @@ -174,9 +177,9 @@ func (pr *polarisRegistry) UnSubscribe(url *common.URL, notifyListener registry. // LoadSubscribeInstances load subscribe instance func (pr *polarisRegistry) LoadSubscribeInstances(url *common.URL, notify registry.NotifyListener) error { - serviceName := getSubscribeName(url) - resp, err := pr.consumer.GetAllInstances(&api.GetAllInstancesRequest{ - GetAllInstancesRequest: model.GetAllInstancesRequest{ + serviceName := url.Interface() + resp, err := pr.consumer.GetInstances(&api.GetInstancesRequest{ + GetInstancesRequest: model.GetInstancesRequest{ Service: serviceName, Namespace: pr.namespace, }, diff --git a/registry/polaris/utils.go b/registry/polaris/utils.go index 82e06c9c62..cdd6d97d19 100644 --- a/registry/polaris/utils.go +++ b/registry/polaris/utils.go @@ -18,10 +18,8 @@ package polaris import ( - "bytes" "fmt" "strconv" - "strings" ) import ( @@ -45,29 +43,3 @@ func getCategory(url *common.URL) string { category := common.DubboNodes[role] return category } - -// just copy from dubbo-go for nacos -func getServiceName(url *common.URL) string { - var buffer bytes.Buffer - - buffer.Write([]byte(getCategory(url))) - appendParam(&buffer, url, constant.InterfaceKey) - return buffer.String() -} - -func getSubscribeName(url *common.URL) string { - var buffer bytes.Buffer - - buffer.Write([]byte(common.DubboNodes[common.PROVIDER])) - appendParam(&buffer, url, constant.InterfaceKey) - return buffer.String() -} - -// just copy from dubbo-go for nacos -func appendParam(target *bytes.Buffer, url *common.URL, key string) { - value := url.GetParam(key, "") - target.Write([]byte(constant.PolarisServiceNameSeparator)) - if strings.TrimSpace(value) != "" { - target.Write([]byte(value)) - } -} diff --git a/tools/dubbogo-cli/cmd/show.go b/tools/dubbogo-cli/cmd/show.go index 991cbe5d11..d730eae1a4 100644 --- a/tools/dubbogo-cli/cmd/show.go +++ b/tools/dubbogo-cli/cmd/show.go @@ -25,8 +25,9 @@ import ( ) import ( - "github.com/spf13/cobra" "github.com/olekukonko/tablewriter" + + "github.com/spf13/cobra" ) import (