Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
4892ed1d
Commit
4892ed1d
authored
5 years ago
by
邹毅贤
Browse files
Options
Downloads
Patches
Plain Diff
add start provider with random port
parent
d62d41dc
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
config/service_config.go
+25
-3
25 additions, 3 deletions
config/service_config.go
config/service_config_test.go
+9
-0
9 additions, 0 deletions
config/service_config_test.go
with
34 additions
and
3 deletions
config/service_config.go
+
25
−
3
View file @
4892ed1d
...
@@ -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
(
p
roto
.
P
ort
),
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
,
","
)),
...
...
This diff is collapsed.
Click to expand it.
config/service_config_test.go
+
9
−
0
View file @
4892ed1d
...
@@ -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
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment