sail

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 26 Imported by: 0

README

sail-client-go

  1. SDK 集成

类似 Nacos、Apollo 都是这种方式,提供一个语言特定的 sdk,集成 sdk 即可。

  1. sidecar 外挂

类似 confd 是这种方式,外挂一个进程,专门负责输出和更新配置文件,程序还是读取配置,侵入性小。

这两种方式,sail 的 client 都是支持的,请看下图:

原理

由于 sail-client 会把从 etcd 中读取的配置备份到文件,并保持文件内容的更新,所以,从使用者的角度来说, 既可以把 sail-client 集成到代码中,也可以单独运行 sail-client,然后去读取备份的配置文件。

安装

代码接入:

go get github.com/HYY-yu/sail-client

单独运行:

git clone https://github.com/HYY-yu/sail-client.git
# 修改 cfg.toml 为你的配置
vim cfg.toml
go build -o service ./cmd/main.go
./service

# 打包成 docker 镜像
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o service ./cmd/main.go
docker build --build-arg=serviceName=sail-client -f ./deploy/Dockerfile .

Documentation

Index

Constants

View Source
const MergeConfigName = "config.toml"

Variables

View Source
var (
	ErrDuplicateKey = errors.New("ErrDuplicateKey")
)

Functions

This section is empty.

Types

type FileMaintainer

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

FileMaintainer 2. 如果设置了ConfigPath,检查Path里有没有配置文件(如果设置了mergeConfig还要merge),有则更新、新增、删除等。(研究是否可以利用Viper) 1. pullETCDConfig后,把viper内的配置全部写成文件。 2. 如果设置mergeConfig,则mergeViper后,再写成文件。 3. 有watch事件,把对应viper的配置重新写成文件。 4. 有mergeConfig,重新mergeViper,覆盖写。 文件默认不加密

func NewFileMaintainer

func NewFileMaintainer(sail *Sail) *FileMaintainer

type GetError

type GetError struct {
	Err   error
	Infos []string
}

func (*GetError) Error

func (g *GetError) Error() string

type MetaConfig

type MetaConfig struct {
	ETCDEndpoints string `toml:"etcd_endpoints"` // 逗号分隔的ETCD地址,0.0.0.0:2379,0.0.0.0:12379,0.0.0.0:22379
	ETCDUsername  string `toml:"etcd_username"`
	ETCDPassword  string `toml:"etcd_password"`

	ProjectKey   string `toml:"project_key"`
	Namespace    string `toml:"namespace"`
	NamespaceKey string `toml:"namespace_key"`

	Configs        string `toml:"configs"`          // 逗号分隔的 config_name.config_type,如:mysql.toml,cfg.json,redis.yaml,空代表不下载任何配置
	ConfigFilePath string `toml:"config_file_path"` // 本地配置文件存放路径,空代表不存储本都配置文件
	LogLevel       string `toml:"log_level"`        // 日志级别(DEBUG\INFO\WARN\ERROR),默认 WARN
	MergeConfig    bool   `toml:"merge_config"`     // 是否合并配置,合并配置则会将同类型的配置合并到一个文件中,需要先设置ConfigFilePath
}

func (*MetaConfig) SplitConfigs

func (m *MetaConfig) SplitConfigs() []string

func (*MetaConfig) SplitETCDEndpoints

func (m *MetaConfig) SplitETCDEndpoints() []string

type OnConfigChange

type OnConfigChange func(configFileKey string, s *Sail)

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithConfigPath

func WithConfigPath(configPath string) Option

WithConfigPath 指定备份配置文件的存储路径,为空则不存储备份配置文件

func WithConfigs

func WithConfigs(configs []string) Option

WithConfigs 指定获取哪些配置文件,传空则不获取任何配置。

func WithETCDClientConfig

func WithETCDClientConfig(cfg *clientv3.Config) Option

WithETCDClientConfig 自定义的 ETCD 连接配置

func WithLogger

func WithLogger(logger logger.Logger) Option

WithLogger 强烈建议替换为自己的Logger,自带的 logger 比较简单

func WithMergeConfig

func WithMergeConfig(merge bool) Option

WithMergeConfig 将所有的配置都合并到一个文件: config.toml 需要先指定 ConfigFilePath

func WithOnConfigChange

func WithOnConfigChange(f OnConfigChange) Option

WithOnConfigChange 配置变更回调 configFileKey 变更的配置文件名 s 更新后的 sail 实例

type Sail

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

func New

func New(meta *MetaConfig, opts ...Option) *Sail

func NewWithEnv

func NewWithEnv(opts ...Option) *Sail

func NewWithFlag

func NewWithFlag(opts ...Option) *Sail

func NewWithToml

func NewWithToml(tomlFilePath string, opts ...Option) *Sail

func (*Sail) Close

func (s *Sail) Close() error

func (*Sail) Err

func (s *Sail) Err() error

Err 初始化(New)时,失败的 Error 会保存在此处 例: s := sail.New() err := s.Err()

if err != nil{
   doing...
}

func (*Sail) Get

func (s *Sail) Get(key string) (interface{}, error)

func (*Sail) GetBool

func (s *Sail) GetBool(key string) (bool, error)

func (*Sail) GetBoolWithName

func (s *Sail) GetBoolWithName(key string, name string) bool

func (*Sail) GetDuration

func (s *Sail) GetDuration(key string) (time.Duration, error)

func (*Sail) GetDurationWithName

func (s *Sail) GetDurationWithName(key string, name string) time.Duration

func (*Sail) GetFloat64

