聊聊dubbo-go的apolloConfiguration

/ distributedgolang / 没有评论 / 0浏览

本文主要研究一下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
}

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()
}

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
}

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)
}

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)
	}
}

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),
	}
}

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,
			})
		}
	}
}

小结

apolloConfiguration定义了url、listeners、appConf、parser属性;newApolloConfiguration方法创建AppConfig,然后执行agollo.InitCustomConfig,最后执行agollo.Start()

doc