关于dubbo:聊聊dubbogo的apolloConfiguration

2次阅读

共计 3173 个字符,预计需要花费 8 分钟才能阅读完成。

本文次要钻研一下 dubbo-go 的 apolloConfiguration

apolloConfiguration

dubbo-go-v1.4.2/config_center/apollo/impl.go

const (
    apolloProtocolPrefix = "http://"
    apolloConfigFormat   = "%s.%s"
)

type apolloConfiguration struct {
    url *common.URL

    listeners sync.Map
    appConf   *agollo.AppConfig
    parser    parser.ConfigurationParser
}
  • apolloConfiguration 定义了 url、listeners、appConf、parser 属性

newApolloConfiguration

dubbo-go-v1.4.2/config_center/apollo/impl.go

func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) {
    c := &apolloConfiguration{url: url,}
    configAddr := c.getAddressWithProtocolPrefix(url)
    configCluster := url.GetParam(constant.CONFIG_CLUSTER_KEY, "")

    appId := url.GetParam(constant.CONFIG_APP_ID_KEY, "")
    namespaces := getProperties(url.GetParam(constant.CONFIG_NAMESPACE_KEY, cc.DEFAULT_GROUP))
    c.appConf = &agollo.AppConfig{
        AppId:         appId,
        Cluster:       configCluster,
        NamespaceName: namespaces,
        Ip:            configAddr,
    }

    agollo.InitCustomConfig(func() (*agollo.AppConfig, error) {return c.appConf, nil})

    return c, agollo.Start()}
  • newApolloConfiguration 办法创立 AppConfig,而后执行 agollo.InitCustomConfig,最初执行 agollo.Start()

GetProperties

dubbo-go-v1.4.2/config_center/apollo/impl.go

func (c *apolloConfiguration) GetProperties(key string, opts ...cc.Option) (string, error) {
    /**
     * when group is not null, we are getting startup configs(config file) from Config Center, for example:
     * key=dubbo.propertie
     */
    config := agollo.GetConfig(key)
    if config == nil {return "", errors.New(fmt.Sprintf("nothing in namespace:%s ", key))
    }
    return config.GetContent(agollo.Properties), nil
}
  • GetProperties 办法先执行 agollo.GetConfig(key) 获取 Config,之后通过 config.GetContent(agollo.Properties) 获取属性

AddListener

dubbo-go-v1.4.2/config_center/apollo/impl.go

func (c *apolloConfiguration) AddListener(key string, listener cc.ConfigurationListener, opts ...cc.Option) {k := &cc.Options{}
    for _, opt := range opts {opt(k)
    }

    key = k.Group + key
    l, _ := c.listeners.LoadOrStore(key, NewApolloListener())
    l.(*apolloListener).AddListener(listener)
}
  • AddListener 办法执行 c.listeners.LoadOrStore(key, NewApolloListener()) 及 l.(*apolloListener).AddListener(listener)

RemoveListener

dubbo-go-v1.4.2/config_center/apollo/impl.go

func (c *apolloConfiguration) RemoveListener(key string, listener cc.ConfigurationListener, opts ...cc.Option) {k := &cc.Options{}
    for _, opt := range opts {opt(k)
    }

    key = k.Group + key
    l, ok := c.listeners.Load(key)
    if ok {l.(*apolloListener).RemoveListener(listener)
    }
}
  • RemoveListener 办法执行 l.(*apolloListener).RemoveListener(listener)

NewApolloListener

dubbo-go-v1.4.2/config_center/apollo/listener.go

type apolloListener struct {listeners map[config_center.ConfigurationListener]struct{}}

// NewApolloListener ...
func NewApolloListener() *apolloListener {
    return &apolloListener{listeners: make(map[config_center.ConfigurationListener]struct{}, 0),
    }
}
  • NewApolloListener 创立 apolloListener

OnChange

dubbo-go-v1.4.2/config_center/apollo/listener.go

// OnChange ...
func (a *apolloListener) OnChange(changeEvent *agollo.ChangeEvent) {
    for key, change := range changeEvent.Changes {
        for listener := range a.listeners {
            listener.Process(&config_center.ConfigChangeEvent{ConfigType: getChangeType(change.ChangeType),
                Key:        key,
                Value:      change.NewValue,
            })
        }
    }
}
  • OnChange 办法接管 ChangeEvent,之后遍历 listeners,执行 listener.Process 回调

小结

apolloConfiguration 定义了 url、listeners、appConf、parser 属性;newApolloConfiguration 办法创立 AppConfig,而后执行 agollo.InitCustomConfig,最初执行 agollo.Start()

doc

  • apollo/impl.go
正文完
 0