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 2019/09/23 06:06:45 UTC

[servicecomb-service-center] branch master updated: add retry of descovery and some logs to samples (#589)

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 79cd6fe  add retry of descovery and some logs to samples (#589)
79cd6fe is described below

commit 79cd6fe81a2d02b883a84ed2f79b18952355005a
Author: Chenzhu1008 <c5...@126.com>
AuthorDate: Mon Sep 23 14:06:39 2019 +0800

    add retry of descovery and some logs to samples (#589)
---
 .../servicecenter/account/AccountController.java   |  1 +
 .../hello-server/conf/microservice.yaml            |  4 +--
 .../servicecenter/hello-server/main.go             | 12 ++++---
 .../hello-server/servicecenter/servicecenter.go    | 38 +++++++++++++++-------
 4 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/syncer/samples/multi-servicecenters/eureka/account-server/src/main/java/org/apache/servicecomb/servicecenter/account/AccountController.java b/syncer/samples/multi-servicecenters/eureka/account-server/src/main/java/org/apache/servicecomb/servicecenter/account/AccountController.java
index b5beced..4701bf3 100644
--- a/syncer/samples/multi-servicecenters/eureka/account-server/src/main/java/org/apache/servicecomb/servicecenter/account/AccountController.java
+++ b/syncer/samples/multi-servicecenters/eureka/account-server/src/main/java/org/apache/servicecomb/servicecenter/account/AccountController.java
@@ -40,6 +40,7 @@ public class AccountController {
             return new ResponseEntity<>("password is empty", HttpStatus.UNAUTHORIZED);
         }
 
+        System.out.println("request from consumer, user is "+loginVO.user);
         // todo: check user and password
         return new ResponseEntity<>("welcome " + loginVO.user, HttpStatus.OK);
     }
diff --git a/syncer/samples/multi-servicecenters/servicecenter/hello-server/conf/microservice.yaml b/syncer/samples/multi-servicecenters/servicecenter/hello-server/conf/microservice.yaml
index e3764b5..ecb81b7 100644
--- a/syncer/samples/multi-servicecenters/servicecenter/hello-server/conf/microservice.yaml
+++ b/syncer/samples/multi-servicecenters/servicecenter/hello-server/conf/microservice.yaml
@@ -20,10 +20,10 @@ service: # 微服务配置
   version: 0.0.1
   instance: # 实例信息
     protocol: rest
-    listenAddress: 10.0.0.1:8091 #实例监听地址
+    listenAddress: 127.0.0.1:8091 #实例监听地址
 provider: # 服务端信息
   appId: eureka
   name: account-server
   version: 0.0.1
 registry:
-  address: http://10.0.0.1:30100 # service-center 地址
\ No newline at end of file
+  address: http://127.0.0.1:30100 # service-center 地址
\ No newline at end of file
diff --git a/syncer/samples/multi-servicecenters/servicecenter/hello-server/main.go b/syncer/samples/multi-servicecenters/servicecenter/hello-server/main.go
index a280e4f..5031d4c 100644
--- a/syncer/samples/multi-servicecenters/servicecenter/hello-server/main.go
+++ b/syncer/samples/multi-servicecenters/servicecenter/hello-server/main.go
@@ -50,13 +50,18 @@ func main() {
 		return
 	}
 	go syssig.Run(ctx)
+	go start(ctx, conf)
+	defer servicecenter.Stop(ctx)
+
+	<-stopCh
+}
 
-	err = servicecenter.Start(ctx, conf)
+func start(ctx context.Context, conf *servicecenter.Config)  {
+	err := servicecenter.Start(ctx, conf)
 	if err != nil {
-		log.Fatal("listen system signal failed", err)
+		log.Fatal("register and discovery from servicecenter failed", err)
 		return
 	}
-	defer servicecenter.Stop(ctx)
 
 	if conf.Provider != nil {
 		// check health
@@ -95,7 +100,6 @@ func main() {
 			}
 		}()
 	}
-	<-stopCh
 }
 
 func checkHealth(providerName string) (string, error) {
diff --git a/syncer/samples/multi-servicecenters/servicecenter/hello-server/servicecenter/servicecenter.go b/syncer/samples/multi-servicecenters/servicecenter/hello-server/servicecenter/servicecenter.go
index 756e650..1d5641a 100644
--- a/syncer/samples/multi-servicecenters/servicecenter/hello-server/servicecenter/servicecenter.go
+++ b/syncer/samples/multi-servicecenters/servicecenter/hello-server/servicecenter/servicecenter.go
@@ -32,13 +32,15 @@ import (
 )
 
 var (
-	domainProject     string
-	cli               *sc.SCClient
-	once              sync.Once
-	heartbeatInterval = 30
-	providerCaches    = &sync.Map{}
-	service           *proto.MicroService
-	instance          *proto.MicroServiceInstance
+	domainProject         string
+	cli                   *sc.SCClient
+	once                  sync.Once
+	heartbeatInterval     = 30
+	providerCaches        = &sync.Map{}
+	service               *proto.MicroService
+	instance              *proto.MicroServiceInstance
+	retryDiscover         = 3
+	retryDiscoverInterval = 30
 )
 
 func Start(ctx context.Context, conf *Config) (err error) {
@@ -70,11 +72,23 @@ func Start(ctx context.Context, conf *Config) (err error) {
 			consumerID = service.ServiceId
 		}
 
+		for i := 0; i <= retryDiscover; i++ {
+			// 定时发送心跳
+			err1 := discoveryToCaches(ctx, consumerID, conf.Provider)
+			if err1 == nil {
+				err = nil
+				log.Infof("discovery provider success, appID = %s, name = %s, version = %s",
+					conf.Provider.AppID, conf.Provider.Name, conf.Provider.Version)
+				break
+			}
+			err = err1
+			log.Warnf("discovery provider failed, appID = %s, name = %s, version = %s",
+				conf.Provider.AppID, conf.Provider.Name, conf.Provider.Version)
+			log.Info("waiting for retry")
+			time.Sleep(time.Duration(retryDiscoverInterval) * time.Second)
 
-		err = discoveryToCaches(ctx, consumerID, conf.Provider)
-		if err != nil {
-			return
 		}
+
 		go watchAndRenewCaches(ctx, conf.Provider)
 	})
 	return
@@ -176,13 +190,13 @@ func watchAndRenewCaches(ctx context.Context, provider *MicroService) {
 		providerList := list.([]*proto.MicroServiceInstance)
 
 		renew := false
-		for i, item := range providerList{
+		for i, item := range providerList {
 			if item.InstanceId != result.Instance.InstanceId {
 				continue
 			}
 			if result.Action == "DELETE" {
 				providerList = append(providerList[:i], providerList[i+1:]...)
-			}else{
+			} else {
 				providerList[i] = result.Instance
 			}
 			renew = true