Skip to content

trpc-ecosystem/go-config-etcd

Repository files navigation

English | 中文

tRPC-Go etcd configuration plugin

Go Reference Go Report Card LICENSE Releases Tests Coverage

The plugin encapsulates etcd-client, facilitating rapid access to configurations in etcd within the tRPC-Go framework.

Get started

Step 1

Anonymous import this package

import _ "trpc.group/trpc-go/trpc-config-etcd"

Step 2

In the trpc_go.yaml configuration file, set the Endpoint and Dialtimeout, for the complete configuration, refer to Config

plugins:                 
  config:
    etcd:
      endpoints:
        - localhost:2379
      dialtimeout: 5s

Step 3

After calling trpc.NewServer, retrieve the etcd configuration item.

func main() {
	trpc.NewServer()

    // Get the configuration item with the key "foo"
	value, err := config.GetString("foo")
	if err != nil {
		panic(err)
	}
	fmt.Println(value)

      // Watch changes to the configuration item with the key "foo"
	ch, err := config.Get("etcd").Watch(context.Background(), "foo")
	if err != nil {
		panic(err)
	}
	for rsp := range ch {
		fmt.Println(rsp.Value())
	}
}

Notes

The plugin currently only supports reading configurations, and the Get and Put functions are not yet implemented.