[ 
https://issues.apache.org/jira/browse/SCB-972?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16660025#comment-16660025
 ] 

ASF GitHub Bot commented on SCB-972:
------------------------------------

asifdxtreme closed pull request #460: SCB-972 Using Sha1 signature key as UUID 
of micro service
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/460
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/etc/conf/app.conf b/etc/conf/app.conf
index df4128c4..38b6690a 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 2f592d7b..e8c0360a 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 458c45d1..0d675ee4 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 ae25b96d..d5a2d837 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 03612753..fd995a25 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 13543c37..78a478f5 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 ba96a7c8..85cf1e12 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 2f80cc0a..531ade91 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 b2796ea5..8218e14e 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/context/context.go 
b/server/plugin/pkg/uuid/context/context.go
new file mode 100644
index 00000000..9ab26e9c
--- /dev/null
+++ b/server/plugin/pkg/uuid/context/context.go
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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, "context", New})
+}
+
+func New() mgr.PluginInstance {
+       return &ContextUUID{}
+}
+
+type ContextUUID struct {
+       buildin.BuildinUUID
+}
+
+func (cu *ContextUUID) fromContext(ctx context.Context) string {
+       key, ok := ctx.Value(uuid.ContextKey).(string)
+       if !ok {
+               return ""
+       }
+       return key
+}
+
+func (cu *ContextUUID) GetServiceId(ctx context.Context) string {
+       content := cu.fromContext(ctx)
+       if len(content) == 0 {
+               return cu.BuildinUUID.GetServiceId(ctx)
+       }
+       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 678ec301..08bab8e6 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 9ee509d6..54eef407 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 60c40f13..b0b36e14 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))
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Using SHA1 signature key as UUID of micro service
> -------------------------------------------------
>
>                 Key: SCB-972
>                 URL: https://issues.apache.org/jira/browse/SCB-972
>             Project: Apache ServiceComb
>          Issue Type: Bug
>          Components: Service-Center
>            Reporter: little-cui
>            Assignee: little-cui
>            Priority: Major
>             Fix For: java-chassis-1.1.0
>
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to