diff --git a/README.md b/README.md index 95ff53d..cc4a62d 100644 --- a/README.md +++ b/README.md @@ -127,9 +127,9 @@ Provide the mechanism to custom the nacos parameter `vo.ConfigParam`. | Variable Name | Default Value | Introduction | | ------------------------- | ---------------------------------- | --------------------------------- | -| Address | 127.0.0.1 | Nacos server address | -| Port | 8848 | Nacos server port | -| NamespaceID | | The namespaceID of Nacos 中的 namespace Id | +| Address | 127.0.0.1 | Nacos server address, may use the environment of `serverAddr` | +| Port | 8848 | Nacos server port, may use the environment of `serverPort` | +| NamespaceID | | The namespaceID of Nacos, may use the environment of `namespace` | | ClientDataIDFormat | {{.ClientServiceName}}.{{.ServerServiceName}}.{{.Category}} | Use go [template](https://pkg.go.dev/text/template) syntax rendering to generate the appropriate ID, and use `ClientServiceName` `ServiceName` `Category` three metadata that can be customised | | ServerDataIDFormat | {{.ServerServiceName}}.{{.Category}} | Use go [template](https://pkg.go.dev/text/template) syntax rendering to generate the appropriate ID, and use `ServiceName` `Category` two metadatas that can be customised | | Group | DEFAULT_GROUP | Use fixed values or dynamic rendering. Usage is the same as configDataId. | diff --git a/README_CN.md b/README_CN.md index 1718160..0f34f38 100644 --- a/README_CN.md +++ b/README_CN.md @@ -125,9 +125,9 @@ func main() { | 参数 | 变量默认值 | 作用 | | ------------------------- | ---------------------------------- | --------------------------------- | -| Address | 127.0.0.1 | nacos 服务器地址 | -| Port | 8848 | nacos 服务器端口 | -| NamespaceID | | nacos 中的 namespace Id | +| Address | 127.0.0.1 | nacos 服务器地址, 如果参数为空使用 serverAddr 环境变量值 | +| Port | 8848 | nacos 服务器端口, 如果参数为空使用 serverPort 环境变量值 | +| NamespaceID | | nacos 中的 namespace Id, 如果参数为空使用 namespace 环境变量值 | | ClientDataIDFormat | {{.ClientServiceName}}.{{.ServerServiceName}}.{{.Category}} | 使用 go [template](https://pkg.go.dev/text/template) 语法渲染生成对应的 ID, 使用 `ClientServiceName` `ServiceName` `Category` 三个元数据 | | ServerDataIDFormat | {{.ServerServiceName}}.{{.Category}} | 使用 go [template](https://pkg.go.dev/text/template) 语法渲染生成对应的 ID, 使用 `ServiceName` `Category` 两个元数据 | | Group | DEFAULT_GROUP | 使用固定值,也可以动态渲染,用法同 DataIDFormat | diff --git a/nacos/env.go b/nacos/env.go new file mode 100644 index 0000000..13ffa71 --- /dev/null +++ b/nacos/env.go @@ -0,0 +1,57 @@ +// Copyright 2023 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 nacos + +import ( + "os" + "strconv" + + "github.com/cloudwego/kitex/pkg/klog" +) + +// keep consistent with the env with alicloud +const ( + NacosAliServerAddrEnv = "serverAddr" + NacosAliPortEnv = "serverPort" + NacosAliNamespaceEnv = "namespace" +) + +// NacosPort Get Nacos port from environment variables +func NacosPort() uint64 { + portText := os.Getenv(NacosAliPortEnv) + if len(portText) == 0 { + return NacosDefaultPort + } + port, err := strconv.ParseInt(portText, 10, 64) + if err != nil { + klog.Errorf("ParseInt failed,err:%s", err.Error()) + return NacosDefaultPort + } + return uint64(port) +} + +// NacosAddr Get Nacos addr from environment variables +func NacosAddr() string { + addr := os.Getenv(NacosAliServerAddrEnv) + if len(addr) == 0 { + return NacosDefaultServerAddr + } + return addr +} + +// NacosNameSpaceId Get Nacos namespace id from environment variables +func NacosNameSpaceId() string { + return os.Getenv(NacosDefaultConfigGroup) +} diff --git a/nacos/nacos.go b/nacos/nacos.go index 44d4941..a8d53cb 100644 --- a/nacos/nacos.go +++ b/nacos/nacos.go @@ -83,10 +83,16 @@ type Options struct { // NewClient Create a default Nacos client func NewClient(opts Options) (Client, error) { if opts.Address == "" { - opts.Address = NacosDefaultServerAddr + opts.Address = NacosAddr() } if opts.Port == 0 { - opts.Port = NacosDefaultPort + opts.Port = NacosPort() + } + if opts.NamespaceID == "" { + opts.NamespaceID = NacosNameSpaceId() + } + if opts.Group == "" { + opts.Group = NacosDefaultConfigGroup } if opts.CustomLogger == nil { opts.CustomLogger = NewCustomNacosLogger() @@ -94,9 +100,6 @@ func NewClient(opts Options) (Client, error) { if opts.ConfigParser == nil { opts.ConfigParser = defaultConfigParse() } - if opts.Group == "" { - opts.Group = NacosDefaultConfigGroup - } if opts.ServerDataIDFormat == "" { opts.ServerDataIDFormat = NacosDefaultServerDataID }