You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2022/01/19 02:06:02 UTC

[servicecomb-service-center] branch master updated: Feature: support customize the sc registry endpoint (#1232)

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/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new cbc4384  Feature: support customize the sc registry endpoint (#1232)
cbc4384 is described below

commit cbc438432a26ea74e859350b1920401e76bd6b2e
Author: little-cui <su...@qq.com>
AuthorDate: Wed Jan 19 10:03:14 2022 +0800

    Feature: support customize the sc registry endpoint (#1232)
---
 etc/conf/app.yaml           |  2 ++
 server/api_server.go        | 16 ----------------
 server/core/microservice.go | 19 +++++++++++++++++--
 3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/etc/conf/app.yaml b/etc/conf/app.yaml
index a489421..89ff15b 100644
--- a/etc/conf/app.yaml
+++ b/etc/conf/app.yaml
@@ -138,6 +138,8 @@ registry:
       cron: '0 1 * * *'
       reserve: 3
   instance:
+    # the endpoint as the service-center's endpoint instead of server.host
+    endpoint:
     # By default, instance TTL = (times + 1) * interval
     # if ttl > 0, the instance will always set this value, ignore the API request body
     ttl:
diff --git a/server/api_server.go b/server/api_server.go
index eb8e170..5c1d8e7 100644
--- a/server/api_server.go
+++ b/server/api_server.go
@@ -27,7 +27,6 @@ import (
 	"github.com/apache/servicecomb-service-center/pkg/log"
 	"github.com/apache/servicecomb-service-center/pkg/rest"
 	"github.com/apache/servicecomb-service-center/server/config"
-	"github.com/apache/servicecomb-service-center/server/core"
 	"github.com/apache/servicecomb-service-center/server/metrics"
 	rs "github.com/apache/servicecomb-service-center/server/rest"
 	"github.com/apache/servicecomb-service-center/server/service/registry"
@@ -77,17 +76,6 @@ func (s *APIServer) Listen(ip, port string) {
 	s.HostPort = net.JoinHostPort(ip, port)
 }
 
-func (s *APIServer) populateEndpoint(ipPort string) {
-	if len(ipPort) == 0 {
-		return
-	}
-	address := fmt.Sprintf("rest://%s/", ipPort)
-	if config.GetSSL().SslEnabled {
-		address += "?sslEnabled=true"
-	}
-	core.Instance.Endpoints = append(core.Instance.Endpoints, address)
-}
-
 func (s *APIServer) serve() (err error) {
 	s.HTTPServer, err = rs.NewServer(s.HostPort)
 	if err != nil {
@@ -95,8 +83,6 @@ func (s *APIServer) serve() (err error) {
 	}
 	log.Info(fmt.Sprintf("listen address: rest://%s", s.HTTPServer.Listener.Addr().String()))
 
-	s.populateEndpoint(s.HTTPServer.Listener.Addr().String())
-
 	s.goroutine.Do(func(_ context.Context) {
 		err := s.HTTPServer.Serve()
 		if s.isClose {
@@ -114,8 +100,6 @@ func (s *APIServer) Start() {
 	}
 	s.isClose = false
 
-	core.Instance.Endpoints = nil
-
 	err := s.serve()
 	if err != nil {
 		s.err <- err
diff --git a/server/core/microservice.go b/server/core/microservice.go
index e37433c..35ece9d 100644
--- a/server/core/microservice.go
+++ b/server/core/microservice.go
@@ -19,6 +19,7 @@ package core
 
 import (
 	"context"
+	"fmt"
 	"strings"
 
 	"github.com/apache/servicecomb-service-center/datasource"
@@ -65,8 +66,9 @@ func InitRegistration() {
 	}
 
 	Instance = &discovery.MicroServiceInstance{
-		Status:   discovery.MSI_UP,
-		HostName: util.HostName(),
+		Status:    discovery.MSI_UP,
+		HostName:  util.HostName(),
+		Endpoints: getEndpoints(),
 		HealthCheck: &discovery.HealthCheck{
 			Mode:     discovery.CHECK_BY_HEARTBEAT,
 			Interval: RegistryDefaultLeaseRenewalInterval,
@@ -86,6 +88,19 @@ func InitRegistration() {
 	}
 }
 
+func getEndpoints() []string {
+	hostPort := config.GetString("registry.instance.endpoint",
+		config.GetString("server.host", "127.0.0.1", config.WithStandby("httpaddr")))
+	if strings.LastIndex(hostPort, ":") < 0 {
+		hostPort += ":" + config.GetString("server.port", "30100", config.WithStandby("httpport"))
+	}
+	endpoint := fmt.Sprintf("rest://%s/", hostPort)
+	if config.GetSSL().SslEnabled {
+		endpoint += "?sslEnabled=true"
+	}
+	return []string{endpoint}
+}
+
 func AddDefaultContextValue(ctx context.Context) context.Context {
 	return util.WithNoCache(util.SetContext(util.SetDomainProject(ctx,
 		datasource.RegistryDomain, datasource.RegistryProject),