From 682f4059dbf77edc8cbd0b718cdd350b3f805dee Mon Sep 17 00:00:00 2001
From: flycash <flycash@apache.org>
Date: Fri, 1 May 2020 22:39:47 +0800
Subject: [PATCH] Add dynamic

---
 .../mapping/dynamic/service_name_mapping.go   | 29 +++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/metadata/mapping/dynamic/service_name_mapping.go b/metadata/mapping/dynamic/service_name_mapping.go
index 4cfac8f82..5e4069290 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
+}
-- 
GitLab