func (s *Sail) GetFloat64(key string) (float64, error)

func (*Sail) GetFloat64WithName

func (s *Sail) GetFloat64WithName(key string, name string) float64

func (*Sail) GetInt

func (s *Sail) GetInt(key string) (int, error)

func (*Sail) GetInt32

func (s *Sail) GetInt32(key string) (int32, error)

func (*Sail) GetInt32WithName

func (s *Sail) GetInt32WithName(key string, name string) int32

func (*Sail) GetInt64

func (s *Sail) GetInt64(key string) (int64, error)

func (*Sail) GetInt64WithName

func (s *Sail) GetInt64WithName(key string, name string) int64

func (*Sail) GetIntSlice

func (s *Sail) GetIntSlice(key string) ([]int, error)

func (*Sail) GetIntSliceWithName

func (s *Sail) GetIntSliceWithName(key string, name string) []int

func (*Sail) GetIntWithName

func (s *Sail) GetIntWithName(key string, name string) int

func (*Sail) GetSizeInBytes

func (s *Sail) GetSizeInBytes(key string) (uint, error)

func (*Sail) GetSizeInBytesWithName

func (s *Sail) GetSizeInBytesWithName(key string, name string) uint

func (*Sail) GetString

func (s *Sail) GetString(key string) (string, error)

func (*Sail) GetStringMap

func (s *Sail) GetStringMap(key string) (map[string]interface{}, error)

func (*Sail) GetStringMapString

func (s *Sail) GetStringMapString(key string) (map[string]string, error)

func (*Sail) GetStringMapStringSlice

func (s *Sail) GetStringMapStringSlice(key string) (map[string][]string, error)

func (*Sail) GetStringMapStringSliceWithName

func (s *Sail) GetStringMapStringSliceWithName(key string, name string) map[string][]string

func (*Sail) GetStringMapStringWithName

func (s *Sail) GetStringMapStringWithName(key string, name string) map[string]string

func (*Sail) GetStringMapWithName

func (s *Sail) GetStringMapWithName(key string, name string) map[string]interface{}

func (*Sail) GetStringSlice

func (s *Sail) GetStringSlice(key string) ([]string, error)

func (*Sail) GetStringSliceWithName

func (s *Sail) GetStringSliceWithName(key string, name string) []string

func (*Sail) GetStringWithName

func (s *Sail) GetStringWithName(key string, name string) string

func (*Sail) GetTime

func (s *Sail) GetTime(key string) (time.Time, error)

func (*Sail) GetTimeWithName

func (s *Sail) GetTimeWithName(key string, name string) time.Time

func (*Sail) GetUint

func (s *Sail) GetUint(key string) (uint, error)

func (*Sail) GetUintWithName

func (s *Sail) GetUintWithName(key string, name string) uint

func (*Sail) GetViperWithName

func (s *Sail) GetViperWithName(name string) *viper.Viper

func (*Sail) GetWithName

func (s *Sail) GetWithName(key string, name string) interface{}

func (*Sail) MergeVipers

func (s *Sail) MergeVipers() (*viper.Viper, error)

MergeVipers 把所有配置文件中的配置合并到一个 Viper 实例 如果有重名的配置,则会出现未定义行为,为保证安全,有重名配置请使用 MergeVipersWithName。

func (*Sail) MergeVipersWithName

func (s *Sail) MergeVipersWithName() (*viper.Viper, error)

MergeVipersWithName 把所有配置文件中的配置合并到 Viper 每个配置文件的配置都会加上文件名前缀,所以不怕有重名。 例子: mysql.toml key="va" MergeVipersWithName 后: viper.Get("mysql.toml.key")

func (*Sail) MustGet

func (s *Sail) MustGet(key string) interface{}

func (*Sail) MustGetBool

func (s *Sail) MustGetBool(key string) bool

func (*Sail) MustGetDuration

func (s *Sail) MustGetDuration(key string) time.Duration

func (*Sail) MustGetFloat64

func (s *Sail) MustGetFloat64(key string) float64

func (*Sail) MustGetInt

func (s *Sail) MustGetInt(key string) int

func (*Sail) MustGetInt32

func (s *Sail) MustGetInt32(key string) int32

func (*Sail) MustGetInt64

func (s *Sail) MustGetInt64(key string) int64

func (*Sail) MustGetIntSlice

func (s *Sail) MustGetIntSlice(key string) []int

func (*Sail) MustGetSizeInBytes

func (s *Sail) MustGetSizeInBytes(key string) uint

func (*Sail) MustGetString

func (s *Sail) MustGetString(key string) string

func (*Sail) MustGetStringMap

func (s *Sail) MustGetStringMap(key string) map[string]interface{}

func (*Sail) MustGetStringMapString

func (s *Sail) MustGetStringMapString(key string) map[string]string

func (*Sail) MustGetStringMapStringSlice

func (s *Sail) MustGetStringMapStringSlice(key string) map[string][]string

func (*Sail) MustGetStringSlice

func (s *Sail) MustGetStringSlice(key string) []string

func (*Sail) MustGetTime

func (s *Sail) MustGetTime(key string) time.Time

func (*Sail) MustGetUint

func (s *Sail) MustGetUint(key string) uint

func (*Sail) Pull

func (s *Sail) Pull() error

Pull 配置拉取 当无法访问 ETCD 时,会自动导入备份配置文件中的配置,同时自动重连 etcd。

type Watcher

type Watcher interface {
	Run()
}

func NewWatcher

func NewWatcher(ctx context.Context, s *Sail) Watcher

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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