序
本文主要研究一下dubbo-go的apolloConfiguration
apolloConfiguration
dubbo-go-v1.4.2/config_center/apollo/impl.go
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制// 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