Skip to content
Snippets Groups Projects
Commit 4892ed1d authored by 邹毅贤's avatar 邹毅贤
Browse files

add start provider with random port

parent d62d41dc
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ package config ...@@ -20,6 +20,7 @@ package config
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
...@@ -105,6 +106,20 @@ func NewServiceConfig(id string, context context.Context) *ServiceConfig { ...@@ -105,6 +106,20 @@ func NewServiceConfig(id string, context context.Context) *ServiceConfig {
} }
} }
// Get Random Ports with size
func getRandomPorts(size int) []int {
ports := make([]int, 0, size)
if size <= 0 {
return ports
}
startPort := int(rand.Int31n(6000) + 10000)
for i := 0; i < size; i++ {
ports = append(ports, startPort+i*3)
}
return ports
}
// Export ... // Export ...
func (c *ServiceConfig) Export() error { func (c *ServiceConfig) Export() error {
// TODO: config center start here // TODO: config center start here
...@@ -123,11 +138,14 @@ func (c *ServiceConfig) Export() error { ...@@ -123,11 +138,14 @@ func (c *ServiceConfig) Export() error {
regUrls := loadRegistries(c.Registry, providerConfig.Registries, common.PROVIDER) regUrls := loadRegistries(c.Registry, providerConfig.Registries, common.PROVIDER)
urlMap := c.getUrlMap() urlMap := c.getUrlMap()
protocolConfigs := loadProtocol(c.Protocol, providerConfig.Protocols) protocolConfigs := loadProtocol(c.Protocol, providerConfig.Protocols)
if len(protocolConfigs) == 0 { protocolSize := len(protocolConfigs)
if protocolSize == 0 {
logger.Warnf("The service %v's '%v' protocols don't has right protocolConfigs ", c.InterfaceName, c.Protocol) logger.Warnf("The service %v's '%v' protocols don't has right protocolConfigs ", c.InterfaceName, c.Protocol)
return nil return nil
} }
for _, proto := range protocolConfigs {
ports := getRandomPorts(protocolSize)
for i, proto := range protocolConfigs {
// registry the service reflect // registry the service reflect
methods, err := common.ServiceMap.Register(c.InterfaceName, proto.Name, c.rpcService) methods, err := common.ServiceMap.Register(c.InterfaceName, proto.Name, c.rpcService)
if err != nil { if err != nil {
...@@ -135,11 +153,15 @@ func (c *ServiceConfig) Export() error { ...@@ -135,11 +153,15 @@ func (c *ServiceConfig) Export() error {
logger.Errorf(err.Error()) logger.Errorf(err.Error())
return err return err
} }
port := proto.Port
if len(proto.Port) == 0 {
port = strconv.Itoa(ports[i])
}
ivkURL := common.NewURLWithOptions( ivkURL := common.NewURLWithOptions(
common.WithPath(c.id), common.WithPath(c.id),
common.WithProtocol(proto.Name), common.WithProtocol(proto.Name),
common.WithIp(proto.Ip), common.WithIp(proto.Ip),
common.WithPort(proto.Port), common.WithPort(port),
common.WithParams(urlMap), common.WithParams(urlMap),
common.WithParamsValue(constant.BEAN_NAME_KEY, c.id), common.WithParamsValue(constant.BEAN_NAME_KEY, c.id),
common.WithMethods(strings.Split(methods, ",")), common.WithMethods(strings.Split(methods, ",")),
......
...@@ -189,3 +189,12 @@ func Test_Export(t *testing.T) { ...@@ -189,3 +189,12 @@ func Test_Export(t *testing.T) {
} }
providerConfig = nil providerConfig = nil
} }
func Test_getRandomPorts(t *testing.T) {
ports := getRandomPorts(3)
t.Logf("len:%v", len(ports))
for _, port := range ports {
t.Logf("port:%v", port)
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment