fix(contrib/config/apollo):where gcfg config apollo failed to retrieve configurations for multiple namespaces, where watch apollo change resulted in missing configurations. (#4509)

Fixed an issue where `gcfg config apollo` failed to retrieve
configurations for multiple namespaces; fixed an issue where `watch
apollo change` resulted in missing configurations.

---------

Co-authored-by: DAWN <xiongchao@cdfsunrise.com>
Co-authored-by: hailaz <739476267@qq.com>
This commit is contained in:
会跳舞的大笨熊
2025-11-19 16:18:55 +08:00
committed by GitHub
parent cb8594eb80
commit a85b221d32

View File

@ -19,6 +19,7 @@ import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)
@ -28,6 +29,10 @@ var (
_ gcfg.WatcherAdapter = (*Client)(nil)
)
const (
apolloNamespaceDelimiter = ","
)
// Config is the configuration object for apollo client.
type Config struct {
AppID string `v:"required"` // See apolloConfig.Config.
@ -97,11 +102,19 @@ func (c *Client) Available(ctx context.Context, resource ...string) (ok bool) {
if len(resource) == 0 && !c.value.IsNil() {
return true
}
namespace := c.config.NamespaceName
namespaces := gstr.SplitAndTrim(c.config.NamespaceName, apolloNamespaceDelimiter)
if len(resource) > 0 {
namespace = resource[0]
namespaces = resource
}
return c.client.GetConfig(namespace) != nil
for _, namespace := range namespaces {
if c.client.GetConfig(namespace) == nil {
return false
}
}
return true
}
// Get retrieves and returns value by specified `pattern` in current resource.
@ -142,19 +155,20 @@ func (c *Client) OnNewestChange(event *storage.FullChangeEvent) {
func (c *Client) updateLocalValue(ctx context.Context) (err error) {
j := gjson.New(nil)
content := gjson.New(nil, true)
cache := c.client.GetConfigCache(c.config.NamespaceName)
cache.Range(func(key, value any) bool {
err = j.Set(gconv.String(key), value)
if err != nil {
return false
}
err = content.Set(gconv.String(key), value)
if err != nil {
return false
}
return true
})
cache.Clear()
for _, namespace := range gstr.SplitAndTrim(c.config.NamespaceName, apolloNamespaceDelimiter) {
cache := c.client.GetConfigCache(namespace)
cache.Range(func(key, value any) bool {
err = j.Set(gconv.String(key), value)
if err != nil {
return false
}
err = content.Set(gconv.String(key), value)
return err == nil
})
cache.Clear()
}
if err == nil {
c.value.Set(j)
adapterCtx := NewAdapterCtx(ctx).WithOperation(gcfg.OperationUpdate).WithNamespace(c.config.NamespaceName).