You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by as...@apache.org on 2018/10/23 03:33:09 UTC

[incubator-servicecomb-service-center] branch master updated: SCB-972 Using Sha1 signature key as UUID of micro service (#460)

This is an automated email from the ASF dual-hosted git repository.

asifdxtreme pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new bd59e8b  SCB-972 Using Sha1 signature key as UUID of micro service (#460)
bd59e8b is described below

commit bd59e8bf98179b49c997f10ed4f529b2d1c0afcf
Author: little-cui <su...@qq.com>
AuthorDate: Tue Oct 23 11:33:04 2018 +0800

    SCB-972 Using Sha1 signature key as UUID of micro service (#460)
    
    * SCB-972 Using Sha1 signature key as UUID of micro service
    
    * Fix the IT failure
---
 etc/conf/app.conf                                  |  3 ++
 integration/governance_test.go                     |  6 ++--
 integration/instances_test.go                      |  8 ++---
 integration/integrationtest_suite_test.go          |  2 ++
 integration/microservices_test.go                  |  6 ++--
 integration/rules_test.go                          |  2 +-
 integration/tags_test.go                           |  2 +-
 server/bootstrap/bootstrap.go                      |  1 +
 server/plugin/pkg/uuid/buildin/buildin.go          |  5 ++--
 .../{buildin/buildin.go => context/context.go}     | 34 +++++++++++++---------
 server/plugin/pkg/uuid/uuid.go                     |  8 +++--
 server/service/instance.go                         |  2 +-
 server/service/microservice.go                     |  8 +++--
 13 files changed, 54 insertions(+), 33 deletions(-)

diff --git a/etc/conf/app.conf b/etc/conf/app.conf
index df4128c..38b6690 100644
--- a/etc/conf/app.conf
+++ b/etc/conf/app.conf
@@ -108,6 +108,9 @@ auditlog_plugin = ""
 #                   in path specified by TRACING_FILE_PATH env variable
 trace_plugin = ""
 
+#customize the uuid format
+uuid_plugin = "context"
+
 ###################################################################
 # rate limit options
 ###################################################################
diff --git a/integration/governance_test.go b/integration/governance_test.go
index 2f592d7..e8c0360 100644
--- a/integration/governance_test.go
+++ b/integration/governance_test.go
@@ -66,7 +66,7 @@ var _ = Describe("MicroService Api Test", func() {
 			Expect(resp.StatusCode).To(Equal(http.StatusOK))
 			respbody, _ := ioutil.ReadAll(resp.Body)
 			serviceId = gojson.Json(string(respbody)).Get("serviceId").Tostring()
-			Expect(len(serviceId)).Should(BeNumerically("==", 32))
+			Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 			//Register MicroService Instance
 			endpoints := []string{"cse://127.0.0.1:9984"}
@@ -105,7 +105,7 @@ var _ = Describe("MicroService Api Test", func() {
 			Expect(resp.StatusCode).To(Equal(http.StatusOK))
 			respbody, _ = ioutil.ReadAll(resp.Body)
 			serviceInstanceID = gojson.Json(string(respbody)).Get("instanceId").Tostring()
-			Expect(len(serviceId)).Should(BeNumerically("==", 32))
+			Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 		})
 
@@ -259,7 +259,7 @@ func BenchmarkGovernance(b *testing.B) {
 	Expect(resp.StatusCode).To(Equal(http.StatusOK))
 	respbody, _ := ioutil.ReadAll(resp.Body)
 	serviceId := gojson.Json(string(respbody)).Get("serviceId").Tostring()
-	Expect(len(serviceId)).Should(BeNumerically("==", 32))
+	Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 	for i := 0; i < b.N; i++ {
 		url := strings.Replace(GETGOVERNANCESERVICEDETAILS, ":serviceId", serviceId, 1)
diff --git a/integration/instances_test.go b/integration/instances_test.go
index 458c45d..0d675ee 100644
--- a/integration/instances_test.go
+++ b/integration/instances_test.go
@@ -69,7 +69,7 @@ var _ = Describe("MicroService Api Test", func() {
 			Expect(resp.StatusCode).To(Equal(http.StatusOK))
 			respbody, _ := ioutil.ReadAll(resp.Body)
 			serviceId = gojson.Json(string(respbody)).Get("serviceId").Tostring()
-			Expect(len(serviceId)).Should(BeNumerically("==", 32))
+			Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 			//Register MicroService Instance
 			endpoints := []string{"cse://127.0.0.1:9984"}
@@ -108,7 +108,7 @@ var _ = Describe("MicroService Api Test", func() {
 			Expect(resp.StatusCode).To(Equal(http.StatusOK))
 			respbody, _ = ioutil.ReadAll(resp.Body)
 			serviceInstanceID = gojson.Json(string(respbody)).Get("instanceId").Tostring()
-			Expect(len(serviceId)).Should(BeNumerically("==", 32))
+			Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 		})
 
@@ -559,7 +559,7 @@ func BenchmarkRegisterMicroServiceInstance(b *testing.B) {
 	Expect(resp.StatusCode).To(Equal(http.StatusOK))
 	respbody, _ := ioutil.ReadAll(resp.Body)
 	serviceId := gojson.Json(string(respbody)).Get("serviceId").Tostring()
-	Expect(len(serviceId)).Should(BeNumerically("==", 32))
+	Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 	for i := 0; i < b.N; i++ {
 		//Register MicroService Instance
@@ -599,7 +599,7 @@ func BenchmarkRegisterMicroServiceInstance(b *testing.B) {
 		Expect(resp.StatusCode).To(Equal(http.StatusOK))
 		respbody, _ = ioutil.ReadAll(resp.Body)
 		serviceInstanceID := gojson.Json(string(respbody)).Get("instanceId").Tostring()
-		Expect(len(serviceId)).Should(BeNumerically("==", 32))
+		Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 		if serviceInstanceID != "" {
 			url := strings.Replace(UNREGISTERINSTANCE, ":serviceId", serviceId, 1)
diff --git a/integration/integrationtest_suite_test.go b/integration/integrationtest_suite_test.go
index ae25b96..d5a2d83 100644
--- a/integration/integrationtest_suite_test.go
+++ b/integration/integrationtest_suite_test.go
@@ -25,6 +25,8 @@ import (
 	"testing"
 )
 
+const LengthUUID = 40
+
 var scclient *http.Client
 
 var insecurityConnection = &http.Client{}
diff --git a/integration/microservices_test.go b/integration/microservices_test.go
index 0361275..fd995a2 100644
--- a/integration/microservices_test.go
+++ b/integration/microservices_test.go
@@ -70,7 +70,7 @@ var _ = Describe("MicroService Api Test", func() {
 				Expect(resp.StatusCode).To(Equal(http.StatusOK))
 				respbody, _ := ioutil.ReadAll(resp.Body)
 				serviceId = gojson.Json(string(respbody)).Get("serviceId").Tostring()
-				Expect(len(serviceId)).Should(BeNumerically("==", 32))
+				Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 
 				// UNRegister Service
 				url := strings.Replace(UNREGISTERMICROSERVICE, ":serviceId", serviceId, 1)
@@ -112,7 +112,7 @@ var _ = Describe("MicroService Api Test", func() {
 				Expect(resp.StatusCode).To(Equal(http.StatusOK))
 				respbody, _ := ioutil.ReadAll(resp.Body)
 				serviceId = gojson.Json(string(respbody)).Get("serviceId").Tostring()
-				Expect(len(serviceId)).Should(BeNumerically("==", 32))
+				Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 			})
 
 			AfterEach(func() {
@@ -573,7 +573,7 @@ func BenchmarkRegisterMicroServiceAndDelete(b *testing.B) {
 		Expect(resp.StatusCode).To(Equal(http.StatusOK))
 		respbody, _ := ioutil.ReadAll(resp.Body)
 		serviceId := gojson.Json(string(respbody)).Get("serviceId").Tostring()
-		Expect(len(serviceId)).Should(BeNumerically("==", 32))
+		Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 		if serviceId != "" {
 			url := strings.Replace(UNREGISTERMICROSERVICE, ":serviceId", serviceId, 1)
 			req, _ := http.NewRequest(DELETE, SCURL+url, nil)
diff --git a/integration/rules_test.go b/integration/rules_test.go
index 13543c3..78a478f 100644
--- a/integration/rules_test.go
+++ b/integration/rules_test.go
@@ -66,7 +66,7 @@ var _ = Describe("MicroService Api Test", func() {
 			Expect(resp.StatusCode).To(Equal(http.StatusOK))
 			respbody, _ := ioutil.ReadAll(resp.Body)
 			serviceId = gojson.Json(string(respbody)).Get("serviceId").Tostring()
-			Expect(len(serviceId)).Should(BeNumerically("==", 32))
+			Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 		})
 
 		AfterEach(func() {
diff --git a/integration/tags_test.go b/integration/tags_test.go
index ba96a7c..85cf1e1 100644
--- a/integration/tags_test.go
+++ b/integration/tags_test.go
@@ -65,7 +65,7 @@ var _ = Describe("MicroService Api Test", func() {
 			Expect(resp.StatusCode).To(Equal(http.StatusOK))
 			respbody, _ := ioutil.ReadAll(resp.Body)
 			serviceId = gojson.Json(string(respbody)).Get("serviceId").Tostring()
-			Expect(len(serviceId)).Should(BeNumerically("==", 32))
+			Expect(len(serviceId)).Should(BeNumerically("==", LengthUUID))
 		})
 
 		AfterEach(func() {
diff --git a/server/bootstrap/bootstrap.go b/server/bootstrap/bootstrap.go
index 2f80cc0..531ade9 100644
--- a/server/bootstrap/bootstrap.go
+++ b/server/bootstrap/bootstrap.go
@@ -43,6 +43,7 @@ import _ "github.com/apache/incubator-servicecomb-service-center/server/plugin/p
 
 // uuid
 import _ "github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/uuid/buildin"
+import _ "github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/uuid/context"
 
 // tracing
 import _ "github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/tracing/buildin"
diff --git a/server/plugin/pkg/uuid/buildin/buildin.go b/server/plugin/pkg/uuid/buildin/buildin.go
index b2796ea..8218e14 100644
--- a/server/plugin/pkg/uuid/buildin/buildin.go
+++ b/server/plugin/pkg/uuid/buildin/buildin.go
@@ -20,6 +20,7 @@ package buildin
 import (
 	"github.com/apache/incubator-servicecomb-service-center/pkg/util"
 	mgr "github.com/apache/incubator-servicecomb-service-center/server/plugin"
+	"golang.org/x/net/context"
 )
 
 func init() {
@@ -33,7 +34,7 @@ func New() mgr.PluginInstance {
 type BuildinUUID struct {
 }
 
-func (du *BuildinUUID) GetServiceId() string {
+func (du *BuildinUUID) GetServiceId(_ context.Context) string {
 	df, ok := mgr.DynamicPluginFunc(mgr.UUID, "GetServiceId").(func() string)
 	if ok {
 		return df()
@@ -41,7 +42,7 @@ func (du *BuildinUUID) GetServiceId() string {
 	return util.GenerateUuid()
 }
 
-func (du *BuildinUUID) GetInstanceId() string {
+func (du *BuildinUUID) GetInstanceId(_ context.Context) string {
 	df, ok := mgr.DynamicPluginFunc(mgr.UUID, "GetInstanceId").(func() string)
 	if ok {
 		return df()
diff --git a/server/plugin/pkg/uuid/buildin/buildin.go b/server/plugin/pkg/uuid/context/context.go
similarity index 57%
copy from server/plugin/pkg/uuid/buildin/buildin.go
copy to server/plugin/pkg/uuid/context/context.go
index b2796ea..9ab26e9 100644
--- a/server/plugin/pkg/uuid/buildin/buildin.go
+++ b/server/plugin/pkg/uuid/context/context.go
@@ -15,36 +15,42 @@
  * limitations under the License.
  */
 
-package buildin
+package context
 
 import (
+	"crypto/sha1"
+	"fmt"
 	"github.com/apache/incubator-servicecomb-service-center/pkg/util"
 	mgr "github.com/apache/incubator-servicecomb-service-center/server/plugin"
+	"github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/uuid"
+	"github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/uuid/buildin"
+	"golang.org/x/net/context"
 )
 
 func init() {
-	mgr.RegisterPlugin(mgr.Plugin{mgr.UUID, "buildin", New})
+	mgr.RegisterPlugin(mgr.Plugin{mgr.UUID, "context", New})
 }
 
 func New() mgr.PluginInstance {
-	return &BuildinUUID{}
+	return &ContextUUID{}
 }
 
-type BuildinUUID struct {
+type ContextUUID struct {
+	buildin.BuildinUUID
 }
 
-func (du *BuildinUUID) GetServiceId() string {
-	df, ok := mgr.DynamicPluginFunc(mgr.UUID, "GetServiceId").(func() string)
-	if ok {
-		return df()
+func (cu *ContextUUID) fromContext(ctx context.Context) string {
+	key, ok := ctx.Value(uuid.ContextKey).(string)
+	if !ok {
+		return ""
 	}
-	return util.GenerateUuid()
+	return key
 }
 
-func (du *BuildinUUID) GetInstanceId() string {
-	df, ok := mgr.DynamicPluginFunc(mgr.UUID, "GetInstanceId").(func() string)
-	if ok {
-		return df()
+func (cu *ContextUUID) GetServiceId(ctx context.Context) string {
+	content := cu.fromContext(ctx)
+	if len(content) == 0 {
+		return cu.BuildinUUID.GetServiceId(ctx)
 	}
-	return util.GenerateUuid()
+	return fmt.Sprintf("%x", sha1.Sum(util.StringToBytesWithNoCopy(content)))
 }
diff --git a/server/plugin/pkg/uuid/uuid.go b/server/plugin/pkg/uuid/uuid.go
index 678ec30..08bab8e 100644
--- a/server/plugin/pkg/uuid/uuid.go
+++ b/server/plugin/pkg/uuid/uuid.go
@@ -16,7 +16,11 @@
  */
 package uuid
 
+import "golang.org/x/net/context"
+
+const ContextKey = "_uuid_key"
+
 type UUID interface {
-	GetServiceId() string
-	GetInstanceId() string
+	GetServiceId(ctx context.Context) string
+	GetInstanceId(ctx context.Context) string
 }
diff --git a/server/service/instance.go b/server/service/instance.go
index 9ee509d..54eef40 100644
--- a/server/service/instance.go
+++ b/server/service/instance.go
@@ -47,7 +47,7 @@ func (s *InstanceService) preProcessRegisterInstance(ctx context.Context, instan
 	}
 
 	if len(instance.InstanceId) == 0 {
-		instance.InstanceId = plugin.Plugins().UUID().GetInstanceId()
+		instance.InstanceId = plugin.Plugins().UUID().GetInstanceId(ctx)
 	}
 
 	instance.Timestamp = strconv.FormatInt(time.Now().Unix(), 10)
diff --git a/server/service/microservice.go b/server/service/microservice.go
index 60c40f1..b0b36e1 100644
--- a/server/service/microservice.go
+++ b/server/service/microservice.go
@@ -31,6 +31,7 @@ import (
 	"github.com/apache/incubator-servicecomb-service-center/server/plugin"
 	"github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/quota"
 	"github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/registry"
+	"github.com/apache/incubator-servicecomb-service-center/server/plugin/pkg/uuid"
 	serviceUtil "github.com/apache/incubator-servicecomb-service-center/server/service/util"
 	"golang.org/x/net/context"
 	"strconv"
@@ -110,10 +111,13 @@ func (s *MicroServiceService) CreateServicePri(ctx context.Context, in *pb.Creat
 		return resp, nil
 	}
 
+	index := apt.GenerateServiceIndexKey(serviceKey)
+
 	// 产生全局service id
 	requestServiceId := service.ServiceId
 	if len(requestServiceId) == 0 {
-		service.ServiceId = plugin.Plugins().UUID().GetServiceId()
+		ctx = util.SetContext(ctx, uuid.ContextKey, index)
+		service.ServiceId = plugin.Plugins().UUID().GetServiceId(ctx)
 	}
 	service.Timestamp = strconv.FormatInt(time.Now().Unix(), 10)
 	service.ModTimestamp = service.Timestamp
@@ -126,9 +130,9 @@ func (s *MicroServiceService) CreateServicePri(ctx context.Context, in *pb.Creat
 			Response: pb.CreateResponse(scerr.ErrInternal, err.Error()),
 		}, err
 	}
+
 	key := apt.GenerateServiceKey(domainProject, service.ServiceId)
 	keyBytes := util.StringToBytesWithNoCopy(key)
-	index := apt.GenerateServiceIndexKey(serviceKey)
 	indexBytes := util.StringToBytesWithNoCopy(index)
 	aliasBytes := util.StringToBytesWithNoCopy(apt.GenerateServiceAliasKey(serviceKey))