diff --git a/adapter/config/nacos/config.go b/adapter/config/nacos/config.go index 2d4f5ce..69528e9 100644 --- a/adapter/config/nacos/config.go +++ b/adapter/config/nacos/config.go @@ -16,6 +16,7 @@ type Option func(*options) type options struct { group string dataID string + format string } // WithGroup With nacos config group. @@ -32,6 +33,13 @@ func WithDataID(dataID string) Option { } } +// WithFormat with nacos config format. +func WithFormat(format string) Option { + return func(o *options) { + o.format = format + } +} + type Config struct { opts options client config_client.IConfigClient @@ -54,17 +62,21 @@ func (c *Config) Load() ([]*config.KeyValue, error) { return nil, err } k := c.opts.dataID + format := strings.TrimPrefix(filepath.Ext(k), ".") + if format == "" && c.opts.format != "" { + format = c.opts.format + } return []*config.KeyValue{ { Key: k, Value: []byte(content), - Format: strings.TrimPrefix(filepath.Ext(k), "."), + Format: format, }, }, nil } func (c *Config) Watch() (config.Watcher, error) { - watcher := newWatcher(context.Background(), c.opts.dataID, c.opts.group, c.client.CancelListenConfig) + watcher := newWatcher(context.Background(), c.opts.dataID, c.opts.group, c.opts.format, c.client.CancelListenConfig) err := c.client.ListenConfig(vo.ConfigParam{ DataId: c.opts.dataID, Group: c.opts.group, diff --git a/adapter/config/nacos/watcher.go b/adapter/config/nacos/watcher.go index 100cc8c..0636c1c 100644 --- a/adapter/config/nacos/watcher.go +++ b/adapter/config/nacos/watcher.go @@ -13,6 +13,7 @@ import ( type Watcher struct { dataID string group string + format string content chan string cancelListenConfig cancelListenConfigFunc @@ -22,11 +23,12 @@ type Watcher struct { type cancelListenConfigFunc func(params vo.ConfigParam) (err error) -func newWatcher(ctx context.Context, dataID string, group string, cancelListenConfig cancelListenConfigFunc) *Watcher { +func newWatcher(ctx context.Context, dataID string, group string, format string, cancelListenConfig cancelListenConfigFunc) *Watcher { ctx, cancel := context.WithCancel(ctx) w := &Watcher{ dataID: dataID, group: group, + format: format, cancelListenConfig: cancelListenConfig, content: make(chan string, 100), @@ -37,6 +39,10 @@ func newWatcher(ctx context.Context, dataID string, group string, cancelListenCo } func (w *Watcher) Next() ([]*config.KeyValue, error) { + format := strings.TrimPrefix(filepath.Ext(w.dataID), ".") + if format == "" && w.format != "" { + format = w.format + } select { case <-w.ctx.Done(): return nil, w.ctx.Err() @@ -46,7 +52,7 @@ func (w *Watcher) Next() ([]*config.KeyValue, error) { { Key: k, Value: []byte(content), - Format: strings.TrimPrefix(filepath.Ext(k), "."), + Format: format, }, }, nil } diff --git a/api/config/v1/config.pb.go b/api/config/v1/config.pb.go index d31c997..ec63afb 100644 --- a/api/config/v1/config.pb.go +++ b/api/config/v1/config.pb.go @@ -900,7 +900,7 @@ type Nacos struct { // nacos config group Group string `protobuf:"bytes,3,opt,name=group,proto3" json:"group,omitempty"` // nacos config namespace - Namespaces string `protobuf:"bytes,4,opt,name=namespaces,proto3" json:"namespaces,omitempty"` + Namespace string `protobuf:"bytes,4,opt,name=namespace,proto3" json:"namespace,omitempty"` // nacos config username Username string `protobuf:"bytes,5,opt,name=username,proto3" json:"username,omitempty"` // nacos config password @@ -915,6 +915,8 @@ type Nacos struct { Timeout *durationpb.Duration `protobuf:"bytes,10,opt,name=timeout,proto3" json:"timeout,omitempty"` // nacos logger level. default info LogLevel string `protobuf:"bytes,11,opt,name=log_level,json=logLevel,proto3" json:"log_level,omitempty"` + // nacos format, default=yaml + Format string `protobuf:"bytes,12,opt,name=format,proto3" json:"format,omitempty"` } func (x *Nacos) Reset() { @@ -970,9 +972,9 @@ func (x *Nacos) GetGroup() string { return "" } -func (x *Nacos) GetNamespaces() string { +func (x *Nacos) GetNamespace() string { if x != nil { - return x.Namespaces + return x.Namespace } return "" } @@ -1026,6 +1028,13 @@ func (x *Nacos) GetLogLevel() string { return "" } +func (x *Nacos) GetFormat() string { + if x != nil { + return x.Format + } + return "" +} + // middleware config type Middleware struct { state protoimpl.MessageState @@ -1225,36 +1234,37 @@ var file_config_v1_config_proto_rawDesc = []byte{ 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, - 0xd3, 0x02, 0x0a, 0x05, 0x4e, 0x61, 0x63, 0x6f, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, + 0xe9, 0x02, 0x0a, 0x05, 0x4e, 0x61, 0x63, 0x6f, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x17, 0x0a, 0x07, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x61, 0x74, 0x61, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x67, 0x72, 0x6f, - 0x75, 0x70, 0x12, 0x1e, 0x0a, 0x0a, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x73, - 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, - 0x65, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, - 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, - 0x6e, 0x74, 0x65, 0x78, 0x74, 0x5f, 0x70, 0x61, 0x74, 0x68, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x0b, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x61, 0x74, 0x68, 0x12, 0x17, 0x0a, - 0x07, 0x6c, 0x6f, 0x67, 0x5f, 0x64, 0x69, 0x72, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, - 0x6c, 0x6f, 0x67, 0x44, 0x69, 0x72, 0x12, 0x1b, 0x0a, 0x09, 0x63, 0x61, 0x63, 0x68, 0x65, 0x5f, - 0x64, 0x69, 0x72, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x61, 0x63, 0x68, 0x65, - 0x44, 0x69, 0x72, 0x12, 0x33, 0x0a, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x0a, - 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, - 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x6c, 0x6f, 0x67, 0x5f, - 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6c, 0x6f, 0x67, - 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x50, 0x0a, 0x0a, 0x4d, 0x69, 0x64, 0x64, 0x6c, 0x65, 0x77, - 0x61, 0x72, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x2e, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, - 0x6e, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, - 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79, 0x52, 0x07, - 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, - 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6e, 0x65, 0x78, 0x74, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2f, - 0x6e, 0x65, 0x78, 0x74, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2f, - 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x75, 0x70, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, + 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, + 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, + 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x74, + 0x65, 0x78, 0x74, 0x5f, 0x70, 0x61, 0x74, 0x68, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, + 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x61, 0x74, 0x68, 0x12, 0x17, 0x0a, 0x07, 0x6c, + 0x6f, 0x67, 0x5f, 0x64, 0x69, 0x72, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6c, 0x6f, + 0x67, 0x44, 0x69, 0x72, 0x12, 0x1b, 0x0a, 0x09, 0x63, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x64, 0x69, + 0x72, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x61, 0x63, 0x68, 0x65, 0x44, 0x69, + 0x72, 0x12, 0x33, 0x0a, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x0a, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x07, 0x74, + 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x6c, 0x6f, 0x67, 0x5f, 0x6c, 0x65, + 0x76, 0x65, 0x6c, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6c, 0x6f, 0x67, 0x4c, 0x65, + 0x76, 0x65, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x0c, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x22, 0x50, 0x0a, 0x0a, 0x4d, + 0x69, 0x64, 0x64, 0x6c, 0x65, 0x77, 0x61, 0x72, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, + 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x2e, 0x0a, + 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, + 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2e, 0x41, 0x6e, 0x79, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x42, 0x29, 0x5a, + 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6e, 0x65, 0x78, 0x74, + 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2f, 0x6e, 0x65, 0x78, 0x74, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, + 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2f, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/api/config/v1/config.proto b/api/config/v1/config.proto index c20c727..38bf983 100644 --- a/api/config/v1/config.proto +++ b/api/config/v1/config.proto @@ -120,7 +120,7 @@ message Nacos { // nacos config group string group = 3; // nacos config namespace - string namespaces = 4; + string namespace = 4; // nacos config username string username = 5; // nacos config password @@ -135,6 +135,8 @@ message Nacos { google.protobuf.Duration timeout = 10; // nacos logger level. default info string log_level = 11; + // nacos format, default=yaml + string format = 12; } // middleware config diff --git a/config/config.go b/config/config.go index d7747f7..1d630b6 100644 --- a/config/config.go +++ b/config/config.go @@ -109,13 +109,16 @@ func (c *Config) buildNacosSource() ([]kConfig.Source, error) { } else if cfg.GetLogLevel() == "" { cfg.LogLevel = "info" } + if cfg.Format == "" { + cfg.Format = "yaml" + } clientConfig := constant.NewClientConfig( constant.WithUsername(cfg.GetUsername()), constant.WithPassword(cfg.GetPassword()), constant.WithTimeoutMs(duration), constant.WithCacheDir(cfg.GetCacheDir()), - constant.WithNamespaceId(cfg.GetNamespaces()), + constant.WithNamespaceId(cfg.GetNamespace()), constant.WithNotLoadCacheAtStart(true), constant.WithLogLevel(cfg.LogLevel), constant.WithOpenKMS(false), @@ -131,7 +134,7 @@ func (c *Config) buildNacosSource() ([]kConfig.Source, error) { } return []kConfig.Source{ - nacos.NewConfigSource(client, nacos.WithDataID(cfg.DataId), nacos.WithGroup(cfg.Group)), + nacos.NewConfigSource(client, nacos.WithDataID(cfg.DataId), nacos.WithGroup(cfg.Group), nacos.WithFormat(cfg.Format)), }, nil }