Commit e5730381 authored by zhaoyunxing's avatar zhaoyunxing

add:config client

parent 4a4e3770
......@@ -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
}
......@@ -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)
})
}
Markdown is supported
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