在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:props开源软件地址:https://gitee.com/tietang/props开源软件介绍:props统一的配置工具库,将各种配置源抽象或转换为类似properties格式的key/value,并提供统一的API来访问这些key/value。支持 properties 文件、ini 文件、zookeeper k/v、zookeeper k/props、consul k/v、consul k/props等配置源,并且支持通过 Unmarshal从配置中抽出struct;支持上下文环境变量的eval,${}形式;支持多种配置源组合使用。 特性支持的配置源:
key/value支持的数据类型:
其他特性
Install
或者通过go mod:
配置源和配置形式使用方法:properties格式文件格式: 例子:
或者
通过kvs.ReadPropertyFile读取文件p, err := kvs.ReadPropertyFile("config.properties")if err != nil { panic(err)}stringValue, err := p.Get("prefix.key1")fmt.Println(stringValue, err)//如果不存在,则返回默认值stringDefaultValue := p.GetDefault("prefix.key1", "default value")fmt.Println(stringDefaultValue)boolValue, err := p.GetBool("prefix.key2")fmt.Println(boolValue)boolDefaultValue := p.GetBoolDefault("prefix.key2", false)fmt.Println(boolDefaultValue)intValue, err := p.GetInt("prefix.key3")fmt.Println(intValue)intDefaultValue := p.GetIntDefault("prefix.key3", 1)fmt.Println(intDefaultValue)floatValue, err := p.GetFloat64("prefix.key4")fmt.Println(floatValue)floatDefaultValue := p.GetFloat64Default("prefix.key4", 1.2)fmt.Println(floatDefaultValue) 通过kvs.NewProperties()从io.Reader中读取 p := kvs.NewProperties() p.Load(strings.NewReader("some data")) p.Load(bytes.NewReader([]byte("some data"))) 通过kvs.NewPropertiesConfigSource()file := "/path/to/config.properties"p := kvs.NewPropertiesConfigSource(file)p = kvs.NewPropertiesConfigSourceByFile("name", file)//通过map构造内存型m := make(map[string]string)m["key"]="value"p = kvs.NewPropertiesConfigSourceByMap("name", m) Properties ConfigSourcevar cs kvs.ConfigSource//cs = kvs.NewPropertiesConfigSource("config.properties")cs = kvs.NewPropertiesConfigSourceByFile("config", "config.properties")stringValue, err := cs.Get("prefix.key1")//如果不存在,则返回默认值stringDefaultValue := cs.GetDefault("prefix.key1", "default value")boolValue, err := cs.GetBool("prefix.key2")boolDefaultValue := cs.GetBoolDefault("prefix.key2", false)intValue, err := cs.GetInt("prefix.key3")intDefaultValue := cs.GetIntDefault("prefix.key3", 1)floatValue, err := cs.GetFloat64("prefix.key4")floatDefaultValue := cs.GetFloat64Default("prefix.key4", 1.2) ini格式文件。格式:参考 wiki百科:INI_file [section][key1][=|:][value1] [key1][=|:][value1]... 不支持sub section 例子: [server]port: 8080read.timeout=6000ms[client]connection.timeout=6squery.timeout=6s 使用方法:file := "/path/to/config.ini"p := ini.NewIniFileConfigSource(file)p = ini.NewIniFileConfigSourceByFile("name", file) Nacos只支持key/properties配置形式。 例如有如下配置: http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=test.id&group=testGroup&tenant=testTenant key-0.x0=value-00key-0.x1=value-01key-0.x2=value-02 基本用法: address := "127.0.0.1:8848" c := NewNacosPropsConfigSource(address) c.DataId = "test.id" c.Tenant = "testTenant" c.Group = "testGroup" v:=c.GetDefault("key-0.x0","defaultval") //value-00 zookeeper支持key/value和key/properties配置形式,key/properties配置和ini类似,将key作为section name。 key/value形式,将path去除root path部分并替换 key/properties形式,在root path下读取所有子节点,将子节点名称作为section name,value为子properties格式内存,通过子节点名称和子properties中的key组合成新的key作为key。 by zookeeper key/value基本例子root := "/config/kv/app1/dev"var conn *zk.Connp := zk.NewZookeeperConfigSource("zookeeper-kv", root, conn) CompositeConfigSource多context例子var cs kvs.ConfigSourceurls := []string{"172.16.1.248:2181"}contexts := []string{"/configs/apps","/configs/users"}cs = zk.NewZookeeperCompositeConfigSource(contexts, urls, time.Second*3) 用properties来配置: key/propertiesvalue值为properties格式内容, 整体设计类似ini格式,例如: key:/config/kv/app1/dev/datasource value:url=tcp(127.0.0.1:3306)/Test?charset=utf8username=rootpassword=root root := "/config/kv/app1/dev"var conn *zk.Connp := zk.NewZookeeperIniConfigSource("zookeeper-props", root, conn) consul 多层key/value形式by consul key/value例如:config101/test/demo1/server/port=8080获取的属性和值是:server.port=8080address := "127.0.0.1:8500"root := "config101/test/demo1"c := consul.NewConsulKeyValueConfigSource("consul", address, root)stringValue, err := cs.Get("prefix.key1")stringDefaultValue := cs.GetDefault("prefix.key1", "default value") 用properties来配置: key/propertiesvalue值为properties格式内容, 整体设计类似ini格式,配置样式如下图: root := "config/app1/dev"address := "127.0.0.1:8500"p := consul.NewConsulIniConfigSourceByName("consul-props", address, root) 支持Unmarshal支持的数据类型:
在struct中规定命名为 type Port struct { Port int `val:"8080"` Enabled bool `val:"true"`}type ServerProperties struct { _prefix string `prefix:"http.server"` Port Port Timeout int `val:"1"` Enabled bool Foo int `val:"1"` Time time.Duration `val:"1s"` Float float32 `val:"0.000001"` Params map[string]string Times map[string]time.Duration}func main() { p := kvs.NewMapProperties() p.Set("http.server.port.port", "8080") p.Set("http.server.params.k1", "v1") p.Set("http.server.params.k2", "v2") p.Set("http.server.Times.m1", "1s") p.Set("http.server.Times.m2", "1h") p.Set("http.server.Times.m3", "1us") p.Set("http.server.port.enabled", "false") p.Set("http.server.timeout", "1234") p.Set("http.server.enabled", "true") p.Set("http.server.time", "10s") p.Set("http.server.float", "23.45") p.Set("http.server.foo", "23") s := &ServerProperties{ Foo: 1234, Float: 1234.5, } p.Unmarshal(s) fmt.Println(s)} 上下文变量表达式(或者占位符)的支持支持在props上下文中替换占位符: p := kvs.NewEmptyMapConfigSource("map2")p.Set("orign.key1", "v1")p.Set("orign.key2", "v2")p.Set("orign.key3", "2")p.Set("ph.key1", "${orign.key1}")p.Set("ph.key2", "${orign.key1}:${orign.key2}")p.Set("ph.key3", "${orign.key3}")conf := kvs.NewDefaultCompositeConfigSource(p)phv1, err := conf.GetInt("ph.key1")//v1phv2, err := conf.Get("ph.key2")//v1:v1phv3, err := conf.GetInt("ph.key3")//2 多种配置源组合使用优先级以追加相反的顺序,最后添加优先级最高。 kv1 := []string{"go.app.key1", "value1", "value1-2"}kv2 := []string{"go.app.key2", "value2", "value2-2"}p1 := kvs.NewEmptyMapConfigSource("map1")p1.Set(kv1[0], kv1[1])p1.Set(kv2[0], kv2[1])p2 := kvs.NewEmptyMapConfigSource("map2")p2.Set(kv1[0], kv1[2])p2.Set(kv2[0], kv2[2])conf.Add(p1)conf.Add(p2)//value1==value1-2value1, err := conf.Get(kv1[0])fmt.Println(value1)//value2=value2-2value2, err := conf.Get(kv2[0])fmt.Println(value2) |
请发表评论