diff --git a/metadata/mapping/dynamic/service_name_mapping.go b/metadata/mapping/dynamic/service_name_mapping.go index 4cfac8f82887d0b101beaf55ae9ca84c124d30b3..5e406929050e5cfd388d2a69bb2f9d6e4cc229af 100644 --- a/metadata/mapping/dynamic/service_name_mapping.go +++ b/metadata/mapping/dynamic/service_name_mapping.go @@ -19,6 +19,7 @@ package dynamic import ( "strconv" + "sync" "time" ) @@ -28,7 +29,9 @@ import ( ) import ( + env "github.com/apache/dubbo-go/common/config" "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/config" "github.com/apache/dubbo-go/config_center" "github.com/apache/dubbo-go/metadata/mapping" @@ -37,9 +40,16 @@ import ( const ( defaultGroup = config_center.DEFAULT_GROUP slash = "/" + name = "dynamic" ) +func init() { + extension.SetServiceNameMapping(name, GetServiceNameMappingInstance) + extension.SetServiceNameMapping(constant.DEFAULT_KEY, GetServiceNameMappingInstance) +} + // DynamicConfigurationServiceNameMapping is the implementation based on config center +// It could be thought as singleton pattern. type DynamicConfigurationServiceNameMapping struct { dc config_center.DynamicConfiguration } @@ -76,7 +86,22 @@ func (d *DynamicConfigurationServiceNameMapping) buildGroup(serviceInterface str return defaultGroup + slash + serviceInterface } -// NewServiceNameMapping will create an instance of DynamicConfigurationServiceNameMapping -func NewServiceNameMapping(dc config_center.DynamicConfiguration) mapping.ServiceNameMapping { +var ( + instance *DynamicConfigurationServiceNameMapping + initOnce sync.Once +) + +// newServiceNameMapping will create an instance of DynamicConfigurationServiceNameMapping +func newServiceNameMapping(dc config_center.DynamicConfiguration) *DynamicConfigurationServiceNameMapping { return &DynamicConfigurationServiceNameMapping{dc: dc} } + +// GetServiceNameMappingInstance will return the instance. +// If the instance is not initiated, it will create one +func GetServiceNameMappingInstance() mapping.ServiceNameMapping { + initOnce.Do(func() { + dc := env.GetEnvInstance().GetDynamicConfiguration() + instance = newServiceNameMapping(dc) + }) + return instance +}