Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/shima-park/agollo
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingwang committed Jun 13, 2019
2 parents 8c98781 + d181803 commit 9e4239a
Showing 1 changed file with 78 additions and 2 deletions.
80 changes: 78 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ go get -u github.com/shima-park/agollo
* 提供Viper配置库的apollo插件

## 示例

##### 简单示例
```
func main(){
a, err := agollo.New("localhost:8080", "your_appid", agollo.AutoFetchOnCacheMiss())
if err != nil {
panic(err)
}
// 获取your_appid下
fmt.Println(
a.Get("foo"), // namespace为application中配置项foo的value
a.GetNameSpace("test.json"), // namespace为test.json的所有配置项
a.Get("foo", agollo.WithDefault("bar")), // foo这个key不存在时返回bar
a.Get("foo", agollo.WithNamespace("baz")), // namespace为baz, key为foo的value
)
}
```
##### 详细特性展示
请将example/sample下app.properties修改为你本地或者测试的apollo配置。
```
func main() {
Expand Down Expand Up @@ -54,10 +73,10 @@ func main() {
// 获取默认配置中cluster=default namespace=application key=Name的值
fmt.Println("Name:", agollo.Get("Name"))
// 获取默认配置中cluster=default namespace=application key=Name的值,提供默认值返回
// 获取默认配置中cluster=default namespace=application key=YourConfigKey的值,提供默认值返回
fmt.Println("YourConfigKey:", agollo.Get("YourConfigKey", agollo.WithDefault("YourDefaultValue")))
// 获取默认配置中cluster=default namespace=Test.Namespace key=Name的值,提供默认值返回
// 获取默认配置中cluster=default namespace=Test.Namespace key=YourConfigKey2的值,提供默认值返回
fmt.Println("YourConfigKey2:", agollo.Get("YourConfigKey2", agollo.WithDefault("YourDefaultValue"), agollo.WithNamespace("YourNamespace")))
// 获取namespace下的所有配置项
Expand Down Expand Up @@ -92,6 +111,63 @@ func main() {
}
```

## 结合viper使用,提高配置读取舒适度
例如apollo中有以下配置:
```
appsalt = xxx
database.driver = mysql
database.host = localhost
database.port = 3306
database.timeout = 5s
// ...
```

示例代码:
```
import (
"fmt"
"github.com/shima-park/agollo/viper-remote"
"github.com/spf13/viper"
)
type Config struct {
AppSalt string `mapstructure:"appsalt"`
DB DatabaseConfig `mapstructure:"database"`
}
type DatabaseConfig struct {
Driver string `mapstructure:"driver"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Timeout time.Duration `mapstructure:"timeout"`
// ...
}
func main(){
remote.SetAppID("your_appid")
v := viper.New()
v.SetConfigType("prop") // 根据namespace实际格式设置对应type
err := v.AddRemoteProvider("apollo", "your_apollo_endpoint", "your_apollo_namespace")
// ... error handle
err = v.ReadRemoteConfig()
// ... error handle
// 直接反序列化到结构体中
var conf Config
err = v.Unmarshal(&conf)
// ... error handle
fmt.Printf("%+v\n", conf)
// 各种基础类型配置项读取
fmt.Println("Host:", v.GetString("db.host"))
fmt.Println("Port:", v.GetInt("db.port"))
fmt.Println("Timeout:", v.GetDuration("db.timeout"))
// 获取所有key,所有配置
fmt.Println("AllKeys", v.AllKeys(), "AllSettings", v.AllSettings())
}
```

## License

The project is licensed under the [Apache 2 license](https://github.com/shima-park/agollo/blob/master/LICENSE).

0 comments on commit 9e4239a

Please sign in to comment.