Skip to content

Commit

Permalink
feat/env: support get the parameter from environment (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
whalecold committed Dec 3, 2023
1 parent 0c5cb17 commit 8375cb2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
6 changes: 3 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
57 changes: 57 additions & 0 deletions nacos/env.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 8 additions & 5 deletions nacos/nacos.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,23 @@ 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()
}
if opts.ConfigParser == nil {
opts.ConfigParser = defaultConfigParse()
}
if opts.Group == "" {
opts.Group = NacosDefaultConfigGroup
}
if opts.ServerDataIDFormat == "" {
opts.ServerDataIDFormat = NacosDefaultServerDataID
}
Expand Down

0 comments on commit 8375cb2

Please sign in to comment.