github.com/nacos-group/nacos-sdk-go@v1.1.4/clients/nacos_client/nacos_client.go (about)

     1  /*
     2   * Copyright 1999-2020 Alibaba Group Holding Ltd.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package nacos_client
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"strconv"
    23  	"time"
    24  
    25  	"github.com/nacos-group/nacos-sdk-go/common/constant"
    26  	"github.com/nacos-group/nacos-sdk-go/common/file"
    27  	"github.com/nacos-group/nacos-sdk-go/common/http_agent"
    28  )
    29  
    30  type NacosClient struct {
    31  	clientConfigValid  bool
    32  	serverConfigsValid bool
    33  	agent              http_agent.IHttpAgent
    34  	clientConfig       constant.ClientConfig
    35  	serverConfigs      []constant.ServerConfig
    36  }
    37  
    38  //SetClientConfig is use to set nacos client Config
    39  func (client *NacosClient) SetClientConfig(config constant.ClientConfig) (err error) {
    40  	if config.TimeoutMs <= 0 {
    41  		config.TimeoutMs = 10 * 1000
    42  	}
    43  
    44  	if config.BeatInterval <= 0 {
    45  		config.BeatInterval = 5 * 1000
    46  	}
    47  
    48  	if config.UpdateThreadNum <= 0 {
    49  		config.UpdateThreadNum = 20
    50  	}
    51  
    52  	if len(config.LogLevel) == 0 {
    53  		config.LogLevel = "info"
    54  	}
    55  
    56  	if config.CacheDir == "" {
    57  		config.CacheDir = file.GetCurrentPath() + string(os.PathSeparator) + "cache"
    58  	}
    59  
    60  	if config.LogDir == "" {
    61  		config.LogDir = file.GetCurrentPath() + string(os.PathSeparator) + "log"
    62  	}
    63  
    64  	if config.LogSampling != nil {
    65  		if config.LogSampling.Initial < 0 {
    66  			config.LogSampling.Initial = 100
    67  		}
    68  
    69  		if config.LogSampling.Thereafter < 0 {
    70  			config.LogSampling.Thereafter = 100
    71  		}
    72  
    73  		if config.LogSampling.Tick < 0 {
    74  			config.LogSampling.Tick = 10 * time.Second
    75  		}
    76  	}
    77  
    78  	client.clientConfig = config
    79  	client.clientConfigValid = true
    80  
    81  	return
    82  }
    83  
    84  //SetServerConfig is use to set nacos server config
    85  func (client *NacosClient) SetServerConfig(configs []constant.ServerConfig) (err error) {
    86  	if len(configs) <= 0 {
    87  		//it's may be use endpoint to get nacos server address
    88  		client.serverConfigsValid = true
    89  		return
    90  	}
    91  
    92  	for i := 0; i < len(configs); i++ {
    93  		if len(configs[i].IpAddr) <= 0 || configs[i].Port <= 0 || configs[i].Port > 65535 {
    94  			err = errors.New("[client.SetServerConfig] configs[" + strconv.Itoa(i) + "] is invalid")
    95  			return
    96  		}
    97  		if len(configs[i].ContextPath) <= 0 {
    98  			configs[i].ContextPath = constant.DEFAULT_CONTEXT_PATH
    99  		}
   100  		if len(configs[i].Scheme) <= 0 {
   101  			configs[i].Scheme = constant.DEFAULT_SERVER_SCHEME
   102  		}
   103  	}
   104  	client.serverConfigs = configs
   105  	client.serverConfigsValid = true
   106  	return
   107  }
   108  
   109  //GetClientConfig use to get client config
   110  func (client *NacosClient) GetClientConfig() (config constant.ClientConfig, err error) {
   111  	config = client.clientConfig
   112  	if !client.clientConfigValid {
   113  		err = errors.New("[client.GetClientConfig] invalid client config")
   114  	}
   115  	return
   116  }
   117  
   118  //GetServerConfig use to get server config
   119  func (client *NacosClient) GetServerConfig() (configs []constant.ServerConfig, err error) {
   120  	configs = client.serverConfigs
   121  	if !client.serverConfigsValid {
   122  		err = errors.New("[client.GetServerConfig] invalid server configs")
   123  	}
   124  	return
   125  }
   126  
   127  //SetHttpAgent use to set http agent
   128  func (client *NacosClient) SetHttpAgent(agent http_agent.IHttpAgent) (err error) {
   129  	if agent == nil {
   130  		err = errors.New("[client.SetHttpAgent] http agent can not be nil")
   131  	} else {
   132  		client.agent = agent
   133  	}
   134  	return
   135  }
   136  
   137  //GetHttpAgent use to get http agent
   138  func (client *NacosClient) GetHttpAgent() (agent http_agent.IHttpAgent, err error) {
   139  	if client.agent == nil {
   140  		err = errors.New("[client.GetHttpAgent] invalid http agent")
   141  	} else {
   142  		agent = client.agent
   143  	}
   144  	return
   145  }