You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2020/09/15 03:10:01 UTC
[servicecomb-service-center] branch master updated: Fix bugs on
running in Syncer registry mode of servicecenter & join mechanism (#693)
This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
The following commit(s) were added to refs/heads/master by this push:
new b06608e Fix bugs on running in Syncer registry mode of servicecenter & join mechanism (#693)
b06608e is described below
commit b06608e32828d3810be3f6b38e4a1a481ac75fc0
Author: jasononion <ja...@gmail.com>
AuthorDate: Tue Sep 15 11:09:52 2020 +0800
Fix bugs on running in Syncer registry mode of servicecenter & join mechanism (#693)
* Upgrade go-chassis to v2.0.0
* Modify version info for go-chassis 2.0.0
* Fix bug when run in registry mode of servicecenter & add join mechanism
* Add license info for registry.pb.go
* Rename joinAddr and move FromDomainProject into pkg/util
---
go.sum | 4 +
pkg/registry/registry.pb.go | 34 +++
pkg/util/util.go | 12 +
scctl/pkg/plugin/get/instance/printer.go | 3 +-
scctl/pkg/plugin/get/schema/schema_cmd.go | 3 +-
scctl/pkg/plugin/get/service/printer.go | 3 +-
server/core/key_convertor.go | 12 -
syncer/plugins/servicecenter/instance.go | 56 ++++
syncer/plugins/servicecenter/instance_test.go | 72 +++++
syncer/plugins/servicecenter/schemas.go | 42 +++
syncer/plugins/servicecenter/service.go | 79 ++++++
syncer/plugins/servicecenter/service_test.go | 68 +++++
syncer/plugins/servicecenter/servicecenter.go | 85 ++++++
syncer/plugins/servicecenter/servicecenter_test.go | 57 ++++
syncer/plugins/servicecenter/transform.go | 289 +++++++++++++++++++++
syncer/serf/serf.go | 10 +-
syncer/serf/serf_test.go | 2 +
syncer/server/server.go | 3 +-
18 files changed, 815 insertions(+), 19 deletions(-)
diff --git a/go.sum b/go.sum
index 87ef0ae..3a8302f 100644
--- a/go.sum
+++ b/go.sum
@@ -96,6 +96,8 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/
github.com/go-chassis/foundation v0.0.0-20190621030543-c3b63f787f4c/go.mod h1:21/ajGtgJlWTCeM0TxGJdRhO8bJkKirWyV8Stlh6g6c=
github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4 h1:wx8JXvg/n4i8acXsBJ5zIkiK7EO2kn/HuEjKK3kSgv8=
github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4/go.mod h1:21/ajGtgJlWTCeM0TxGJdRhO8bJkKirWyV8Stlh6g6c=
+github.com/go-chassis/foundation v0.1.1-0.20200825060850-b16bf420f7b3 h1:c+bwT0qLY69jSU8TmzuNcb9UL/QFAiU96kjuX5TMiQc=
+github.com/go-chassis/foundation v0.1.1-0.20200825060850-b16bf420f7b3/go.mod h1:21/ajGtgJlWTCeM0TxGJdRhO8bJkKirWyV8Stlh6g6c=
github.com/go-chassis/glog v0.0.0-20180920075250-95a09b2413e9 h1:z6ezGZIJVMl2Yo0gOJlv8jOvCDFi9W3TLADQvJ6Z2t0=
github.com/go-chassis/glog v0.0.0-20180920075250-95a09b2413e9/go.mod h1:tfdQUl8deEC8YmpbYjbSZJv7Y/7hC0ag9n3RVrjIoJs=
github.com/go-chassis/go-archaius v0.20.0/go.mod h1:lMsxSkFbBju4zrc9DTvRc5uZZNUl7sqzJkzlggP7skg=
@@ -131,6 +133,8 @@ github.com/go-chassis/go-chassis v0.0.0-20200716073336-4183d5dd1246 h1:yxtoclu0Q
github.com/go-chassis/go-chassis v0.0.0-20200716073336-4183d5dd1246/go.mod h1:S7RbnpeTWBqZ4+UxGAs05a6qdhDyWKpTNuil8FYNiME=
github.com/go-chassis/go-chassis v0.0.0-20200820073047-a1c634b7bdd1 h1:eAGcefnlCeBi8+01Bokh6nmRR/PjrC3QCqPRUarv6dw=
github.com/go-chassis/go-chassis v0.0.0-20200820073047-a1c634b7bdd1/go.mod h1:nMWwWrgDEyDKnMQLE5Gt65RdwbgrQg5amt4BqNFY09g=
+github.com/go-chassis/go-chassis v0.0.0-20200826064053-d90be848aa10 h1:eImN6UFx4pksyxJQnT1laHoVOwSlhEC1ONhJQc4Ix08=
+github.com/go-chassis/go-chassis v0.0.0-20200826064053-d90be848aa10/go.mod h1:ZzLCaQFgFHbkwf8grJrbzBLlE19iz+4LMgNtjGlnanU=
github.com/go-chassis/go-chassis v1.8.1/go.mod h1:vI0rU2FNAtGi6owfYKXBVj6cvq633/n+8bqbsVfib7E=
github.com/go-chassis/go-chassis v1.8.2-0.20200620015240-3621ca234c7c h1:Bu4xdnLMGYmupX5E6dV34fFOvEo3p0trcKQsThqyXAA=
github.com/go-chassis/go-chassis v1.8.2-0.20200620015240-3621ca234c7c/go.mod h1:eQpXV6jY4aWolimDvC5jYzwYlxhlyO3fByfrXTbFN/g=
diff --git a/pkg/registry/registry.pb.go b/pkg/registry/registry.pb.go
new file mode 100644
index 0000000..f582b99
--- /dev/null
+++ b/pkg/registry/registry.pb.go
@@ -0,0 +1,34 @@
+/*
+ * 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 registry
+
+import (
+ "github.com/gogo/protobuf/proto"
+)
+
+func (m *Schema) Reset() { *m = Schema{} }
+func (m *Schema) String() string { return proto.CompactTextString(m) }
+func (*Schema) ProtoMessage() {}
+
+func (m *MicroService) Reset() { *m = MicroService{} }
+func (m *MicroService) String() string { return proto.CompactTextString(m) }
+func (*MicroService) ProtoMessage() {}
+
+func (m *MicroServiceInstance) Reset() { *m = MicroServiceInstance{} }
+func (m *MicroServiceInstance) String() string { return proto.CompactTextString(m) }
+func (*MicroServiceInstance) ProtoMessage() {}
diff --git a/pkg/util/util.go b/pkg/util/util.go
index 65fd822..a3f271b 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -156,3 +156,15 @@ func StringTRUE(s string) bool {
}
return false
}
+
+func FromDomainProject(domainProject string) (domain, project string) {
+ if i := strings.Index(domainProject, "/"); i >= 0 {
+ return domainProject[:i], domainProject[i+1:]
+ }
+ return domainProject, ""
+}
+
+func ToDomainProject(domain, project string) (domainProject string) {
+ domainProject = domain + "/" + project
+ return
+}
diff --git a/scctl/pkg/plugin/get/instance/printer.go b/scctl/pkg/plugin/get/instance/printer.go
index 3ea507b..a6bde87 100644
--- a/scctl/pkg/plugin/get/instance/printer.go
+++ b/scctl/pkg/plugin/get/instance/printer.go
@@ -19,7 +19,6 @@ import (
"github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/scctl/pkg/model"
"github.com/apache/servicecomb-service-center/scctl/pkg/writer"
- "github.com/apache/servicecomb-service-center/server/core"
"time"
)
@@ -61,7 +60,7 @@ func (s *InstanceRecord) AgeString() string {
}
func (s *InstanceRecord) Domain() string {
- domain, _ := core.FromDomainProject(s.DomainProject)
+ domain, _ := util.FromDomainProject(s.DomainProject)
return domain
}
diff --git a/scctl/pkg/plugin/get/schema/schema_cmd.go b/scctl/pkg/plugin/get/schema/schema_cmd.go
index ade2b9b..97b1941 100644
--- a/scctl/pkg/plugin/get/schema/schema_cmd.go
+++ b/scctl/pkg/plugin/get/schema/schema_cmd.go
@@ -20,6 +20,7 @@ import (
"fmt"
"github.com/apache/servicecomb-service-center/client"
model2 "github.com/apache/servicecomb-service-center/pkg/model"
+ "github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/scctl/pkg/cmd"
"github.com/apache/servicecomb-service-center/scctl/pkg/model"
"github.com/apache/servicecomb-service-center/scctl/pkg/plugin/get"
@@ -66,7 +67,7 @@ func saveDirectory(root string, ms *model2.Microservice) string {
if len(root) == 0 {
return ""
}
- domain, project := core.FromDomainProject(model.GetDomainProject(ms))
+ domain, project := util.FromDomainProject(model.GetDomainProject(ms))
if domain == core.RegistryDomain {
domain = ""
}
diff --git a/scctl/pkg/plugin/get/service/printer.go b/scctl/pkg/plugin/get/service/printer.go
index a80beef..5143d0f 100644
--- a/scctl/pkg/plugin/get/service/printer.go
+++ b/scctl/pkg/plugin/get/service/printer.go
@@ -19,7 +19,6 @@ import (
"github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/scctl/pkg/model"
"github.com/apache/servicecomb-service-center/scctl/pkg/writer"
- "github.com/apache/servicecomb-service-center/server/core"
)
const maxWidth = 35
@@ -55,7 +54,7 @@ func (s *ServiceRecord) AgeString() string {
}
func (s *ServiceRecord) Domain() string {
- domain, _ := core.FromDomainProject(s.DomainProject)
+ domain, _ := util.FromDomainProject(s.DomainProject)
return domain
}
diff --git a/server/core/key_convertor.go b/server/core/key_convertor.go
index 57fb513..d0efac7 100644
--- a/server/core/key_convertor.go
+++ b/server/core/key_convertor.go
@@ -166,15 +166,3 @@ func GetInfoFromDependencyRuleKV(key []byte) (t string, _ *registry.MicroService
Version: keys[l-1],
}
}
-
-func FromDomainProject(domainProject string) (domain, project string) {
- if i := strings.Index(domainProject, "/"); i >= 0 {
- return domainProject[:i], domainProject[i+1:]
- }
- return domainProject, ""
-}
-
-func ToDomainProject(domain, project string) (domainProject string) {
- domainProject = domain + "/" + project
- return
-}
diff --git a/syncer/plugins/servicecenter/instance.go b/syncer/plugins/servicecenter/instance.go
new file mode 100644
index 0000000..1837d38
--- /dev/null
+++ b/syncer/plugins/servicecenter/instance.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 servicecenter
+
+import (
+ "context"
+ "github.com/apache/servicecomb-service-center/pkg/util"
+ pb "github.com/apache/servicecomb-service-center/syncer/proto"
+)
+
+// RegisterInstance register instance to servicecenter
+func (c *Client) RegisterInstance(ctx context.Context, domainProject, serviceId string, syncInstance *pb.SyncInstance) (string, error) {
+ instance := toInstance(syncInstance)
+ instance.InstanceId = ""
+ instance.ServiceId = serviceId
+ domain, project := util.FromDomainProject(domainProject)
+ instanceID, err := c.cli.RegisterInstance(ctx, domain, project, serviceId, instance)
+ if err != nil {
+ return "", err
+ }
+ return instanceID, nil
+}
+
+// UnregisterInstance unregister instance from servicecenter
+func (c *Client) UnregisterInstance(ctx context.Context, domainProject, serviceId, instanceId string) error {
+ domain, project := util.FromDomainProject(domainProject)
+ err := c.cli.UnregisterInstance(ctx, domain, project, serviceId, instanceId)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+// Heartbeat sends heartbeat to servicecenter
+func (c *Client) Heartbeat(ctx context.Context, domainProject, serviceId, instanceId string) error {
+ domain, project := util.FromDomainProject(domainProject)
+ err := c.cli.Heartbeat(ctx, domain, project, serviceId, instanceId)
+ if err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/syncer/plugins/servicecenter/instance_test.go b/syncer/plugins/servicecenter/instance_test.go
new file mode 100644
index 0000000..726eca3
--- /dev/null
+++ b/syncer/plugins/servicecenter/instance_test.go
@@ -0,0 +1,72 @@
+/*
+ * 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 servicecenter
+
+import (
+ "context"
+ "testing"
+
+ pb "github.com/apache/servicecomb-service-center/syncer/proto"
+)
+
+func TestClient_RegisterInstance(t *testing.T) {
+ svr, repo := newServiceCenter(t)
+ _, err := repo.RegisterInstance(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd", &pb.SyncInstance{})
+ if err != nil {
+ t.Errorf("register instance failed, error: %s", err)
+ }
+
+ svr.Close()
+ _, err = repo.RegisterInstance(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd", &pb.SyncInstance{})
+ if err != nil {
+ t.Logf("register instance failed, error: %s", err)
+ }
+}
+
+func TestClient_UnregisterInstance(t *testing.T) {
+ svr, repo := newServiceCenter(t)
+ err := repo.UnregisterInstance(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd", "7a6be9f861a811e9b3f6fa163eca30e0")
+ if err != nil {
+ t.Errorf("unregister instance failed, error: %s", err)
+ }
+
+ svr.Close()
+ err = repo.UnregisterInstance(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd", "7a6be9f861a811e9b3f6fa163eca30e0")
+ if err != nil {
+ t.Logf("unregister instance failed, error: %s", err)
+ }
+}
+
+func TestClient_Heartbeat(t *testing.T) {
+ svr, repo := newServiceCenter(t)
+ err := repo.Heartbeat(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd", "7a6be9f861a811e9b3f6fa163eca30e0")
+ if err != nil {
+ t.Errorf("send instance heartbeat failed, error: %s", err)
+ }
+
+ svr.Close()
+ err = repo.Heartbeat(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd", "7a6be9f861a811e9b3f6fa163eca30e0")
+ if err != nil {
+ t.Logf("send instance heartbeat, error: %s", err)
+ }
+}
diff --git a/syncer/plugins/servicecenter/schemas.go b/syncer/plugins/servicecenter/schemas.go
new file mode 100644
index 0000000..43f9760
--- /dev/null
+++ b/syncer/plugins/servicecenter/schemas.go
@@ -0,0 +1,42 @@
+/*
+ * 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 servicecenter
+
+import (
+ "context"
+
+ pb "github.com/apache/servicecomb-service-center/pkg/registry"
+)
+
+// CreateSchemas Create schemas to servicecenter
+func (c *Client) CreateSchemas(ctx context.Context, domain, project, serviceId string, schemas []*pb.Schema) error {
+ err := c.cli.CreateSchemas(ctx, domain, project, serviceId, schemas)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+// GetSchemasByServiceID Get schemas by serviceId from servicecenter
+func (c *Client) GetSchemasByServiceId(ctx context.Context, domain, project, serviceId string) ([]*pb.Schema, error) {
+ schemas, err := c.cli.GetSchemasByServiceID(ctx, domain, project, serviceId)
+ if err != nil {
+ return nil, err
+ }
+ return schemas, nil
+}
diff --git a/syncer/plugins/servicecenter/service.go b/syncer/plugins/servicecenter/service.go
new file mode 100644
index 0000000..17adab8
--- /dev/null
+++ b/syncer/plugins/servicecenter/service.go
@@ -0,0 +1,79 @@
+/*
+ * 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 servicecenter
+
+import (
+ "context"
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ scpb "github.com/apache/servicecomb-service-center/pkg/registry"
+ "github.com/apache/servicecomb-service-center/pkg/util"
+ pb "github.com/apache/servicecomb-service-center/syncer/proto"
+ "github.com/gogo/protobuf/proto"
+)
+
+// CreateService creates the service of servicecenter
+func (c *Client) CreateService(ctx context.Context, domainProject string, syncService *pb.SyncService) (string, error) {
+ service := toService(syncService)
+ service.ServiceId = ""
+ domain, project := util.FromDomainProject(domainProject)
+ serviceID, err := c.cli.CreateService(ctx, domain, project, service)
+ if err != nil {
+ log.Debugf("create service err %v", err)
+ return "", err
+ }
+
+ matches := pb.Expansions(syncService.Expansions).Find(expansionSchema, map[string]string{})
+ if len(matches) > 0 {
+ schemas := make([]*scpb.Schema, 0, len(matches))
+ for _, expansion := range matches {
+ schema := &scpb.Schema{}
+ err1 := proto.Unmarshal(expansion.Bytes, schema)
+ if err1 != nil {
+ log.Errorf(err1, "proto unmarshal %s service schema, serviceID = %s, kind = %v, content = %v failed",
+ PluginName, serviceID, expansion.Kind, expansion.Bytes)
+ }
+ schemas = append(schemas, schema)
+ }
+ err2 := c.CreateSchemas(ctx, domain, project, serviceID, schemas)
+ if err2 != nil {
+ log.Errorf(err2, "create service schemas failed, serviceID = %s", serviceID)
+ }
+ }
+
+ return serviceID, nil
+}
+
+// DeleteService deletes service from servicecenter
+func (c *Client) DeleteService(ctx context.Context, domainProject, serviceId string) error {
+ domain, project := util.FromDomainProject(domainProject)
+ err := c.cli.DeleteService(ctx, domain, project, serviceId)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+// ServiceExistence Checkes service exists in servicecenter
+func (c *Client) ServiceExistence(ctx context.Context, domainProject string, syncService *pb.SyncService) (string, error) {
+ service := toService(syncService)
+ domain, project := util.FromDomainProject(domainProject)
+ serviceID, err := c.cli.ServiceExistence(ctx, domain, project, service.AppId, service.ServiceName, service.Version, service.Environment)
+ if err != nil {
+ return "", err
+ }
+ return serviceID, nil
+}
diff --git a/syncer/plugins/servicecenter/service_test.go b/syncer/plugins/servicecenter/service_test.go
new file mode 100644
index 0000000..b8d10f3
--- /dev/null
+++ b/syncer/plugins/servicecenter/service_test.go
@@ -0,0 +1,68 @@
+/*
+ * 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 servicecenter
+
+import (
+ "context"
+ "testing"
+
+ pb "github.com/apache/servicecomb-service-center/syncer/proto"
+)
+
+func TestClient_CreateService(t *testing.T) {
+ svr, repo := newServiceCenter(t)
+ _, err := repo.CreateService(context.Background(), "default/deault", &pb.SyncService{})
+ if err != nil {
+ t.Errorf("create service failed, error: %s", err)
+ }
+
+ svr.Close()
+ _, err = repo.CreateService(context.Background(), "default/deault", &pb.SyncService{})
+ if err != nil {
+ t.Logf("create service failed, error: %s", err)
+ }
+}
+
+func TestClient_ServiceExistence(t *testing.T) {
+ svr, repo := newServiceCenter(t)
+ _, err := repo.ServiceExistence(context.Background(), "default/deault", &pb.SyncService{})
+ if err != nil {
+ t.Errorf("check service existence failed, error: %s", err)
+ }
+
+ svr.Close()
+ _, err = repo.ServiceExistence(context.Background(), "default/deault", &pb.SyncService{})
+ if err != nil {
+ t.Logf("check service existence failed, error: %s", err)
+ }
+}
+
+func TestClient_DeleteService(t *testing.T) {
+ svr, repo := newServiceCenter(t)
+ err := repo.DeleteService(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd")
+ if err != nil {
+ t.Errorf("delete service failed, error: %s", err)
+ }
+
+ svr.Close()
+ err = repo.DeleteService(context.Background(), "default/deault",
+ "4042a6a3e5a2893698ae363ea99a69eb63fc51cd")
+ if err != nil {
+ t.Logf("delete service failed, error: %s", err)
+ }
+}
diff --git a/syncer/plugins/servicecenter/servicecenter.go b/syncer/plugins/servicecenter/servicecenter.go
new file mode 100644
index 0000000..18612c5
--- /dev/null
+++ b/syncer/plugins/servicecenter/servicecenter.go
@@ -0,0 +1,85 @@
+/*
+ * 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 servicecenter
+
+import (
+ "context"
+ sc "github.com/apache/servicecomb-service-center/client"
+
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ scpb "github.com/apache/servicecomb-service-center/pkg/registry"
+ "github.com/apache/servicecomb-service-center/syncer/plugins"
+ pb "github.com/apache/servicecomb-service-center/syncer/proto"
+)
+
+const PluginName = "servicecenter"
+
+func init() {
+ // Register self as a repository plugin
+ plugins.RegisterPlugin(&plugins.Plugin{
+ Kind: plugins.PluginServicecenter,
+ Name: PluginName,
+ New: New,
+ })
+}
+
+type adaptor struct{}
+
+func New() plugins.PluginInstance {
+ return &adaptor{}
+}
+
+// New repository with endpoints
+func (*adaptor) New(opts ...plugins.SCConfigOption) (plugins.Servicecenter, error) {
+ cli, err := sc.NewSCClient(plugins.ToSCConfig(opts...))
+ if err != nil {
+ return nil, err
+ }
+ return &Client{cli: cli}, nil
+}
+
+type Client struct {
+ cli *sc.Client
+}
+
+// GetAll get and transform servicecenter data to SyncData
+func (c *Client) GetAll(ctx context.Context) (*pb.SyncData, error) {
+ cache, err := c.cli.GetScCache(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ schemas := make([]*scpb.Schema, 0, len(cache.Microservices))
+ for _, service := range cache.Microservices {
+ if len(service.Value.Schemas) == 0 {
+ continue
+ }
+
+ domain, project := getDomainProjectFromServiceKey(service.Key)
+ if domain == "" {
+ continue
+ }
+
+ ss, err := c.cli.GetSchemasByServiceID(ctx, domain, project, service.Value.ServiceId)
+ if err != nil {
+ log.Warnf("get schemas by serviceId failed: %s", err)
+ continue
+ }
+ schemas = append(schemas, ss...)
+ }
+ return toSyncData(cache, schemas), nil
+}
diff --git a/syncer/plugins/servicecenter/servicecenter_test.go b/syncer/plugins/servicecenter/servicecenter_test.go
new file mode 100644
index 0000000..5ce8460
--- /dev/null
+++ b/syncer/plugins/servicecenter/servicecenter_test.go
@@ -0,0 +1,57 @@
+/*
+ * 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 servicecenter
+
+import (
+ "context"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/apache/servicecomb-service-center/syncer/pkg/mock/mockservicecenter"
+ "github.com/apache/servicecomb-service-center/syncer/plugins"
+)
+
+func TestClient_GetAll(t *testing.T) {
+ _, sc := newServiceCenter(t)
+ _, err := sc.GetAll(context.Background())
+ if err != nil {
+ t.Errorf("get all from %s server failed, error: %s", PluginName, err)
+ }
+
+ _, err = sc.GetAll(context.Background())
+ if err != nil {
+ t.Logf("get all from %s server failed, error: %s", PluginName, err)
+ }
+}
+
+func newServiceCenter(t *testing.T) (*httptest.Server, plugins.Servicecenter) {
+ plugins.SetPluginConfig(plugins.PluginServicecenter.String(), PluginName)
+ adaptor := plugins.Plugins().Servicecenter()
+ if adaptor == nil {
+ t.Errorf("get repository adaptor %s failed", PluginName)
+ }
+ svr := mockservicecenter.NewMockServer()
+ if svr == nil {
+ t.Error("new httptest server failed")
+ }
+
+ repo, err := adaptor.New(plugins.WithEndpoints([]string{svr.URL}))
+ if err != nil {
+ t.Errorf("new repository %s failed, error: %s", PluginName, err)
+ }
+ return svr, repo
+}
diff --git a/syncer/plugins/servicecenter/transform.go b/syncer/plugins/servicecenter/transform.go
new file mode 100644
index 0000000..f1a0270
--- /dev/null
+++ b/syncer/plugins/servicecenter/transform.go
@@ -0,0 +1,289 @@
+/*
+ * 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 servicecenter
+
+import (
+ "github.com/apache/servicecomb-service-center/pkg/model"
+ "net/url"
+ "strconv"
+ "strings"
+
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ scpb "github.com/apache/servicecomb-service-center/pkg/registry"
+ pb "github.com/apache/servicecomb-service-center/syncer/proto"
+ "github.com/gogo/protobuf/proto"
+)
+
+const (
+ expansionDatasource = "datasource"
+ expansionSchema = "schema"
+)
+
+// toSyncData transform service-center service cache to SyncData
+func toSyncData(cache *model.Cache, schemas []*scpb.Schema) (data *pb.SyncData) {
+ data = &pb.SyncData{
+ Services: make([]*pb.SyncService, 0, len(cache.Microservices)),
+ Instances: make([]*pb.SyncInstance, 0, len(cache.Instances)),
+ }
+
+ for _, service := range cache.Microservices {
+ domain, project := getDomainProjectFromServiceKey(service.Key)
+ if domain == "" {
+ continue
+ }
+ syncService := toSyncService(service.Value)
+ syncService.DomainProject = domain + "/" + project
+ syncService.Expansions = append(syncService.Expansions, schemaExpansions(service.Value, schemas)...)
+
+ syncInstances := toSyncInstances(syncService.ServiceId, cache.Instances)
+ if len(syncInstances) == 0 {
+ continue
+ }
+
+ data.Services = append(data.Services, syncService)
+ data.Instances = append(data.Instances, syncInstances...)
+ }
+ return
+}
+
+// toSyncService transform service-center service to SyncService
+func toSyncService(service *scpb.MicroService) (syncService *pb.SyncService) {
+ syncService = &pb.SyncService{
+ ServiceId: service.ServiceId,
+ App: service.AppId,
+ Name: service.ServiceName,
+ Version: service.Version,
+ Environment: service.Environment,
+ PluginName: PluginName,
+ }
+ switch service.Status {
+ case scpb.MS_UP:
+ syncService.Status = pb.SyncService_UP
+ case scpb.MS_DOWN:
+ syncService.Status = pb.SyncService_DOWN
+ default:
+ syncService.Status = pb.SyncService_UNKNOWN
+ }
+
+ content, err := proto.Marshal(service)
+ if err != nil {
+ log.Errorf(err, "transform sc service to syncer service failed: %s", err)
+ return
+ }
+
+ syncService.Expansions = []*pb.Expansion{{
+ Kind: expansionDatasource,
+ Bytes: content,
+ Labels: map[string]string{},
+ }}
+ return
+}
+
+// toSyncInstances transform service-center instances to SyncInstances
+func toSyncInstances(serviceID string, instances []*model.Instance) (syncInstances []*pb.SyncInstance) {
+ for _, inst := range instances {
+ if inst.Value.Status != scpb.MSI_UP {
+ continue
+ }
+
+ if inst.Value.ServiceId == serviceID {
+ syncInstances = append(syncInstances, toSyncInstance(serviceID, inst.Value))
+ }
+ }
+ return
+}
+
+// toSyncInstance transform service-center instance to SyncInstance
+func toSyncInstance(serviceID string, instance *scpb.MicroServiceInstance) (syncInstance *pb.SyncInstance) {
+ syncInstance = &pb.SyncInstance{
+ InstanceId: instance.InstanceId,
+ ServiceId: serviceID,
+ Endpoints: make([]string, 0, len(instance.Endpoints)),
+ HostName: instance.HostName,
+ Version: instance.Version,
+ PluginName: PluginName,
+ }
+ switch instance.Status {
+ case scpb.MSI_UP:
+ syncInstance.Status = pb.SyncInstance_UP
+ case scpb.MSI_DOWN:
+ syncInstance.Status = pb.SyncInstance_DOWN
+ case scpb.MSI_STARTING:
+ syncInstance.Status = pb.SyncInstance_STARTING
+ case scpb.MSI_OUTOFSERVICE:
+ syncInstance.Status = pb.SyncInstance_OUTOFSERVICE
+ default:
+ syncInstance.Status = pb.SyncInstance_UNKNOWN
+ }
+
+ for _, ep := range instance.Endpoints {
+ endpoint := ep
+ addr, err := url.Parse(ep)
+ if err != nil {
+ log.Errorf(err, "parse sc instance endpoint failed: %s", err)
+ continue
+ }
+ if addr.Scheme == "rest" {
+ prefix := "http://"
+ b, _ := strconv.ParseBool(addr.Query().Get("sslEnabled"))
+ if b {
+ prefix = "https://"
+ }
+ endpoint = strings.Replace(ep, addr.Scheme+"://", prefix, 1)
+ }
+ syncInstance.Endpoints = append(syncInstance.Endpoints, endpoint)
+ }
+
+ if instance.HealthCheck != nil {
+ syncInstance.HealthCheck = &pb.HealthCheck{
+ Port: instance.HealthCheck.Port,
+ Interval: instance.HealthCheck.Interval,
+ Times: instance.HealthCheck.Times,
+ Url: instance.HealthCheck.Url,
+ }
+ }
+
+ content, err := proto.Marshal(instance)
+ if err != nil {
+ log.Errorf(err, "transform sc instance to syncer instance failed: %s", err)
+ return
+ }
+
+ syncInstance.Expansions = []*pb.Expansion{{
+ Kind: expansionDatasource,
+ Bytes: content,
+ Labels: map[string]string{},
+ }}
+ return
+}
+
+func schemaExpansions(service *scpb.MicroService, schemas []*scpb.Schema) (expansions []*pb.Expansion) {
+ for _, val := range schemas {
+ if !inSlice(service.Schemas, val.SchemaId) {
+ continue
+ }
+
+ content, err := proto.Marshal(val)
+ if err != nil {
+ log.Errorf(err, "proto marshal schemas failed, app = %s, service = %s, version = %s datasource = %s",
+ service.AppId, service.ServiceName, service.Version, expansionSchema)
+ continue
+ }
+ expansions = append(expansions, &pb.Expansion{
+ Kind: expansionSchema,
+ Bytes: content,
+ Labels: map[string]string{},
+ })
+ }
+ return
+}
+
+// toService transform SyncService to service-center service
+func toService(syncService *pb.SyncService) (service *scpb.MicroService) {
+ service = &scpb.MicroService{}
+ var err error
+ if syncService.PluginName == PluginName && len(syncService.Expansions) > 0 {
+ matches := pb.Expansions(syncService.Expansions).Find(expansionDatasource, map[string]string{})
+ if len(matches) > 0 {
+ err = proto.Unmarshal(matches[0].Bytes, service)
+ if err == nil {
+ service.ServiceId = syncService.ServiceId
+ return
+ }
+ log.Errorf(err, "proto unmarshal %s service, serviceID = %s, kind = %v, content = %v failed",
+ PluginName, service.ServiceId, matches[0].Kind, matches[0].Bytes)
+ }
+ }
+ service.AppId = syncService.App
+ service.ServiceId = syncService.ServiceId
+ service.ServiceName = syncService.Name
+ service.Version = syncService.Version
+ service.Status = pb.SyncService_Status_name[int32(syncService.Status)]
+ service.Environment = syncService.Environment
+ return
+}
+
+// toInstance transform SyncInstance to service-center instance
+func toInstance(syncInstance *pb.SyncInstance) (instance *scpb.MicroServiceInstance) {
+ instance = &scpb.MicroServiceInstance{}
+ if syncInstance.PluginName == PluginName && len(syncInstance.Expansions) > 0 {
+ matches := pb.Expansions(syncInstance.Expansions).Find(expansionDatasource, map[string]string{})
+ if len(matches) > 0 {
+ err := proto.Unmarshal(matches[0].Bytes, instance)
+ if err == nil {
+ instance.InstanceId = syncInstance.InstanceId
+ instance.ServiceId = syncInstance.ServiceId
+ return
+ }
+ log.Errorf(err, "proto unmarshal %s instance, instanceID = %s, kind = %v, content = %v failed",
+ PluginName, instance.InstanceId, matches[0].Kind, matches[0].Bytes)
+
+ }
+ }
+ instance.InstanceId = syncInstance.InstanceId
+ instance.ServiceId = syncInstance.ServiceId
+ instance.Endpoints = make([]string, 0, len(syncInstance.Endpoints))
+ instance.HostName = syncInstance.HostName
+ instance.Version = syncInstance.Version
+ instance.Status = pb.SyncInstance_Status_name[int32(syncInstance.Status)]
+
+ for _, ep := range syncInstance.Endpoints {
+ addr, err := url.Parse(ep)
+ if err != nil {
+ log.Errorf(err, "parse sc instance endpoint failed: %s", err)
+ continue
+ }
+ endpoint := ""
+ switch addr.Scheme {
+ case "http":
+ endpoint = strings.Replace(ep, "http://", "rest://", 1)
+ case "https":
+ endpoint = strings.Replace(ep, "https://", "rest://", 1) + "?sslEnabled=true"
+ case "rest", "highway":
+ endpoint = ep
+ }
+ instance.Endpoints = append(instance.Endpoints, endpoint)
+ }
+
+ if syncInstance.HealthCheck != nil && syncInstance.HealthCheck.Mode != pb.HealthCheck_UNKNOWN {
+ instance.HealthCheck = &scpb.HealthCheck{
+ Mode: pb.HealthCheck_Modes_name[int32(syncInstance.HealthCheck.Mode)],
+ Port: syncInstance.HealthCheck.Port,
+ Interval: syncInstance.HealthCheck.Interval,
+ Times: syncInstance.HealthCheck.Times,
+ Url: syncInstance.HealthCheck.Url,
+ }
+ }
+ return
+}
+
+func getDomainProjectFromServiceKey(serviceKey string) (string, string) {
+ tenant := strings.Split(serviceKey, "/")
+ if len(tenant) < 6 {
+ return "", ""
+ }
+ return tenant[4], tenant[5]
+}
+
+func inSlice(slice []string, val string) bool {
+ for _, item := range slice {
+ if item == val {
+ return true
+ }
+ }
+ return false
+}
diff --git a/syncer/serf/serf.go b/syncer/serf/serf.go
index ee8a407..517cb44 100644
--- a/syncer/serf/serf.go
+++ b/syncer/serf/serf.go
@@ -19,6 +19,7 @@ package serf
import (
"context"
+ "strings"
"sync"
"time"
@@ -48,10 +49,11 @@ type Server struct {
stopCh chan struct{}
handlerMap *sync.Map
+ peerAddr []string
}
// NewServer new serf server with options
-func NewServer(opts ...Option) *Server {
+func NewServer(peerAddr string, opts ...Option) *Server {
conf := serf.DefaultConfig()
conf.Tags = map[string]string{}
for _, opt := range opts {
@@ -61,6 +63,10 @@ func NewServer(opts ...Option) *Server {
eventCh := make(chan serf.Event, 64)
conf.EventCh = eventCh
+ address := strings.Split(peerAddr, ",")
+ if len(peerAddr) == 0 {
+ address = []string{}
+ }
return &Server{
conf: conf,
running: utils.NewAtomicBool(false),
@@ -68,6 +74,7 @@ func NewServer(opts ...Option) *Server {
readyCh: make(chan struct{}),
stopCh: make(chan struct{}),
handlerMap: &sync.Map{},
+ peerAddr: address,
}
}
@@ -81,6 +88,7 @@ func (s *Server) Start(ctx context.Context) {
return
}
s.serf = sf
+ s.Join(s.peerAddr)
close(s.readyCh)
go s.waitEvent(ctx)
})
diff --git a/syncer/serf/serf_test.go b/syncer/serf/serf_test.go
index b350222..64c7337 100644
--- a/syncer/serf/serf_test.go
+++ b/syncer/serf/serf_test.go
@@ -56,6 +56,7 @@ func TestSerfServer(t *testing.T) {
func TestServerFailed(t *testing.T) {
svr := NewServer(
+ "",
WithNode("syncer-test"),
WithTags(map[string]string{"test-key": "test-value"}),
WithAddTag("added-key", "added-value"),
@@ -149,6 +150,7 @@ func TestEventHandler(t *testing.T) {
func defaultServer() *Server {
return NewServer(
+ "",
WithNode("syncer-test"),
WithBindAddr("127.0.0.1"),
WithBindPort(35151),
diff --git a/syncer/server/server.go b/syncer/server/server.go
index 6364273..1eb2da0 100644
--- a/syncer/server/server.go
+++ b/syncer/server/server.go
@@ -40,6 +40,7 @@ import (
// import plugins
_ "github.com/apache/servicecomb-service-center/syncer/plugins/eureka"
+ _ "github.com/apache/servicecomb-service-center/syncer/plugins/servicecenter"
// import task
_ "github.com/apache/servicecomb-service-center/syncer/task/idle"
@@ -184,7 +185,7 @@ func (s *Server) initialization() (err error) {
pb.RegisterSyncServer(svr, s)
})
- s.serf = serf.NewServer(convertSerfOptions(s.conf)...)
+ s.serf = serf.NewServer(s.conf.Join.Address, convertSerfOptions(s.conf)...)
s.serf.OnceEventHandler(serf.NewEventHandler(serf.MemberJoinFilter(), s.waitClusterMembers))
s.etcd, err = etcd.NewServer(convertEtcdOptions(s.conf)...)