Documentation
¶
Overview ¶
Package nacos provides a testable Nacos config client for downloading and watching remote YAML.
Design constraints:
- Configuration via Config struct (no viper, no log.Fatal inside the library).
- NewClientWithAPI injects the config API for unit tests.
- NACOS_* env vars are owned by this package; use LoadConfigFromEnvAndMeta, NamespaceFromEnv, and ShouldSkip rather than reading those keys elsewhere.
Index ¶
Examples ¶
Constants ¶
const ( DefaultPollTime = 30 * time.Second DefaultTimeout = 15 * time.Second )
const ( EnvNamespace = "NACOS_NAMESPACE" EnvServer = "NACOS_SERVER" EnvPort = "NACOS_PORT" EnvUsername = "NACOS_USERNAME" EnvPassword = "NACOS_PASSWORD" EnvSkip = "NACOS_SKIP" )
Deployment environment variable names shared by all services using go-nacos. Callers must not read these keys directly; use LoadConfigFromEnvAndMeta, NamespaceFromEnv, and ShouldSkip instead.
Variables ¶
var ErrClosed = errors.New("nacos: client is closed")
ErrClosed is returned when a closed Client is used to start a watch.
Functions ¶
func ShouldSkip ¶
ShouldSkip reports whether Nacos bootstrap should be skipped.
func WriteConfigFile ¶
WriteConfigFile atomically writes content to path and verifies MD5 after read-back.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client downloads and watches Nacos configuration content.
func NewClient ¶
NewClient creates a Nacos config client.
Example ¶
package main
import (
"context"
"github.com/andywang2011/go-nacos"
)
func main() {
if nacos.ShouldSkip("") {
return
}
cfg, err := nacos.LoadConfigFromEnvAndMeta("./config/nacos.yaml", nacos.NamespaceFromEnv())
if err != nil {
return
}
client, err := nacos.NewClient(cfg)
if err != nil {
return
}
content, err := client.Download(context.Background())
if err != nil {
return
}
_ = content
}
Output:
func NewClientWithAPI ¶
NewClientWithAPI injects the config API for tests.
func (*Client) Close ¶
Close cancels every active watch via CancelListenConfig. It is safe to call more than once; later Watch calls return ErrClosed.
type Config ¶
type Config struct {
Address string
Port uint64
Username string
Password string
NamespaceID string
DataID string
Group string
PollTime time.Duration
Timeout time.Duration
Scheme string
}
Config holds Nacos connection and target config item settings.
func ConfigDefault ¶
func ConfigDefault() Config
ConfigDefault is an alias for DefaultConfig (naming consistency across go-* libs).
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns library defaults for optional timing fields.
func LoadConfigFromEnvAndMeta ¶
LoadConfigFromEnvAndMeta builds Config from NACOS_* env vars and a meta YAML file.
type Meta ¶
Meta holds data_id/group resolved from a local nacos meta YAML file.
func LoadMetaFromYAML ¶
LoadMetaFromYAML reads namespace-specific keys from a nacos meta file.