pull/2627/head
demoManito 2 years ago
parent ae2f87fe8c
commit ff35910a44
  1. 8
      contrib/config/apollo/apollo.go
  2. 23
      contrib/config/apollo/watcher.go

@ -150,6 +150,11 @@ func format(ns string) string {
return suffix return suffix
} }
func isOriginConfig(ns string) bool {
f := format(ns)
return f == yaml || f == yml || f == json
}
func (e *apollo) load() []*config.KeyValue { func (e *apollo) load() []*config.KeyValue {
var ( var (
kvs = make([]*config.KeyValue, 0) kvs = make([]*config.KeyValue, 0)
@ -166,8 +171,7 @@ func (e *apollo) load() []*config.KeyValue {
kvs = append(kvs, kv) kvs = append(kvs, kv)
continue continue
} }
if strings.Contains(ns, ".") && !strings.HasSuffix(ns, "."+properties) && if strings.Contains(ns, ".") && !strings.HasSuffix(ns, "."+properties) && isOriginConfig(ns) {
(format(ns) == yaml || format(ns) == yml || format(ns) == json) {
kv, err := e.getOriginConfig(ns) kv, err := e.getOriginConfig(ns)
if err != nil { if err != nil {
log.Errorf("apollo get config failed,err:%v", err) log.Errorf("apollo get config failed,err:%v", err)

@ -24,21 +24,19 @@ type customChangeListener struct {
} }
func (c *customChangeListener) onChange(namespace string, changes map[string]*storage.ConfigChange) []*config.KeyValue { func (c *customChangeListener) onChange(namespace string, changes map[string]*storage.ConfigChange) []*config.KeyValue {
kv := make([]*config.KeyValue, 0, 2) if strings.Contains(namespace, ".") && !strings.HasSuffix(namespace, "."+properties) && isOriginConfig(namespace) {
if strings.Contains(namespace, ".") && !strings.HasSuffix(namespace, "."+properties) &&
(format(namespace) == yaml || format(namespace) == yml || format(namespace) == json) {
value, err := c.apollo.client.GetConfigCache(namespace).Get("content") value, err := c.apollo.client.GetConfigCache(namespace).Get("content")
if err != nil { if err != nil {
log.Warnw("apollo get config failed", "err", err) log.Warnw("apollo get config failed", "err", err)
return nil return nil
} }
kv = append(kv, &config.KeyValue{ return []*config.KeyValue{
{
Key: namespace, Key: namespace,
Value: []byte(value.(string)), Value: []byte(value.(string)),
Format: format(namespace), Format: format(namespace),
}) },
}
return kv
} }
next := make(map[string]interface{}) next := make(map[string]interface{})
@ -48,19 +46,18 @@ func (c *customChangeListener) onChange(namespace string, changes map[string]*st
} }
f := format(namespace) f := format(namespace)
codec := encoding.GetCodec(f) val, err := encoding.GetCodec(f).Marshal(next)
val, err := codec.Marshal(next)
if err != nil { if err != nil {
log.Warnf("apollo could not handle namespace %s: %v", namespace, err) log.Warnf("apollo could not handle namespace %s: %v", namespace, err)
return nil return nil
} }
kv = append(kv, &config.KeyValue{ return []*config.KeyValue{
{
Key: namespace, Key: namespace,
Value: val, Value: val,
Format: f, Format: f,
}) },
}
return kv
} }
func (c *customChangeListener) OnChange(changeEvent *storage.ChangeEvent) { func (c *customChangeListener) OnChange(changeEvent *storage.ChangeEvent) {

Loading…
Cancel
Save