nacos

package module
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 9 Imported by: 1

README

nacos

Package nacos implements GoFrame gcfg.Adapter using nacos service.

Installation

go get -u github.com/gogf/gf/contrib/config/nacos/v2

Usage

Create a custom boot package

If you wish using configuration from nacos globally, it is strongly recommended creating a custom boot package in very top import, which sets the Adapter of default configuration instance before any other package boots.

package boot

import (
	"github.com/gogf/gf/contrib/config/nacos/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
)

func init() {
	var (
		ctx          = gctx.GetInitCtx()
		serverConfig = constant.ServerConfig{
			IpAddr: "localhost",
			Port:   8848,
		}
		clientConfig = constant.ClientConfig{
			CacheDir: "/tmp/nacos",
			LogDir:   "/tmp/nacos",
		}
		configParam = vo.ConfigParam{
			DataId: "config.toml",
			Group:  "test",
		}
	)
	// Create anacosClient that implements gcfg.Adapter.
	adapter, err := nacos.New(ctx, nacos.Config{
		ServerConfigs: []constant.ServerConfig{serverConfig},
		ClientConfig:  clientConfig,
		ConfigParam:   configParam,
	})
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}
	// Change the adapter of default configuration instance.
	g.Cfg().SetAdapter(adapter)
}

Import boot package in top of main

It is strongly recommended import your boot package in top of your main.go.

Note the top import: _ "github.com/gogf/gf/example/config/nacos/boot" .

package main

import (
    _ "github.com/gogf/gf/example/config/nacos/boot"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
)

func main() {
    var ctx = gctx.GetInitCtx()

    // Available checks.
    g.Dump(g.Cfg().Available(ctx))

    // All key-value configurations.
    g.Dump(g.Cfg().Data(ctx))

    // Retrieve certain value by key.
    g.Dump(g.Cfg().MustGet(ctx, "server.address"))
}

License

GoFrame nacos is licensed under the MIT License, 100% free and open-source, forever.

Documentation

Overview

Package nacos implements gcfg.Adapter using nacos service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, config Config) (adapter gcfg.Adapter, err error)

New creates and returns gcfg.Adapter implementing using nacos service.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements gcfg.Adapter implementing using nacos service.

func (*Client) Available

func (c *Client) Available(ctx context.Context, resource ...string) (ok bool)

Available checks and returns the backend configuration service is available. The optional parameter `resource` specifies certain configuration resource.

Note that this function does not return error as it just does simply check for backend configuration service.

func (*Client) Data

func (c *Client) Data(ctx context.Context) (data map[string]interface{}, err error)

Data retrieves and returns all configuration data in current resource as map. Note that this function may lead lots of memory usage if configuration data is too large, you can implement this function if necessary.

func (*Client) Get

func (c *Client) Get(ctx context.Context, pattern string) (value interface{}, err error)

Get retrieves and returns value by specified `pattern` in current resource. Pattern like: "x.y.z" for map item. "x.0.y" for slice item.

type Config

type Config struct {
	ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig
	ClientConfig  constant.ClientConfig   `v:"required"` // See constant.ClientConfig
	ConfigParam   vo.ConfigParam          `v:"required"` // See vo.ConfigParam
	Watch         bool                    // Watch watches remote configuration updates, which updates local configuration in memory immediately when remote configuration changes.
}

Config is the configuration object for nacos client.

Jump to

Keyboard shortcuts

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