mirror of
https://github.com/go-micro/go-micro.git
synced 2025-03-17 20:28:06 +02:00
This change fixes grpc config example code which is currently broken. Several pieces in the change: - use yaml encoder to read config files - rename *.yml to *.yaml to fix format (file suffix) for encoder lookup - replace util/log package with logger as the former one is deprecated
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
grpcConfig "github.com/asim/go-micro/plugins/config/source/grpc/v3"
|
|
"github.com/asim/go-micro/v3/config"
|
|
log "github.com/asim/go-micro/v3/logger"
|
|
)
|
|
|
|
type Micro struct {
|
|
Info
|
|
}
|
|
|
|
type Info struct {
|
|
Name string `json:"name,omitempty"`
|
|
Version string `json:"version,omitempty"`
|
|
Message string `json:"message,omitempty"`
|
|
Age int `json:"age,omitempty"`
|
|
}
|
|
|
|
func main() {
|
|
// create new source
|
|
source := grpcConfig.NewSource(
|
|
grpcConfig.WithAddress("127.0.0.1:8600"),
|
|
grpcConfig.WithPath("/micro"),
|
|
)
|
|
|
|
// create new config
|
|
conf, _ := config.NewConfig()
|
|
|
|
// load the source into config
|
|
if err := conf.Load(source); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
configs := &Micro{}
|
|
if err := conf.Scan(configs); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Infof("Read config: %s", string(conf.Bytes()))
|
|
|
|
// watch the config for changes
|
|
watcher, err := conf.Watch()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Infof("Watching for changes ...")
|
|
|
|
for {
|
|
v, err := watcher.Next()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Infof("Watching for changes: %v", string(v.Bytes()))
|
|
}
|
|
}
|