sequence
This article mainly studies apolloConfiguration of Dubo-Go
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
}
Copy the code
- ApolloConfiguration defines URL, Listeners, appConf, and Parser attributes
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()
}
Copy the code
- The newApolloConfiguration method creates AppConfig, then agollo.InitCustomConfig, and finally 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 }Copy the code
- GetProperties executes agollo.GetConfig(key) to GetConfig, and config.getcontent (agollo.Properties) to GetProperties
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) }Copy the code
- LoadOrStore(key, NewApolloListener()) and 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) } }Copy the code
- (*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),
}
}
Copy the code
- NewApolloListener create 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,
})
}
}
}
Copy the code
- The OnChange method receives a ChangeEvent and then iterates over the Listeners, executing the listener.process callback
summary
ApolloConfiguration defines URL, Listeners, appConf, and Parser attributes. The newApolloConfiguration method creates AppConfig, then agollo.InitCustomConfig, and finally agollo.Start()
doc
- apollo/impl.go