Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
e5730381
Commit
e5730381
authored
May 26, 2021
by
zhaoyunxing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add:config client
parent
4a4e3770
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
20 deletions
+75
-20
client.go
database/kv/nacos/client.go
+45
-8
client_test.go
database/kv/nacos/client_test.go
+30
-12
No files found.
database/kv/nacos/client.go
View file @
e5730381
...
...
@@ -18,6 +18,8 @@
package
nacos
import
(
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/vo"
"sync"
)
...
...
@@ -28,8 +30,10 @@ import (
)
var
(
clientPool
nacosClientPool
clientPoolOnce
sync
.
Once
clientPool
nacosClientPool
configClientPool
nacosConfigClientPool
clientPoolOnce
sync
.
Once
configClientPoolOnce
sync
.
Once
)
type
nacosClientPool
struct
{
...
...
@@ -37,12 +41,21 @@ type nacosClientPool struct {
namingClient
map
[
string
]
naming_client
.
INamingClient
}
type
nacosConfigClientPool
struct
{
sync
.
Mutex
configClient
map
[
string
]
config_client
.
IConfigClient
}
func
initNacosClientPool
()
{
clientPool
.
namingClient
=
make
(
map
[
string
]
naming_client
.
INamingClient
)
}
// NewNamingClient create nacos client
func
NewNamingClient
(
name
string
,
share
bool
,
sc
[]
constant
.
ServerConfig
,
func
initNacosConfigClientPool
()
{
configClientPool
.
configClient
=
make
(
map
[
string
]
config_client
.
IConfigClient
)
}
// NewNacosNamingClient create nacos client
func
NewNacosNamingClient
(
name
string
,
share
bool
,
sc
[]
constant
.
ServerConfig
,
cc
constant
.
ClientConfig
)
(
naming_client
.
INamingClient
,
error
)
{
if
share
{
clientPoolOnce
.
Do
(
initNacosClientPool
)
...
...
@@ -52,12 +65,36 @@ func NewNamingClient(name string, share bool, sc []constant.ServerConfig,
return
client
,
nil
}
}
configMap
:=
make
(
map
[
string
]
interface
{},
2
)
configMap
[
"serverConfigs"
]
=
sc
configMap
[
"clientConfig"
]
=
cc
client
,
err
:=
clients
.
CreateNamingClient
(
configMap
)
cfg
:=
vo
.
NacosClientParam
{
ClientConfig
:
&
cc
,
ServerConfigs
:
sc
}
client
,
err
:=
clients
.
NewNamingClient
(
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
if
share
{
clientPool
.
namingClient
[
name
]
=
client
}
return
client
,
err
}
// NewNacosConfigClient create config client
func
NewNacosConfigClient
(
name
string
,
share
bool
,
sc
[]
constant
.
ServerConfig
,
cc
constant
.
ClientConfig
)
(
config_client
.
IConfigClient
,
error
)
{
if
share
{
configClientPoolOnce
.
Do
(
initNacosConfigClientPool
)
configClientPool
.
Lock
()
defer
configClientPool
.
Unlock
()
if
client
,
ok
:=
configClientPool
.
configClient
[
name
];
ok
{
return
client
,
nil
}
}
cfg
:=
vo
.
NacosClientParam
{
ClientConfig
:
&
cc
,
ServerConfigs
:
sc
}
client
,
err
:=
clients
.
NewConfigClient
(
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
if
share
{
configClientPool
.
configClient
[
name
]
=
client
}
return
client
,
err
}
database/kv/nacos/client_test.go
View file @
e5730381
...
...
@@ -17,29 +17,47 @@
package
nacos
import
"testing"
import
(
"testing"
)
import
(
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/stretchr/testify/assert"
)
func
TestNewNamingClient
(
t
*
testing
.
T
)
{
scs
:=
make
([]
constant
.
ServerConfig
,
0
,
1
)
scs
=
append
(
scs
,
constant
.
ServerConfig
{
IpAddr
:
"console.nacos.io"
,
Port
:
80
})
func
TestNewNacosClient
(
t
*
testing
.
T
)
{
scs
:=
[]
constant
.
ServerConfig
{
*
constant
.
NewServerConfig
(
"console.nacos.io"
,
80
),
}
cc
:=
constant
.
ClientConfig
{
TimeoutMs
:
5
*
1000
,
NotLoadCacheAtStart
:
true
,
}
client1
,
err
:=
NewNamingClient
(
"nacos"
,
true
,
scs
,
cc
)
client2
,
err
:=
NewNamingClient
(
"nacos"
,
true
,
scs
,
cc
)
client3
,
err
:=
NewNamingClient
(
"nacos"
,
false
,
scs
,
cc
)
client4
,
err
:=
NewNamingClient
(
"test"
,
true
,
scs
,
cc
)
t
.
Run
(
"naming_client"
,
func
(
t
*
testing
.
T
)
{
client1
,
err
:=
NewNacosNamingClient
(
"nacos"
,
true
,
scs
,
cc
)
client2
,
err
:=
NewNacosNamingClient
(
"nacos"
,
true
,
scs
,
cc
)
client3
,
err
:=
NewNacosNamingClient
(
"nacos"
,
false
,
scs
,
cc
)
client4
,
err
:=
NewNacosNamingClient
(
"test"
,
true
,
scs
,
cc
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
client1
,
client2
)
assert
.
NotEqual
(
t
,
client1
,
client3
)
assert
.
NotEqual
(
t
,
client1
,
client4
)
})
t
.
Run
(
"config_client"
,
func
(
t
*
testing
.
T
)
{
client1
,
err
:=
NewNacosConfigClient
(
"nacos"
,
true
,
scs
,
cc
)
client2
,
err
:=
NewNacosConfigClient
(
"nacos"
,
true
,
scs
,
cc
)
client3
,
err
:=
NewNacosConfigClient
(
"nacos"
,
false
,
scs
,
cc
)
client4
,
err
:=
NewNacosConfigClient
(
"test"
,
true
,
scs
,
cc
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
client1
,
client2
)
assert
.
NotEqual
(
t
,
client1
,
client3
)
assert
.
NotEqual
(
t
,
client1
,
client4
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
client1
,
client2
)
assert
.
Equal
(
t
,
client1
,
client3
)
assert
.
Equal
(
t
,
client1
,
client4
)
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment