diff --git a/consul/utils.go b/consul/utils.go index 07ab172..5029c04 100644 --- a/consul/utils.go +++ b/consul/utils.go @@ -42,7 +42,6 @@ func parseAddr(addr net.Addr) (host string, port int, err error) { } host, _, err = net.SplitHostPort(detectHost) - if err != nil { return "", 0, fmt.Errorf("empty host") } diff --git a/nacos/v2/README.md b/nacos/v2/README.md index dbc8c43..2320f32 100644 --- a/nacos/v2/README.md +++ b/nacos/v2/README.md @@ -6,6 +6,12 @@ Nacos as service discovery for Hertz. ## How to use? +### +- The nacos/v2 version of hertz does not currently support creating multiple port examples in the same group multiple times. +- Service registration and discovery in nacos/v2 is compatible with previous versions. +- `CustomLogger` type in constant.ClientConfig has been removed in nacos-sdk-go v2. Instead, use the `(github.com/nacos-group/nacos-sdk-go/v2/common/logger).SetLogger` to customize the log. +- nacos/v2 only supports nacos 2.X version. + ### Server **[example/standard/server/main.go](examples/standard/server/main.go)** @@ -78,6 +84,28 @@ client, err := client.NewClient() ``` +### Custom Logger + +**[examples/logger/main.go](examples/logger/main.go)** + +```go +package main + +import ( + "github.com/hertz-contrib/registry/nacos/v2/common" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +func main() { + logger.InitLogger(logger.Config{ + Level: "debug", + }) + logger.SetLogger(common.NewCustomNacosLogger()) + logger.Info("info") +} + +``` + ## How to run example? ### run docker diff --git a/nacos/v2/README_CN.md b/nacos/v2/README_CN.md index f7cfd23..d689987 100644 --- a/nacos/v2/README_CN.md +++ b/nacos/v2/README_CN.md @@ -6,6 +6,13 @@ ## 这个项目应当如何使用? +### 注意 + +- nacos/v2 版本中 hertz 目前不支持多次在同分组下创建多端口示例 +- nacos/v2 的服务注册与发现和先前的版本兼容 +- nacos-sdk-go v2 版本中 constant.ClientConfig 中 CustomLogger 类型被移除, 转而使用 (github.com/nacos-group/nacos-sdk-go/v2/common/logger).SetLogger 方法进行日志自定义 +- nacos/v2 只支持 nacos 2.X 版本 + ### 服务端 **[example/server/main.go](examples/standard/server/main.go)** @@ -76,6 +83,28 @@ func main() { } ``` +### 自定义日志 + +**[examples/logger/main.go](examples/logger/main.go)** + +```go +package main + +import ( + "github.com/hertz-contrib/registry/nacos/v2/common" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +func main() { + logger.InitLogger(logger.Config{ + Level: "debug", + }) + logger.SetLogger(common.NewCustomNacosLogger()) + logger.Info("info") +} + +``` + ## 如何运行示例 ? ### docker 运行 nacos-server diff --git a/nacos/v2/common/logger.go b/nacos/v2/common/logger.go new file mode 100644 index 0000000..6bb3721 --- /dev/null +++ b/nacos/v2/common/logger.go @@ -0,0 +1,58 @@ +// Copyright 2021 CloudWeGo Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package common + +import ( + "github.com/cloudwego/hertz/pkg/common/hlog" + v2 "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +type customNacosLogger struct{} + +func NewCustomNacosLogger() v2.Logger { + return customNacosLogger{} +} + +func (c customNacosLogger) Info(args ...interface{}) { + hlog.Info(args) +} + +func (c customNacosLogger) Warn(args ...interface{}) { + hlog.Warn(args) +} + +func (c customNacosLogger) Error(args ...interface{}) { + hlog.Error(args) +} + +func (c customNacosLogger) Debug(args ...interface{}) { + hlog.Debug(args) +} + +func (c customNacosLogger) Infof(fmt string, args ...interface{}) { + hlog.Infof(fmt, args) +} + +func (c customNacosLogger) Warnf(fmt string, args ...interface{}) { + hlog.Warnf(fmt, args) +} + +func (c customNacosLogger) Errorf(fmt string, args ...interface{}) { + hlog.Errorf(fmt, args) +} + +func (c customNacosLogger) Debugf(fmt string, args ...interface{}) { + hlog.Debug(fmt, args) +} diff --git a/nacos/v2/examples/logger/main.go b/nacos/v2/examples/logger/main.go new file mode 100644 index 0000000..363aa9d --- /dev/null +++ b/nacos/v2/examples/logger/main.go @@ -0,0 +1,28 @@ +// Copyright 2021 CloudWeGo Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "github.com/hertz-contrib/registry/nacos/v2/common" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" +) + +func main() { + logger.InitLogger(logger.Config{ + Level: "debug", + }) + logger.SetLogger(common.NewCustomNacosLogger()) + logger.Info("info") +}