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

littlecui 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 5a54da2  SCB-953 Make clusters configuration convenient (#459)
5a54da2 is described below

commit 5a54da2388860cebdeadc53d267e77b69b4fa06b
Author: little-cui <[email protected]>
AuthorDate: Mon Oct 22 10:05:24 2018 +0800

    SCB-953 Make clusters configuration convenient (#459)
    
    * SCB-953 Make clusters configuration convenient
    
    * Update the README
---
 docs/multidcs.md                                   |   7 +-
 docs/multidcs2-1.PNG                               | Bin 0 -> 122943 bytes
 docs/multidcs2.md                                  | 114 +++++++++++++++++++++
 .../pkg/discovery/k8s/adaptor/cacher_index.go      |   2 +-
 .../pkg/discovery/k8s/adaptor/cacher_instance.go   |   6 +-
 .../pkg/discovery/k8s/adaptor/cacher_service.go    |   2 +-
 server/plugin/pkg/discovery/k8s/adaptor/common.go  |   6 ++
 .../plugin/pkg/discovery/k8s/adaptor/convertor.go  |   2 +-
 server/plugin/pkg/registry/config.go               |  14 ++-
 server/plugin/pkg/registry/etcd/etcd_test.go       |  21 +++-
 10 files changed, 160 insertions(+), 14 deletions(-)

diff --git a/docs/multidcs.md b/docs/multidcs.md
index 3354ff6..fa5381b 100644
--- a/docs/multidcs.md
+++ b/docs/multidcs.md
@@ -69,7 +69,7 @@ scctl --addr http://10.12.0.3:30100 get cluster
 
 ## Example
 
-Here we show an example of multiple datacenters access, where we use an 
+Here we show a `golang` example of multiple datacenters access, where we use 
an 
 
[`example`](https://github.com/go-chassis/go-chassis/tree/master/examples/discovery)
 of the [`go-chassis`](https://github.com/go-chassis/go-chassis) project, 
assuming that below.
 
@@ -78,6 +78,11 @@ of the 
[`go-chassis`](https://github.com/go-chassis/go-chassis) project, assumin
 | Client       | dc-1        | 10.12.0.4   |   
 | Server       | dc-2        | 10.12.0.5   |
 
+Notes: `go-chassis` application can run perfectly in the above 2 architectures.
+If you are using 
[`java-chassis`](https://github.com/apache/incubator-servicecomb-java-chassis),
+there are only support the service center with the second architecture at the 
moment.
+You can ref to [`here`](/docs/multidcs2.md) for more details of the second 
architecture.
+
 ##### Start Server
 
 Edit the configuration of the ip/port on which Server will register.
diff --git a/docs/multidcs2-1.PNG b/docs/multidcs2-1.PNG
new file mode 100644
index 0000000..879cb63
Binary files /dev/null and b/docs/multidcs2-1.PNG differ
diff --git a/docs/multidcs2.md b/docs/multidcs2.md
new file mode 100644
index 0000000..6670cc2
--- /dev/null
+++ b/docs/multidcs2.md
@@ -0,0 +1,114 @@
+Using Java chassis for cross data center access
+-------
+
+Now that you've seen two [`multiple data center`](/docs/multidcs.md) 
architectures of the Service Center, 
+we'll show you how to implement micro-service cross data center access with 
the 
+[`java-chassis`](https://github.com/apache/incubator-servicecomb-java-chassis) 
framework.
+
+![architecture](/docs/multidcs2-1.PNG)
+
+## Quick Start
+
+Let's assume you want to install 2 clusters of Service-Center in different DCs 
with following details.
+
+| Cluster | Datacenter  | Address     |  
+| :-----: | :---------: | :---------: |  
+| sc-1    | dc-1        | 10.12.0.1   |   
+| sc-2    | dc-2        | 10.12.0.2   | 
+
+##### Start Service-Center
+
+Edit the configuration of the ip/port on which SC will run in dc-1.
+And here we assume your etcd is running on http://127.0.0.1:2379 (you can 
follow 
[this](https://github.com/coreos/etcd/blob/master/Documentation/op-guide/container.md)
 guide to install etcd in cluster mode.)
+```bash
+vi conf/app.conf
+# Replace the below values
+httpaddr = 10.12.0.1
+discovery_plugin = aggregate
+aggregate_mode = "etcd,servicecenter"
+manager_name = "sc-1"
+manager_addr = "http://127.0.0.1:2379";
+manager_cluster = "sc-1=http://10.12.0.1:30100,sc-2=http://10.12.0.2:30100";
+
+# Start the Service-center
+./service-center
+```
+
+Notes:
++ `manager_name` is the alias of the data center.
+`manager_addr` is the etcd cluster client urls.
+`manager_cluster` is the full Service Center clusters list.
++ To deploy Service Center in dc-2, you can repeat the
+above steps and just change the `httpaddr` value to `10.12.0.2`.
+
+##### Confirm the service is OK
+
+We recommend that you use [`scctl`](/scctl/README.md), and using
+[`cluster command`](/scctl/pkg/plugin/README.md#cluster-options)
+which makes it very convenient to verify OK.
+
+```bash
+scctl --addr http://10.12.0.3:30100 get cluster
+#   CLUSTER |        ENDPOINTS         
+# +---------+-------------------------+
+#   sc-1    | http://10.12.0.1:30100  
+#   sc-2    | http://10.12.0.2:30100
+```
+
+## Example
+
+Here we show a `java` example of multiple datacenters access, where we use an 
+[`example`](https://github.com/apache/incubator-servicecomb-java-chassis/tree/master/demo/demo-springmvc),
+assuming that below.
+
+| Microservice | Datacenter  | Address     |  
+| :-----:      | :---------: | :---------: |  
+| Client       | dc-1        | 10.12.0.4   |   
+| Server       | dc-2        | 10.12.0.5   |
+
+##### Start springmvc-server
+
+Edit the configuration of the ip/port on which `springmvc-server` will 
register.
+```
+vi src/main/resources/microservice.yaml
+```
+Replace the below values
+```yaml
+cse:
+  service:
+    registry:
+      address: http://10.12.0.2:30100 # the address of SC in dc-2
+```
+
+Run the Server
+```bash
+mvn clean install
+java -jar target/springmvc-server-0.0.1-SNAPSHOT.jar
+```
+##### Start springmvc-client
+
+Edit the configuration of the ip/port on which `springmvc-client` will 
register.
+```bash
+vi src/main/resources/microservice.yaml
+```
+Replace the below values
+```yaml
+cse:
+  service:
+    registry:
+      address: http://10.12.0.1:30100 # the address of SC in dc-1
+```
+
+Run the Client
+```bash
+mvn clean install
+java -jar target/springmvc-client-0.0.1-SNAPSHOT.jar
+```
+
+##### Confirm the multiple datacenters discovery is OK
+
+Since `springmvc-client` is not a service, we check its running log.
+```
+...
+[2018-10-19 23:04:42,800/CST][main][INFO]............. test finished 
............ org.apache.servicecomb.demo.TestMgr.summary(TestMgr.java:83)
+```
diff --git a/server/plugin/pkg/discovery/k8s/adaptor/cacher_index.go 
b/server/plugin/pkg/discovery/k8s/adaptor/cacher_index.go
index b03a637..5958f31 100644
--- a/server/plugin/pkg/discovery/k8s/adaptor/cacher_index.go
+++ b/server/plugin/pkg/discovery/k8s/adaptor/cacher_index.go
@@ -30,7 +30,7 @@ type ServiceIndexCacher struct {
 func (c *ServiceIndexCacher) onServiceEvent(evt K8sEvent) {
        svc := evt.Object.(*v1.Service)
        domainProject := Kubernetes().GetDomainProject()
-       serviceId := string(svc.UID)
+       serviceId := uuid(svc.UID)
        indexKey := 
core.GenerateServiceIndexKey(generateServiceKey(domainProject, svc))
 
        if !ShouldRegisterService(svc) {
diff --git a/server/plugin/pkg/discovery/k8s/adaptor/cacher_instance.go 
b/server/plugin/pkg/discovery/k8s/adaptor/cacher_instance.go
index 55c0f3e..8147384 100644
--- a/server/plugin/pkg/discovery/k8s/adaptor/cacher_instance.go
+++ b/server/plugin/pkg/discovery/k8s/adaptor/cacher_instance.go
@@ -33,7 +33,7 @@ type InstanceCacher struct {
 func (c *InstanceCacher) onServiceEvent(evt K8sEvent) {
        svc := evt.Object.(*v1.Service)
        domainProject := Kubernetes().GetDomainProject()
-       serviceId := string(svc.UID)
+       serviceId := uuid(svc.UID)
 
        switch evt.EventType {
        case pb.EVT_DELETE:
@@ -77,7 +77,7 @@ func (c *InstanceCacher) onEndpointsEvent(evt K8sEvent) {
                return
        }
 
-       serviceId := string(svc.UID)
+       serviceId := uuid(svc.UID)
        domainProject := Kubernetes().GetDomainProject()
        oldKvs := c.getInstances(domainProject, serviceId)
        newKvs := make(map[string]*discovery.KeyValue)
@@ -88,7 +88,7 @@ func (c *InstanceCacher) onEndpointsEvent(evt K8sEvent) {
                                continue
                        }
 
-                       instanceId := string(pod.UID)
+                       instanceId := uuid(pod.UID)
                        key := 
core.GenerateInstanceKey(Kubernetes().GetDomainProject(), serviceId, instanceId)
                        switch evt.EventType {
                        case pb.EVT_CREATE, pb.EVT_UPDATE:
diff --git a/server/plugin/pkg/discovery/k8s/adaptor/cacher_service.go 
b/server/plugin/pkg/discovery/k8s/adaptor/cacher_service.go
index cb5fcf0..f1df1ac 100644
--- a/server/plugin/pkg/discovery/k8s/adaptor/cacher_service.go
+++ b/server/plugin/pkg/discovery/k8s/adaptor/cacher_service.go
@@ -30,7 +30,7 @@ type ServiceCacher struct {
 func (c *ServiceCacher) onServiceEvent(evt K8sEvent) {
        svc := evt.Object.(*v1.Service)
        domainProject := Kubernetes().GetDomainProject()
-       serviceId := string(svc.UID)
+       serviceId := uuid(svc.UID)
        key := core.GenerateServiceKey(domainProject, serviceId)
 
        if !ShouldRegisterService(svc) {
diff --git a/server/plugin/pkg/discovery/k8s/adaptor/common.go 
b/server/plugin/pkg/discovery/k8s/adaptor/common.go
index cf67a62..15ee29a 100644
--- a/server/plugin/pkg/discovery/k8s/adaptor/common.go
+++ b/server/plugin/pkg/discovery/k8s/adaptor/common.go
@@ -20,8 +20,10 @@ import (
        "github.com/apache/incubator-servicecomb-service-center/pkg/util"
        "k8s.io/api/core/v1"
        meta "k8s.io/apimachinery/pkg/apis/meta/v1"
+       "k8s.io/apimachinery/pkg/types"
        "k8s.io/client-go/kubernetes"
        "k8s.io/client-go/tools/clientcmd"
+       "strings"
        "time"
 )
 
@@ -95,3 +97,7 @@ func ShouldRegisterService(service *v1.Service) bool {
        }
        return true
 }
+
+func uuid(id types.UID) string {
+       return strings.Replace(string(id), "-", "", -1)
+}
diff --git a/server/plugin/pkg/discovery/k8s/adaptor/convertor.go 
b/server/plugin/pkg/discovery/k8s/adaptor/convertor.go
index a3239ad..c5e2fda 100644
--- a/server/plugin/pkg/discovery/k8s/adaptor/convertor.go
+++ b/server/plugin/pkg/discovery/k8s/adaptor/convertor.go
@@ -89,7 +89,7 @@ func generateServiceKey(domainProject string, svc 
*v1.Service) *pb.MicroServiceK
 
 func FromK8sService(svc *v1.Service) (ms *pb.MicroService) {
        ms = &pb.MicroService{
-               ServiceId:   string(svc.UID),
+               ServiceId:   uuid(svc.UID),
                Environment: getLabel(svc.Labels, LabelEnvironment, ""),
                AppId:       getLabel(svc.Labels, LabelApp, pb.APP_ID),
                ServiceName: svc.Name,
diff --git a/server/plugin/pkg/registry/config.go 
b/server/plugin/pkg/registry/config.go
index b75cfa4..d48beb0 100644
--- a/server/plugin/pkg/registry/config.go
+++ b/server/plugin/pkg/registry/config.go
@@ -67,9 +67,12 @@ func (c *Config) InitClusters() {
                for i, name := range names {
                        c.Clusters[name] = addrs[i]
                }
+               if len(c.ManagerAddress) > 0 {
+                       c.Clusters[c.ClusterName] = 
strings.Split(c.ManagerAddress, ",")
+               }
        }
        if len(c.Clusters) == 0 {
-               c.Clusters[c.ClusterName] = []string{c.ClusterAddresses}
+               c.Clusters[c.ClusterName] = strings.Split(c.ClusterAddresses, 
",")
        }
 }
 
@@ -84,6 +87,12 @@ func Configuration() *Config {
                defaultRegistryConfig.ClusterName = 
beego.AppConfig.DefaultString("manager_name", "default")
                defaultRegistryConfig.ManagerAddress = 
beego.AppConfig.String("manager_addr")
                defaultRegistryConfig.ClusterAddresses = 
beego.AppConfig.DefaultString("manager_cluster", "http://127.0.0.1:2379";)
+               defaultRegistryConfig.InitClusters()
+
+               registryAddresses := 
strings.Join(defaultRegistryConfig.RegistryAddresses(), ",")
+               defaultRegistryConfig.SslEnabled = 
core.ServerInfo.Config.SslEnabled &&
+                       strings.Index(strings.ToLower(registryAddresses), 
"https://";) >= 0
+
                defaultRegistryConfig.DialTimeout, err = 
time.ParseDuration(beego.AppConfig.DefaultString("connect_timeout", "10s"))
                if err != nil {
                        log.Errorf(err, "connect_timeout is invalid, use 
default time %s", defaultDialTimeout)
@@ -94,13 +103,10 @@ func Configuration() *Config {
                        log.Errorf(err, "registry_timeout is invalid, use 
default time %s", defaultRequestTimeout)
                        defaultRegistryConfig.RequestTimeOut = 
defaultRequestTimeout
                }
-               defaultRegistryConfig.SslEnabled = 
core.ServerInfo.Config.SslEnabled &&
-                       
strings.Index(strings.ToLower(defaultRegistryConfig.ClusterAddresses), 
"https://";) >= 0
                defaultRegistryConfig.AutoSyncInterval, err = 
time.ParseDuration(core.ServerInfo.Config.AutoSyncInterval)
                if err != nil {
                        log.Errorf(err, "auto_sync_interval is invalid")
                }
-               defaultRegistryConfig.InitClusters()
        })
        return &defaultRegistryConfig
 }
diff --git a/server/plugin/pkg/registry/etcd/etcd_test.go 
b/server/plugin/pkg/registry/etcd/etcd_test.go
index f3856b4..328ae6d 100644
--- a/server/plugin/pkg/registry/etcd/etcd_test.go
+++ b/server/plugin/pkg/registry/etcd/etcd_test.go
@@ -61,7 +61,7 @@ func TestInitCluster(t *testing.T) {
                t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().RegistryAddresses())
        }
        if strings.Join(registry.Configuration().Clusters["sc-0"], ",") != 
"127.0.0.1:2379,127.0.0.2:2379" {
-               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().RegistryAddresses())
+               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().Clusters)
        }
        registry.Configuration().ClusterName = "sc-0"
        registry.Configuration().ClusterAddresses = 
"sc-1=127.0.0.1:2379,127.0.0.2:2379,sc-2=127.0.0.3:2379"
@@ -70,10 +70,10 @@ func TestInitCluster(t *testing.T) {
                t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().RegistryAddresses())
        }
        if strings.Join(registry.Configuration().Clusters["sc-1"], ",") != 
"127.0.0.1:2379,127.0.0.2:2379" {
-               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().RegistryAddresses())
+               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().Clusters)
        }
        if strings.Join(registry.Configuration().Clusters["sc-2"], ",") != 
"127.0.0.3:2379" {
-               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().RegistryAddresses())
+               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().Clusters)
        }
        registry.Configuration().ClusterName = "sc-0"
        registry.Configuration().ClusterAddresses = 
"sc-0=127.0.0.1:2379,sc-1=127.0.0.3:2379,127.0.0.4:2379"
@@ -82,11 +82,26 @@ func TestInitCluster(t *testing.T) {
                t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().RegistryAddresses())
        }
        if strings.Join(registry.Configuration().Clusters["sc-1"], ",") != 
"127.0.0.3:2379,127.0.0.4:2379" {
+               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().Clusters)
+       }
+       registry.Configuration().ClusterName = "sc-0"
+       registry.Configuration().ManagerAddress = 
"127.0.0.1:2379,127.0.0.2:2379"
+       registry.Configuration().ClusterAddresses = 
"sc-0=127.0.0.1:30100,sc-1=127.0.0.2:30100"
+       registry.Configuration().InitClusters()
+       if strings.Join(registry.Configuration().RegistryAddresses(), ",") != 
"127.0.0.1:2379,127.0.0.2:2379" {
                t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().RegistryAddresses())
        }
+       if strings.Join(registry.Configuration().Clusters["sc-1"], ",") != 
"127.0.0.2:30100" {
+               t.Fatalf("TestInitCluster failed, %v", 
registry.Configuration().Clusters)
+       }
 }
 
 func TestEtcdClient(t *testing.T) {
+       registry.Configuration().ClusterName = ""
+       registry.Configuration().ManagerAddress = ""
+       registry.Configuration().ClusterAddresses = endpoint
+       registry.Configuration().InitClusters()
+
        etcd := &EtcdClient{
                Endpoints:   []string{endpoint},
                DialTimeout: dialTimeout,

Reply via email to