github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/config/grpc/client/main.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/micro/go-micro/v2/config"
     5  	"github.com/micro/go-micro/v2/util/log"
     6  	grpcConfig "github.com/micro/go-plugins/config/source/grpc/v2"
     7  )
     8  
     9  type Micro struct {
    10  	Info
    11  }
    12  
    13  type Info struct {
    14  	Name    string `json:"name,omitempty"`
    15  	Version string `json:"version,omitempty"`
    16  	Message string `json:"message,omitempty"`
    17  	Age     int    `json:"age,omitempty"`
    18  }
    19  
    20  func main() {
    21  	// create new source
    22  	source := grpcConfig.NewSource(
    23  		grpcConfig.WithAddress("127.0.0.1:8600"),
    24  		grpcConfig.WithPath("/micro"),
    25  	)
    26  
    27  	// create new config
    28  	conf, _ := config.NewConfig()
    29  
    30  	// load the source into config
    31  	if err := conf.Load(source); err != nil {
    32  		log.Fatal(err)
    33  	}
    34  
    35  	configs := &Micro{}
    36  	if err := conf.Scan(configs); err != nil {
    37  		log.Fatal(err)
    38  	}
    39  
    40  	log.Logf("Read config: %s", string(conf.Bytes()))
    41  
    42  	// watch the config for changes
    43  	watcher, err := conf.Watch()
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  
    48  	log.Logf("Watching for changes ...")
    49  
    50  	for {
    51  		v, err := watcher.Next()
    52  		if err != nil {
    53  			log.Fatal(err)
    54  		}
    55  
    56  		log.Logf("Watching for changes: %v", string(v.Bytes()))
    57  	}
    58  }