zdpgo_viper

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2022 License: MIT Imports: 5 Imported by: 0

README

zdpgo_viper

基于viper二次封装的配置管理库

项目地址:https://github.com/zhangdapeng520/zdpgo_viper

功能清单

  • 读取配置文件,映射为Go结构体

版本历史

  • 版本0.1.0 2022年2月16日 基础功能
  • 版本0.1.1 2022年3月10日 移除zap,配置比日志要提前

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsulConfig

type ConsulConfig struct {
	Host string `mapstructure:"host" json:"host"` // consul主机地址
	Port uint16 `mapstructure:"port" json:"port"` // consul端口号
	Key  string `mapstructure:"key" json:"key"`   // key
	Type string `mapstructure:"type" json:"type"` // 类型:json,yaml
	// contains filtered or unexported fields
}

ConsulConfig 读取consul的配置信息

type MysqlConfig

type MysqlConfig struct {
	Host     string `mapstructure:"host" json:"host"`         // 主机地址
	Port     int    `mapstructure:"port" json:"port"`         // 端口号
	Name     string `mapstructure:"db" json:"database"`       // 数据库名称
	User     string `mapstructure:"user" json:"username"`     // 用户名
	Password string `mapstructure:"password" json:"password"` // 密码
}

MysqlConfig MySQL配置信息

type NacosConfig

type NacosConfig struct {
	Host      string `mapstructure:"host" json:"host"`           // nacos主机
	Port      uint64 `mapstructure:"port" json:"port"`           // nacos端口
	Namespace string `mapstructure:"namespace" json:"namespace"` // 名称空间
	User      string `mapstructure:"user" json:"user"`           // 用户名
	Password  string `mapstructure:"password" json:"password"`   // 密码
	DataId    string `mapstructure:"dataid" json:"data_id"`      // 配置名
	Group     string `mapstructure:"group" json:"group"`         // 组名
}

NacosConfig nacos配置

type ServiceConfig

type ServiceConfig struct {
	Name       string       `mapstructure:"name" json:"name"`     // 服务名
	Host       string       `mapstructure:"host" json:"host"`     // 主机地址
	Port       uint16       `mapstructure:"port" json:"port"`     // 服务端口号
	Tags       []string     `mapstructure:"tags" json:"tags"`     // 服务标签列表
	MysqlInfo  MysqlConfig  `mapstructure:"mysql" json:"mysql"`   // mysql配置
	ConsulInfo ConsulConfig `mapstructure:"consul" json:"consul"` // consul配置
}

ServiceConfig Service服务配置

type Viper

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

Viper viper配置核心对象

func New

func New() *Viper

New 创建viper实例

func (*Viper) Get

func (v *Viper) Get(key string) (value interface{})

Get 根据key获取配置 @param key 键 @return value 成功返回配置值,失败返回nil

func (*Viper) GetEnvBool

func (v *Viper) GetEnvBool(envName string) (flag bool)

GetEnvBool 读取bool类型环境变量 @param envName 环境变量名称 @return flag 读取结果

func (*Viper) ReadConfig

func (v *Viper) ReadConfig(configFilePath string, configStruct interface{}) (err error)

ReadConfig 读取配置 @param configFilePath 配置文件路径 @param configStruct 配置文件对应的结构体 @return err 读取成功返回nil,读取失败返回失败信息

func (*Viper) ReadConsul

func (v *Viper) ReadConsul(config ConsulConfig) (err error)

ReadConsul 读取consul配置 @param config consul的配置信息 @param configStruct 配置对应的结构体 @return err 成功返回nil,失败返回错误信息

func (*Viper) Update

func (v *Viper) Update(key string, value interface{})

Update 更新配置 @param key 键 @param value 值

func (*Viper) WatchConfig

func (v *Viper) WatchConfig(configFilePath string, configStruct interface{}) (err error)

WatchConfig 监听配置文件变化 @param configFilePath 配置文件路径 @param configStruct 配置文件对应的结构体 @return err 读取成功返回nil,读取失败返回失败信息

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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