Add Config to service options (#1336)

Co-authored-by: Ben Toogood <ben@micro.mu>
Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
ben-toogood 2020-03-12 18:13:03 +00:00 committed by GitHub
parent 1b4e881d74
commit 47f1203e97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 24 deletions

View File

@ -7,12 +7,13 @@ import (
"strings" "strings"
"time" "time"
"github.com/micro/go-micro/v2/auth/provider"
"github.com/micro/go-micro/v2/auth" "github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/auth/provider"
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
configSrv "github.com/micro/go-micro/v2/config/source/service"
"github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/profile/http" "github.com/micro/go-micro/v2/debug/profile/http"
"github.com/micro/go-micro/v2/debug/profile/pprof" "github.com/micro/go-micro/v2/debug/profile/pprof"
@ -305,6 +306,11 @@ var (
EnvVars: []string{"MICRO_AUTH_PROVIDER_SCOPE"}, EnvVars: []string{"MICRO_AUTH_PROVIDER_SCOPE"},
Usage: "The scope to be used for oauth", Usage: "The scope to be used for oauth",
}, },
&cli.StringFlag{
Name: "config",
EnvVars: []string{"MICRO_CONFIG"},
Usage: "The source of the config to be used to get configuration",
},
} }
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{ DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
@ -373,6 +379,10 @@ var (
"http": http.NewProfile, "http": http.NewProfile,
"pprof": pprof.NewProfile, "pprof": pprof.NewProfile,
} }
DefaultConfigs = map[string]func(...config.Option) (config.Config, error){
"service": config.NewConfig,
}
) )
func init() { func init() {
@ -392,6 +402,7 @@ func newCmd(opts ...Option) Cmd {
Store: &store.DefaultStore, Store: &store.DefaultStore,
Tracer: &trace.DefaultTracer, Tracer: &trace.DefaultTracer,
Profile: &profile.DefaultProfile, Profile: &profile.DefaultProfile,
Config: &config.DefaultConfig,
Brokers: DefaultBrokers, Brokers: DefaultBrokers,
Clients: DefaultClients, Clients: DefaultClients,
@ -404,6 +415,7 @@ func newCmd(opts ...Option) Cmd {
Tracers: DefaultTracers, Tracers: DefaultTracers,
Auths: DefaultAuths, Auths: DefaultAuths,
Profiles: DefaultProfiles, Profiles: DefaultProfiles,
Configs: DefaultConfigs,
} }
for _, o := range opts { for _, o := range opts {
@ -700,6 +712,13 @@ func (c *cmd) Before(ctx *cli.Context) error {
} }
} }
if ctx.String("config") == "service" {
opt := config.WithSource(configSrv.NewSource())
if err := (*c.opts.Config).Init(opt); err != nil {
logger.Fatalf("Error configuring config: %v", err)
}
}
// client opts // client opts
if r := ctx.Int("client_retries"); r >= 0 { if r := ctx.Int("client_retries"); r >= 0 {
clientOpts = append(clientOpts, client.Retries(r)) clientOpts = append(clientOpts, client.Retries(r))

View File

@ -7,6 +7,7 @@ import (
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/registry"
@ -34,6 +35,7 @@ type Options struct {
Tracer *trace.Tracer Tracer *trace.Tracer
Auth *auth.Auth Auth *auth.Auth
Profile *profile.Profile Profile *profile.Profile
Config *config.Config
Brokers map[string]func(...broker.Option) broker.Broker Brokers map[string]func(...broker.Option) broker.Broker
Clients map[string]func(...client.Option) client.Client Clients map[string]func(...client.Option) client.Client
@ -46,6 +48,7 @@ type Options struct {
Tracers map[string]func(...trace.Option) trace.Tracer Tracers map[string]func(...trace.Option) trace.Tracer
Auths map[string]func(...auth.Option) auth.Auth Auths map[string]func(...auth.Option) auth.Auth
Profiles map[string]func(...profile.Option) profile.Profile Profiles map[string]func(...profile.Option) profile.Profile
Configs map[string]func(...config.Option) (config.Config, error)
// Other options for implementations of the interface // Other options for implementations of the interface
// can be stored in a context // can be stored in a context

View File

@ -14,6 +14,8 @@ import (
type Config interface { type Config interface {
// provide the reader.Values interface // provide the reader.Values interface
reader.Values reader.Values
// Init the config
Init(opts ...Option) error
// Stop the config loader/watcher // Stop the config loader/watcher
Close() error Close() error
// Load config sources // Load config sources

View File

@ -31,38 +31,40 @@ type watcher struct {
} }
func newConfig(opts ...Option) (Config, error) { func newConfig(opts ...Option) (Config, error) {
options := Options{ var c config
c.Init(opts...)
go c.run()
return &c, nil
}
func (c *config) Init(opts ...Option) error {
c.opts = Options{
Loader: memory.NewLoader(), Loader: memory.NewLoader(),
Reader: json.NewReader(), Reader: json.NewReader(),
} }
for _, o := range opts { for _, o := range opts {
o(&options) o(&c.opts)
} }
if err := options.Loader.Load(options.Source...); err != nil { err := c.opts.Loader.Load(c.opts.Source...)
return nil, err
}
snap, err := options.Loader.Snapshot()
if err != nil { if err != nil {
return nil, err return err
} }
vals, err := options.Reader.Values(snap.ChangeSet)
c.snap, err = c.opts.Loader.Snapshot()
if err != nil { if err != nil {
return nil, err return err
} }
c := &config{ c.vals, err = c.opts.Reader.Values(c.snap.ChangeSet)
exit: make(chan bool), if err != nil {
opts: options, return err
snap: snap,
vals: vals,
} }
go c.run() return nil
return c, nil
} }
func (c *config) run() { func (c *config) run() {

View File

@ -13,7 +13,6 @@ var (
DefaultName = "go.micro.config" DefaultName = "go.micro.config"
DefaultNamespace = "global" DefaultNamespace = "global"
DefaultPath = "" DefaultPath = ""
DefaultClient = client.DefaultClient
) )
type service struct { type service struct {
@ -25,7 +24,8 @@ type service struct {
} }
func (m *service) Read() (set *source.ChangeSet, err error) { func (m *service) Read() (set *source.ChangeSet, err error) {
req, err := m.client.Read(context.Background(), &proto.ReadRequest{ client := proto.NewConfigService(m.serviceName, client.DefaultClient)
req, err := client.Read(context.Background(), &proto.ReadRequest{
Namespace: m.namespace, Namespace: m.namespace,
Path: m.path, Path: m.path,
}) })
@ -37,7 +37,8 @@ func (m *service) Read() (set *source.ChangeSet, err error) {
} }
func (m *service) Watch() (w source.Watcher, err error) { func (m *service) Watch() (w source.Watcher, err error) {
stream, err := m.client.Watch(context.Background(), &proto.WatchRequest{ client := proto.NewConfigService(m.serviceName, client.DefaultClient)
stream, err := client.Watch(context.Background(), &proto.WatchRequest{
Namespace: m.namespace, Namespace: m.namespace,
Path: m.path, Path: m.path,
}) })
@ -91,7 +92,6 @@ func NewSource(opts ...source.Option) source.Source {
opts: options, opts: options,
namespace: namespace, namespace: namespace,
path: path, path: path,
client: proto.NewConfigService(addr, DefaultClient),
} }
return s return s

View File

@ -9,6 +9,7 @@ import (
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/config/cmd" "github.com/micro/go-micro/v2/config/cmd"
"github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/debug/trace"
@ -27,6 +28,7 @@ type Options struct {
Registry registry.Registry Registry registry.Registry
Transport transport.Transport Transport transport.Transport
Profile profile.Profile Profile profile.Profile
Config config.Config
// Before and After funcs // Before and After funcs
BeforeStart []func() error BeforeStart []func() error
@ -46,6 +48,7 @@ func newOptions(opts ...Option) Options {
Auth: auth.DefaultAuth, Auth: auth.DefaultAuth,
Broker: broker.DefaultBroker, Broker: broker.DefaultBroker,
Cmd: cmd.DefaultCmd, Cmd: cmd.DefaultCmd,
Config: config.DefaultConfig,
Client: client.DefaultClient, Client: client.DefaultClient,
Server: server.DefaultServer, Server: server.DefaultServer,
Registry: registry.DefaultRegistry, Registry: registry.DefaultRegistry,
@ -143,6 +146,13 @@ func Auth(a auth.Auth) Option {
} }
} }
// Config sets the config for the service
func Config(c config.Config) Option {
return func(o *Options) {
o.Config = c
}
}
// Selector sets the selector for the service client // Selector sets the selector for the service client
func Selector(s selector.Selector) Option { func Selector(s selector.Selector) Option {
return func(o *Options) { return func(o *Options) {