diff --git a/contrib/config/nacos/go.mod b/contrib/config/nacos/go.mod index cc2207b9ad9..cfd7bdeb4aa 100644 --- a/contrib/config/nacos/go.mod +++ b/contrib/config/nacos/go.mod @@ -1,6 +1,6 @@ module github.com/go-kratos/kratos/contrib/config/nacos/v2 -go 1.21 +go 1.19 require ( github.com/go-kratos/kratos/v2 v2.8.3 @@ -23,7 +23,7 @@ require ( go.uber.org/atomic v1.6.0 // indirect go.uber.org/multierr v1.5.0 // indirect go.uber.org/zap v1.15.0 // indirect - google.golang.org/protobuf v1.36.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/ini.v1 v1.42.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/contrib/config/nacos/go.sum b/contrib/config/nacos/go.sum index 506438df2c8..15cc531420d 100644 --- a/contrib/config/nacos/go.sum +++ b/contrib/config/nacos/go.sum @@ -103,7 +103,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= -google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/contrib/nacos/config.go b/contrib/nacos/config.go index f8858f2e533..980810b4dd7 100644 --- a/contrib/nacos/config.go +++ b/contrib/nacos/config.go @@ -2,11 +2,13 @@ package nacos import ( "context" - kconfig "github.com/go-kratos/kratos/v2/config" - "github.com/nacos-group/nacos-sdk-go/v2/clients/config_client" - "github.com/nacos-group/nacos-sdk-go/v2/vo" "path/filepath" "strings" + + "github.com/go-kratos/kratos/v2/config" + + "github.com/nacos-group/nacos-sdk-go/v2/clients/config_client" + "github.com/nacos-group/nacos-sdk-go/v2/vo" ) type ConfigOption func(*configOptions) @@ -40,7 +42,7 @@ type Config struct { client config_client.IConfigClient } -func NewConfigSource(client config_client.IConfigClient, opts ...ConfigOption) kconfig.Source { +func NewConfigSource(client config_client.IConfigClient, opts ...ConfigOption) config.Source { _options := configOptions{} for _, o := range opts { o(&_options) @@ -48,7 +50,7 @@ func NewConfigSource(client config_client.IConfigClient, opts ...ConfigOption) k return &Config{client: client, opts: _options} } -func (c *Config) Load() ([]*kconfig.KeyValue, error) { +func (c *Config) Load() ([]*config.KeyValue, error) { content, err := c.client.GetConfig(vo.ConfigParam{ DataId: c.opts.dataID, Group: c.opts.group, @@ -58,7 +60,7 @@ func (c *Config) Load() ([]*kconfig.KeyValue, error) { return nil, err } k := c.opts.dataID - return []*kconfig.KeyValue{ + return []*config.KeyValue{ { Key: k, Value: []byte(content), @@ -67,7 +69,7 @@ func (c *Config) Load() ([]*kconfig.KeyValue, error) { }, nil } -func (c *Config) Watch() (kconfig.Watcher, error) { +func (c *Config) Watch() (config.Watcher, error) { watcher := newWatcher(context.Background(), c.opts.dataID, c.opts.group, c.client.CancelListenConfig) err := c.client.ListenConfig(vo.ConfigParam{ DataId: c.opts.dataID, diff --git a/contrib/nacos/config_test.go b/contrib/nacos/config_test.go index a383dccc598..c2186fecdf1 100644 --- a/contrib/nacos/config_test.go +++ b/contrib/nacos/config_test.go @@ -1,36 +1,48 @@ package nacos import ( - "github.com/nacos-group/nacos-sdk-go/v2/clients" - "github.com/nacos-group/nacos-sdk-go/v2/clients/config_client" - "github.com/nacos-group/nacos-sdk-go/v2/common/constant" - "github.com/nacos-group/nacos-sdk-go/v2/vo" "os" "path/filepath" "testing" "time" + + "github.com/nacos-group/nacos-sdk-go/v2/clients" + "github.com/nacos-group/nacos-sdk-go/v2/clients/config_client" + "github.com/nacos-group/nacos-sdk-go/v2/common/constant" + "github.com/nacos-group/nacos-sdk-go/v2/vo" ) var ( - DATA_ID = "nacos-test-config.json" - GROUP = "DEFAULT_GROUP" + DataID = "nacos-test-config.json" + Group = "DEFAULT_GROUP" ) func newConfigClient() (config_client.IConfigClient, error) { userHomeDir, _ := os.UserHomeDir() + // 设置默认值 + serverAddr := os.Getenv("NACOS_SERVER_ADDRESS") + if serverAddr == "" { + serverAddr = "127.0.0.1" // 设置默认地址 + } + + username := os.Getenv("NACOS_USER_NAME") + password := os.Getenv("NACOS_USER_PASSWORD") + clientConfig := constant.NewClientConfig( constant.WithLogDir(filepath.Join(userHomeDir, "logs", "nacos")), constant.WithCacheDir(filepath.Join(userHomeDir, "nacos", "cache")), - constant.WithUsername(os.Getenv("NACOS_USER_NAME")), - constant.WithPassword(os.Getenv("NACOS_USER_PASSWORD")), + constant.WithUsername(username), + constant.WithPassword(password), constant.WithLogLevel("info"), ) + serverConfigs := []constant.ServerConfig{ { - IpAddr: os.Getenv("NACOS_SERVER_ADDRESS"), + IpAddr: serverAddr, ContextPath: "/nacos", Port: 8848, + Scheme: "http", // 添加scheme }, } @@ -41,23 +53,28 @@ func newConfigClient() (config_client.IConfigClient, error) { } func TestConfig_Get_AND_WATCH(t *testing.T) { + // 检查必要的环境变量 + if os.Getenv("NACOS_SERVER_ADDRESS") == "" { + t.Skip("NACOS_SERVER_ADDRESS environment variable not set") + } + client, err := newConfigClient() if err != nil { t.Fatal(err) } _, err = client.PublishConfig(vo.ConfigParam{ - DataId: DATA_ID, - Group: GROUP, - Content: "hello world"}) - + DataId: DataID, + Group: Group, + Content: "hello world", + }) if err != nil { t.Fatal(err) } time.Sleep(1 * time.Second) - source := NewConfigSource(client, WithConfigGroup(GROUP), WithDataID(DATA_ID)) + source := NewConfigSource(client, WithConfigGroup(Group), WithDataID(DataID)) kvs, err := source.Load() if err != nil { @@ -73,10 +90,10 @@ func TestConfig_Get_AND_WATCH(t *testing.T) { } _, err = client.PublishConfig(vo.ConfigParam{ - DataId: DATA_ID, - Group: GROUP, - Content: "hello world2"}) - + DataId: DataID, + Group: Group, + Content: "hello world2", + }) if err != nil { t.Fatal(err) } diff --git a/contrib/nacos/go.mod b/contrib/nacos/go.mod index 93b4019c552..d31f24f3be8 100644 --- a/contrib/nacos/go.mod +++ b/contrib/nacos/go.mod @@ -1,11 +1,9 @@ module github.com/go-kratos/kratos/contrib/nacos/v2 -go 1.21 - -toolchain go1.23.2 +go 1.19 require ( - github.com/go-kratos/kratos/v2 v2.8.2 + github.com/go-kratos/kratos/v2 v2.8.3 github.com/nacos-group/nacos-sdk-go/v2 v2.2.7 ) @@ -48,3 +46,5 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/go-kratos/kratos/v2 => ../../ diff --git a/contrib/nacos/go.sum b/contrib/nacos/go.sum index a6b0e251936..499d9f89e07 100644 --- a/contrib/nacos/go.sum +++ b/contrib/nacos/go.sum @@ -33,6 +33,7 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -53,6 +54,7 @@ github.com/aliyun/alibabacloud-dkms-gcs-go-sdk v0.2.2 h1:rWkH6D2XlXb/Y+tNAQROxBz github.com/aliyun/alibabacloud-dkms-gcs-go-sdk v0.2.2/go.mod h1:GDtq+Kw+v0fO+j5BrrWiUHbBq7L+hfpzpPfXKOZMFE0= github.com/aliyun/alibabacloud-dkms-transfer-go-sdk v0.1.7 h1:olLiPI2iM8Hqq6vKnSxpM3awCrm9/BeOgHpzQkOYnI4= github.com/aliyun/alibabacloud-dkms-transfer-go-sdk v0.1.7/go.mod h1:oDg1j4kFxnhgftaiLJABkGeSvuEvSF5Lo6UmRAMruX4= +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= @@ -71,6 +73,7 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -82,8 +85,6 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2 github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= -github.com/go-kratos/kratos/v2 v2.8.2 h1:EsEA7AmPQ2YQQ0FZrDWO2HgBNqeWM8z/mWKzS5UkQaQ= -github.com/go-kratos/kratos/v2 v2.8.2/go.mod h1:+Vfe3FzF0d+BfMdajA11jT0rAyJWublRE/seZQNZVxE= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= @@ -132,6 +133,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -166,7 +168,9 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -185,6 +189,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -209,6 +214,7 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= @@ -219,6 +225,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -230,6 +237,7 @@ go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= @@ -476,7 +484,6 @@ google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7Fc google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 h1:YJ5pD9rF8o9Qtta0Cmy9rdBwkSjrTCT6XTiUQVOtIos= google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 h1:6G8oQ016D88m1xAKljMlBOOGWDZkes4kMhgGFlf8WcQ= google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917/go.mod h1:xtjpI3tXFPP051KaWnhvxkiubL/6dJ18vLVf7q2pTOU= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= @@ -511,6 +518,7 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= @@ -522,6 +530,7 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/contrib/nacos/registry_test.go b/contrib/nacos/registry_test.go index ad7cebea30a..c9db807f842 100644 --- a/contrib/nacos/registry_test.go +++ b/contrib/nacos/registry_test.go @@ -2,37 +2,49 @@ package nacos import ( "context" - "github.com/go-kratos/kratos/v2/registry" - "github.com/nacos-group/nacos-sdk-go/v2/clients" - "github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client" - "github.com/nacos-group/nacos-sdk-go/v2/common/constant" - "github.com/nacos-group/nacos-sdk-go/v2/vo" "os" "path/filepath" "testing" "time" + + "github.com/nacos-group/nacos-sdk-go/v2/clients" + "github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client" + "github.com/nacos-group/nacos-sdk-go/v2/common/constant" + "github.com/nacos-group/nacos-sdk-go/v2/vo" + + "github.com/go-kratos/kratos/v2/registry" ) var ( - REGISTRY_GROUP = "DEFAULT_GROUP" - REGISTRY_CLUSTER = "DEFAULT" + RegistryGroup = "DEFAULT_GROUP" + RegistryCluster = "DEFAULT" ) func newNamingClient() (naming_client.INamingClient, error) { userHomeDir, _ := os.UserHomeDir() + serverAddr := os.Getenv("NACOS_SERVER_ADDRESS") + if serverAddr == "" { + serverAddr = "127.0.0.1" + } + + username := os.Getenv("NACOS_USER_NAME") + password := os.Getenv("NACOS_USER_PASSWORD") + clientConfig := constant.NewClientConfig( constant.WithLogDir(filepath.Join(userHomeDir, "logs", "nacos")), constant.WithCacheDir(filepath.Join(userHomeDir, "nacos", "cache")), - constant.WithUsername(os.Getenv("NACOS_USER_NAME")), - constant.WithPassword(os.Getenv("NACOS_USER_PASSWORD")), + constant.WithUsername(username), + constant.WithPassword(password), constant.WithLogLevel("info"), ) + serverConfigs := []constant.ServerConfig{ { - IpAddr: os.Getenv("NACOS_SERVER_ADDRESS"), + IpAddr: serverAddr, ContextPath: "/nacos", Port: 8848, + Scheme: "http", }, } @@ -43,6 +55,10 @@ func newNamingClient() (naming_client.INamingClient, error) { } func TestRegistry(t *testing.T) { + if os.Getenv("NACOS_SERVER_ADDRESS") == "" { + t.Skip("NACOS_SERVER_ADDRESS environment variable not set") + } + client, err := newNamingClient() if err != nil { t.Fatal(err) @@ -50,8 +66,8 @@ func TestRegistry(t *testing.T) { nacosRegistry := NewRegistry( client, - WithRegistryGroup(REGISTRY_GROUP), - WithCluster(REGISTRY_CLUSTER), + WithRegistryGroup(RegistryGroup), + WithCluster(RegistryCluster), WithWeight(1.0)) mm := map[string]string{ @@ -68,7 +84,9 @@ func TestRegistry(t *testing.T) { } err = nacosRegistry.Register(context.Background(), ins) - + if err != nil { + t.Fatal(err) + } nacosWatcher, err := nacosRegistry.Watch(context.Background(), "test-ut") if err != nil { t.Fatal(err) @@ -104,7 +122,10 @@ func TestRegistry(t *testing.T) { Metadata: mm, } - nacosRegistry.Register(context.Background(), ins) + err = nacosRegistry.Register(context.Background(), ins) + if err != nil { + return + } instances, err := nacosWatcher.Next() for _, instance := range instances { diff --git a/contrib/nacos/subscriber.go b/contrib/nacos/subscriber.go index 018d75b9e19..137d33a3b21 100644 --- a/contrib/nacos/subscriber.go +++ b/contrib/nacos/subscriber.go @@ -3,6 +3,7 @@ package nacos import ( "context" "fmt" + "github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client" "github.com/nacos-group/nacos-sdk-go/v2/model" "github.com/nacos-group/nacos-sdk-go/v2/vo" @@ -39,7 +40,7 @@ func newSubscriber(ctx context.Context, cli naming_client.INamingClient, service ServiceName: serviceName, Clusters: clusters, GroupName: groupName, - SubscribeCallback: func(instances []model.Instance, err error) { + SubscribeCallback: func(instances []model.Instance, _ error) { w.subscribeChan <- instances }, } diff --git a/contrib/registry/servicecomb/registry_test.go b/contrib/registry/servicecomb/registry_test.go index 9e13949251f..82712cd3f29 100644 --- a/contrib/registry/servicecomb/registry_test.go +++ b/contrib/registry/servicecomb/registry_test.go @@ -23,7 +23,7 @@ func (receiver *mockClient) WatchMicroService(_ string, _ func(*sc.MicroServiceI return nil } -//nolint +// nolint func (receiver *mockClient) FindMicroServiceInstances(_, _, microServiceName, _ string, _ ...sc.CallOption, ) ([]*pb.MicroServiceInstance, error) { diff --git a/internal/group/group.go b/internal/group/group.go index c97496d14d9..2b7eb2c7302 100644 --- a/internal/group/group.go +++ b/internal/group/group.go @@ -13,12 +13,12 @@ type Group struct { } // NewGroup news a group container. -func NewGroup(new func() interface{}) *Group { - if new == nil { +func NewGroup(factory func() interface{}) *Group { + if factory == nil { panic("container.group: can't assign a nil to the new function") } return &Group{ - new: new, + new: factory, vals: make(map[string]interface{}), } } @@ -46,12 +46,12 @@ func (g *Group) Get(key string) interface{} { } // Reset resets the new function and deletes all existing objects. -func (g *Group) Reset(new func() interface{}) { - if new == nil { +func (g *Group) Reset(factory func() interface{}) { + if factory == nil { panic("container.group: can't assign a nil to the new function") } g.Lock() - g.new = new + g.new = factory g.Unlock() g.Clear() }