Skip to content

Commit

Permalink
set random port
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Aug 1, 2024
1 parent cde8a93 commit 4e71285
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
10 changes: 8 additions & 2 deletions cluster/router/script/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions common/host_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package common

import (
"fmt"
"os"
"strconv"
"strings"
Expand All @@ -26,6 +27,8 @@ import (
import (
"github.com/dubbogo/gost/log/logger"
gxnet "github.com/dubbogo/gost/net"

perrors "github.com/pkg/errors"
)

import (
Expand Down Expand Up @@ -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]
}
2 changes: 1 addition & 1 deletion config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package config

import (
"dubbo.apache.org/dubbo-go/v3/metadata/service"
"sync"
)

Expand All @@ -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"
)

Expand Down
8 changes: 1 addition & 7 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down
16 changes: 8 additions & 8 deletions metadata/service/exporter/configurable/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 4e71285

Please sign in to comment.