mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
Add yaml config example (#2203)
* examples: add yaml config * examples: update go mod for yaml plugin
This commit is contained in:
parent
3e0411a3f6
commit
a46dc0b856
9
examples/config/yaml/README.md
Normal file
9
examples/config/yaml/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Yaml Config
|
||||
|
||||
This is an example of reading yaml config in a file.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
go run main.go
|
||||
```
|
7
examples/config/yaml/config.yaml
Normal file
7
examples/config/yaml/config.yaml
Normal file
@ -0,0 +1,7 @@
|
||||
hosts:
|
||||
database:
|
||||
address: 10.0.0.1
|
||||
port: 3306
|
||||
cache:
|
||||
address: 10.0.0.2
|
||||
port: 6379
|
51
examples/config/yaml/main.go
Normal file
51
examples/config/yaml/main.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
yaml "github.com/asim/go-micro/plugins/config/encoder/yaml/v3"
|
||||
"github.com/asim/go-micro/v3/config"
|
||||
"github.com/asim/go-micro/v3/config/reader"
|
||||
"github.com/asim/go-micro/v3/config/reader/json"
|
||||
"github.com/asim/go-micro/v3/config/source/file"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// new yaml encoder
|
||||
enc := yaml.NewEncoder()
|
||||
|
||||
// new config
|
||||
c, _ := config.NewConfig(
|
||||
config.WithReader(
|
||||
json.NewReader( // json reader for internal config merge
|
||||
reader.WithEncoder(enc),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
// load the config from a file source
|
||||
if err := c.Load(file.NewSource(
|
||||
file.WithPath("./config.yaml"),
|
||||
)); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("data", c.Map())
|
||||
|
||||
// define our own host type
|
||||
type Host struct {
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
var host Host
|
||||
|
||||
// read a database host
|
||||
if err := c.Get("hosts", "database").Scan(&host); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(host.Address, host.Port)
|
||||
}
|
@ -5,6 +5,7 @@ go 1.16
|
||||
require (
|
||||
github.com/asim/go-micro/plugins/client/http/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
github.com/asim/go-micro/plugins/config/encoder/toml/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
github.com/asim/go-micro/plugins/config/encoder/yaml/v3 v3.0.0-20210804083901-3e0411a3f61b // indirect
|
||||
github.com/asim/go-micro/plugins/config/source/grpc/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
github.com/asim/go-micro/plugins/server/http/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
github.com/asim/go-micro/plugins/wrapper/select/roundrobin/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
|
@ -89,6 +89,8 @@ github.com/asim/go-micro/plugins/client/http/v3 v3.0.0-20210403073940-e7a7e3a050
|
||||
github.com/asim/go-micro/plugins/client/http/v3 v3.0.0-20210403073940-e7a7e3a05092/go.mod h1:Ku9ESaA5G1BIU9CYYM+nXxIDoDt+7X4lJse/iU0R7dI=
|
||||
github.com/asim/go-micro/plugins/config/encoder/toml/v3 v3.0.0-20210403073940-e7a7e3a05092 h1:IlS6vW3jvmJjk5HryWgVP+0uYloKqossvbGiQ4nyXKg=
|
||||
github.com/asim/go-micro/plugins/config/encoder/toml/v3 v3.0.0-20210403073940-e7a7e3a05092/go.mod h1:D+mkq2EASL7nsd7UNLpKZZsuhCnxladX3sfM/gmcW+M=
|
||||
github.com/asim/go-micro/plugins/config/encoder/yaml/v3 v3.0.0-20210804083901-3e0411a3f61b h1:4BLu8EYJyOJ66BQHPLtQYGuxe1pbpvxYLjPWCnwWSY4=
|
||||
github.com/asim/go-micro/plugins/config/encoder/yaml/v3 v3.0.0-20210804083901-3e0411a3f61b/go.mod h1:yXyEK7d097bwYcWdjZHwX07kC9kKNSNKmo9z2Kf8Zj4=
|
||||
github.com/asim/go-micro/plugins/config/source/grpc/v3 v3.0.0-20210403073940-e7a7e3a05092 h1:h8VnyFBH1JyrN76rxqpBmXratAEc7D/goWUweA1W8CM=
|
||||
github.com/asim/go-micro/plugins/config/source/grpc/v3 v3.0.0-20210403073940-e7a7e3a05092/go.mod h1:zQevfYGVnE3ojeKbuiawb/vU+TuolXSVilxu9Nyhr/g=
|
||||
github.com/asim/go-micro/plugins/registry/memory/v3 v3.0.0-20210202145831-070250155285 h1:zgDzypD412DKHoXIXW1JwIOWoGcjxzI1iF6JmwtyqHw=
|
||||
@ -206,6 +208,7 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
|
||||
github.com/fsouza/go-dockerclient v1.6.0/go.mod h1:YWwtNPuL4XTX1SKJQk86cWPmmqwx+4np9qfPbb+znGc=
|
||||
github.com/fsouza/go-dockerclient v1.7.3/go.mod h1:8xfZB8o9SptLNJ13VoV5pMiRbZGWkU/Omu5VOu/KC9Y=
|
||||
github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw=
|
||||
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
|
Loading…
Reference in New Issue
Block a user