nacos

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-nacos

Testable Nacos configuration client: download remote YAML, watch changes, and write local config files with MD5 verification.

Install

go get github.com/andywang2011/go-nacos@v0.1.0

Quick start

if nacos.ShouldSkip("") {
	return
}
cfg, err := nacos.LoadConfigFromEnvAndMeta("./config/nacos.yaml", nacos.NamespaceFromEnv())
if err != nil {
	// handle
}
client, err := nacos.NewClient(cfg)
if err != nil {
	// handle
}
content, err := client.Download(context.Background())
_ = content

Examples

cd go-nacos/examples/watch && go run .

License

MIT — see LICENSE.

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

View Source
const (
	DefaultPollTime = 30 * time.Second
	DefaultTimeout  = 15 * time.Second
)
View Source
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

View Source
var ErrClosed = errors.New("nacos: client is closed")

ErrClosed is returned when a closed Client is used to start a watch.

Functions

func NamespaceFromEnv

func NamespaceFromEnv() string

NamespaceFromEnv returns EnvNamespace.

func ShouldSkip

func ShouldSkip(skipFlag string) bool

ShouldSkip reports whether Nacos bootstrap should be skipped.

func WriteConfigFile

func WriteConfigFile(path string, content []byte) error

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

func NewClient(cfg Config) (*Client, error)

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
}

func NewClientWithAPI

func NewClientWithAPI(cfg Config, api configAPI) (*Client, error)

NewClientWithAPI injects the config API for tests.

func (*Client) Close

func (c *Client) Close() error

Close cancels every active watch via CancelListenConfig. It is safe to call more than once; later Watch calls return ErrClosed.

func (*Client) Download

func (c *Client) Download(ctx context.Context) (string, error)

Download fetches remote config with one retry.

func (*Client) Watch

func (c *Client) Watch(ctx context.Context, onChange func(content []byte) error) error

Watch registers a change listener. onChange receives raw config bytes. The listener is cancelled (CancelListenConfig) when ctx is done or Close is called; onChange is not invoked after cancellation.

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

func LoadConfigFromEnvAndMeta(metaPath, namespace string) (Config, error)

LoadConfigFromEnvAndMeta builds Config from NACOS_* env vars and a meta YAML file.

func (Config) String

func (c Config) String() string

func (*Config) Validate

func (c *Config) Validate() error

Validate checks required fields and applies defaults.

type Meta

type Meta struct {
	DataID   string
	Group    string
	PollTime time.Duration
	Timeout  time.Duration
}

Meta holds data_id/group resolved from a local nacos meta YAML file.

func LoadMetaFromYAML

func LoadMetaFromYAML(path, namespace string) (Meta, error)

LoadMetaFromYAML reads namespace-specific keys from a nacos meta file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL