fix/apollo
baozhecheng 2 years ago
parent dad8275bd5
commit 7342800caa
  1. 67
      contrib/config/apollo/apollo.go
  2. 12
      contrib/config/apollo/parser.go

@ -4,11 +4,13 @@ import (
"strings"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/encoding"
"github.com/go-kratos/kratos/v2/log"
"github.com/apolloconfig/agollo/v4"
"github.com/apolloconfig/agollo/v4/constant"
apolloConfig "github.com/apolloconfig/agollo/v4/env/config"
"github.com/apolloconfig/agollo/v4/extension"
"github.com/go-kratos/kratos/v2/encoding"
)
type apollo struct {
@ -34,6 +36,7 @@ type options struct {
namespace string
isBackupConfig bool
backupPath string
originConfig bool
}
// WithAppID with apollo config app id
@ -92,6 +95,16 @@ func WithBackupPath(backupPath string) Option {
}
}
// WithOriginalConfig use the original configuration file without parse processing
func WithOriginalConfig() Option {
return func(o *options) {
extension.AddFormatParser(constant.JSON, &jsonExtParser{})
extension.AddFormatParser(constant.YAML, &yamlExtParser{})
extension.AddFormatParser(constant.YML, &yamlExtParser{})
o.originConfig = true
}
}
func NewSource(opts ...Option) config.Source {
op := options{}
for _, o := range opts {
@ -123,25 +136,41 @@ func format(ns string) string {
}
func (e *apollo) load() []*config.KeyValue {
kv := make([]*config.KeyValue, 0)
kvs := make([]*config.KeyValue, 0)
namespaces := strings.Split(e.opt.namespace, ",")
for _, ns := range namespaces {
if !e.opt.originConfig {
kv, err := e.getConfig(ns)
if err != nil {
log.Errorf("apollo get config failed,err:%v", err)
continue
}
kvs = append(kvs, kv)
continue
}
if strings.Contains(ns, ".") && !strings.Contains(ns, properties) &&
(format(ns) == yaml || format(ns) == yml || format(ns) == json) {
value, err := e.client.GetConfigCache(ns).Get("content")
kv, err := e.getOriginConfig(ns)
if err != nil {
log.Errorf("apollo get config failed,err:%v", err)
continue
}
// serialize the namespace content KeyValue into bytes.
kv = append(kv, &config.KeyValue{
Key: ns,
Value: []byte(value.(string)),
Format: format(ns),
})
kvs = append(kvs, kv)
continue
} else {
kv, err := e.getConfig(ns)
if err != nil {
log.Errorf("apollo get config failed,err:%v", err)
continue
}
kvs = append(kvs, kv)
}
}
return kvs
}
func (e *apollo) getConfig(ns string) (*config.KeyValue, error) {
next := map[string]interface{}{}
e.client.GetConfigCache(ns).Range(func(key, value interface{}) bool {
// all values are out properties format
@ -152,16 +181,26 @@ func (e *apollo) load() []*config.KeyValue {
codec := encoding.GetCodec(f)
val, err := codec.Marshal(next)
if err != nil {
log.Warnf("apollo could not handle namespace %s: %v", ns, err)
continue
return nil, err
}
kv = append(kv, &config.KeyValue{
return &config.KeyValue{
Key: ns,
Value: val,
Format: f,
})
}, nil
}
func (e apollo) getOriginConfig(ns string) (*config.KeyValue, error) {
value, err := e.client.GetConfigCache(ns).Get("content")
if err != nil {
return nil, err
}
return kv
// serialize the namespace content KeyValue into bytes.
return &config.KeyValue{
Key: ns,
Value: []byte(value.(string)),
Format: format(ns),
}, nil
}
func (e *apollo) Load() (kv []*config.KeyValue, err error) {

@ -1,10 +1,5 @@
package apollo
import (
"github.com/apolloconfig/agollo/v4/constant"
"github.com/apolloconfig/agollo/v4/extension"
)
type jsonExtParser struct{}
func (parser jsonExtParser) Parse(configContent interface{}) (map[string]interface{}, error) {
@ -16,10 +11,3 @@ type yamlExtParser struct{}
func (parser yamlExtParser) Parse(configContent interface{}) (map[string]interface{}, error) {
return map[string]interface{}{"content": configContent}, nil
}
func init() {
// add json/yaml/yml format
extension.AddFormatParser(constant.JSON, &jsonExtParser{})
extension.AddFormatParser(constant.YAML, &yamlExtParser{})
extension.AddFormatParser(constant.YML, &yamlExtParser{})
}

Loading…
Cancel
Save