fix/apollo
baozhecheng 2 years ago
parent c423dc3494
commit 102092009e
  1. 13
      contrib/config/apollo/apollo.go
  2. 7
      contrib/config/apollo/watcher.go

@ -16,6 +16,13 @@ type apollo struct {
opt *options opt *options
} }
const (
yaml = "yaml"
json = "json"
yml = "yml"
properties = "properties"
)
// Option is apollo option // Option is apollo option
type Option func(*options) type Option func(*options)
@ -109,7 +116,7 @@ func NewSource(opts ...Option) config.Source {
func format(ns string) string { func format(ns string) string {
arr := strings.Split(ns, ".") arr := strings.Split(ns, ".")
if len(arr) <= 1 || arr[len(arr)-1] == "properties" { if len(arr) <= 1 || arr[len(arr)-1] == {
return "json" return "json"
} }
return arr[len(arr)-1] return arr[len(arr)-1]
@ -120,8 +127,8 @@ func (e *apollo) load() []*config.KeyValue {
namespaces := strings.Split(e.opt.namespace, ",") namespaces := strings.Split(e.opt.namespace, ",")
for _, ns := range namespaces { for _, ns := range namespaces {
if strings.Contains(ns, ".") && !strings.Contains(ns, "properties") && if strings.Contains(ns, ".") && !strings.Contains(ns, properties) &&
(format(ns) == "yaml" || format(ns) == "yaml" || format(ns) == "json") { (format(ns) == yaml || format(ns) == yml || format(ns) == json) {
value, err := e.client.GetConfigCache(ns).Get("content") value, err := e.client.GetConfigCache(ns).Get("content")
if err != nil { if err != nil {
log.Errorf("apollo get config failed,err:%v", err) log.Errorf("apollo get config failed,err:%v", err)

@ -2,9 +2,10 @@ package apollo
import ( import (
"context" "context"
"github.com/go-kratos/kratos/v2/encoding"
"strings" "strings"
"github.com/go-kratos/kratos/v2/encoding"
"github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
@ -25,8 +26,8 @@ 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) kv := make([]*config.KeyValue, 0, 2)
if strings.Contains(namespace, ".") && !strings.Contains(namespace, "properties") && if strings.Contains(namespace, ".") && !strings.Contains(namespace, properties) &&
(format(namespace) == "yaml" || format(namespace) == "yaml" || format(namespace) == "json") { (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)

Loading…
Cancel
Save