From 4e71285ddee9ae0389a0d52f387ce8ab1a9cb335 Mon Sep 17 00:00:00 2001 From: chickenlj Date: Thu, 1 Aug 2024 23:24:37 +0800 Subject: [PATCH] set random port --- cluster/router/script/router_test.go | 10 ++++++++-- common/host_util.go | 12 ++++++++++++ config/root_config.go | 2 +- config/service_config.go | 8 +------- metadata/metadata.go | 2 +- .../service/exporter/configurable/exporter.go | 16 ++++++++-------- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/cluster/router/script/router_test.go b/cluster/router/script/router_test.go index bf3ff7551c..a7fcb4d8b9 100644 --- a/cluster/router/script/router_test.go +++ b/cluster/router/script/router_test.go @@ -19,13 +19,19 @@ package script import ( "context" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/config_center" "dubbo.apache.org/dubbo-go/v3/protocol" "dubbo.apache.org/dubbo-go/v3/protocol/invocation" "dubbo.apache.org/dubbo-go/v3/remoting" - "github.com/stretchr/testify/assert" - "testing" ) var url1 = func() *common.URL { diff --git a/common/host_util.go b/common/host_util.go index a1bbc4fa8d..de42c7995b 100644 --- a/common/host_util.go +++ b/common/host_util.go @@ -18,6 +18,7 @@ package common import ( + "fmt" "os" "strconv" "strings" @@ -26,6 +27,8 @@ import ( import ( "github.com/dubbogo/gost/log/logger" gxnet "github.com/dubbogo/gost/net" + + perrors "github.com/pkg/errors" ) import ( @@ -106,3 +109,12 @@ func IsMatchGlobPattern(pattern, value string) bool { return strings.HasPrefix(value, prefix) && strings.HasSuffix(value, suffix) } } + +func GetRandomPort(ip string) string { + tcp, err := gxnet.ListenOnTCPRandomPort(ip) + if err != nil { + panic(perrors.New(fmt.Sprintf("Get tcp port error, err is {%v}", err))) + } + defer tcp.Close() + return strings.Split(tcp.Addr().String(), ":")[1] +} diff --git a/config/root_config.go b/config/root_config.go index 569c603c94..12f835df0f 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -18,7 +18,6 @@ package config import ( - "dubbo.apache.org/dubbo-go/v3/metadata/service" "sync" ) @@ -39,6 +38,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/config_center" + "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter" ) diff --git a/config/service_config.go b/config/service_config.go index 65f3dc6146..a60bd631cf 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -32,7 +32,6 @@ import ( "github.com/creasty/defaults" "github.com/dubbogo/gost/log/logger" - gxnet "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" @@ -222,12 +221,7 @@ func getRandomPort(protocolConfigs []*ProtocolConfig) *list.List { continue } - tcp, err := gxnet.ListenOnTCPRandomPort(proto.Ip) - if err != nil { - panic(perrors.New(fmt.Sprintf("Get tcp port error, err is {%v}", err))) - } - defer tcp.Close() - ports.PushBack(strings.Split(tcp.Addr().String(), ":")[1]) + ports.PushBack(common.GetRandomPort(proto.Ip)) } return ports } diff --git a/metadata/metadata.go b/metadata/metadata.go index 608b487f57..d5a76c7f0e 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -19,7 +19,6 @@ package metadata import ( - "dubbo.apache.org/dubbo-go/v3/metadata/service" "github.com/dubbogo/gost/log/logger" "go.uber.org/atomic" @@ -28,6 +27,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" + "dubbo.apache.org/dubbo-go/v3/metadata/service" ) var ( diff --git a/metadata/service/exporter/configurable/exporter.go b/metadata/service/exporter/configurable/exporter.go index 72a3e55090..3cffeb9e11 100644 --- a/metadata/service/exporter/configurable/exporter.go +++ b/metadata/service/exporter/configurable/exporter.go @@ -71,20 +71,20 @@ func (exporter *MetadataServiceExporter) Export() error { exporter.lock.Lock() defer exporter.lock.Unlock() var err error - err = exporter.exportV1() + pro, port := getMetadataProtocolAndPort() + err = exporter.exportV1(pro, port) if err != nil { return err } - err = exporter.exportV2() + err = exporter.exportV2(pro, port) return err } logger.Warnf("[Metadata Service] The MetadataService has been exported : %v ", exporter.ServiceConfig.GetExportedUrls()) return nil } -func (exporter *MetadataServiceExporter) exportV1() error { +func (exporter *MetadataServiceExporter) exportV1(pro, port string) error { version, _ := exporter.metadataService.Version() - pro, port := getMetadataProtocolAndPort() if pro == constant.DefaultProtocol { exporter.ServiceConfig = config.NewServiceConfigBuilder(). SetServiceID(constant.SimpleMetadataServiceName). @@ -125,9 +125,8 @@ func (exporter *MetadataServiceExporter) exportV1() error { } } -func (exporter *MetadataServiceExporter) exportV2() error { +func (exporter *MetadataServiceExporter) exportV2(pro, port string) error { info := server.MetadataServiceV2_ServiceInfo - pro, port := getMetadataProtocolAndPort() // v2 only supports triple protocol if pro == constant.DefaultProtocol { return nil @@ -180,15 +179,16 @@ func getMetadataProtocolAndPort() (string, string) { } if protocolConfig == nil { + port = common.GetRandomPort("0") if protocol == "" || protocol == constant.TriProtocol { protocolConfig = &config.ProtocolConfig{ Name: constant.TriProtocol, - Port: "0", // use a random port + Port: port, // use a random port } } else { protocolConfig = &config.ProtocolConfig{ Name: constant.DefaultProtocol, - Port: "0", // use a random port + Port: port, // use a random port } } }