Repository: go-kratos/kratos (go)

Issue:
最新版本服务注册随机端口为0
#### kratos版本： v2.0.0-rc2, v2.0.0-rc3

配置随机端口，在服务注册的时候，端口又变回0了。是不是这里：
```golang
	wg := sync.WaitGroup{}
	for _, srv := range a.opts.servers {
		srv := srv
		eg.Go(func() error {
			<-ctx.Done() // wait for stop signal
			return srv.Stop(ctx)
		})
		wg.Add(1)
		eg.Go(func() error {
			wg.Done()
			return srv.Start(ctx)
		})
	}
	wg.Wait()
```
App Run 方法里面的`WaitGroup`先调用了`wg.Done()`，再Start导致下面的`wg.Wait()`直接就往下走了，但端口还没获取到。

Pull request:
transport: fix endpoint extract
fix #1058 

Patch:
diff --git a/internal/host/host.go b/internal/host/host.go
index 0014c0a2..193056d6 100644
--- a/internal/host/host.go
+++ b/internal/host/host.go
@@ -1,6 +1,7 @@
 package host
 
 import (
+	"fmt"
 	"net"
 	"strconv"
 )
@@ -27,6 +28,8 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
 	if lis != nil {
 		if p, ok := Port(lis); ok {
 			port = strconv.Itoa(p)
+		} else {
+			return "", fmt.Errorf("failed to extract port: %v", lis.Addr())
 		}
 	}
 	if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]" && addr != "::") {
diff --git a/transport/grpc/server.go b/transport/grpc/server.go
index 5710c824..21868f2a 100644
--- a/transport/grpc/server.go
+++ b/transport/grpc/server.go
@@ -138,7 +138,7 @@ func (s *Server) Endpoint() (*url.URL, error) {
 			s.err = err
 			return
 		}
-		addr, err := host.Extract(s.address, s.lis)
+		addr, err := host.Extract(s.address, lis)
 		if err != nil {
 			lis.Close()
 			s.err = err
diff --git a/transport/http/server.go b/transport/http/server.go
index 99266ef4..aa7acbf2 100644
--- a/transport/http/server.go
+++ b/transport/http/server.go
@@ -190,7 +190,7 @@ func (s *Server) Endpoint() (*url.URL, error) {
 			s.err = err
 			return
 		}
-		addr, err := host.Extract(s.address, s.lis)
+		addr, err := host.Extract(s.address, lis)
 		if err != nil {
 			lis.Close()
 			s.err = err


Test patch:
diff --git a/transport/grpc/server_test.go b/transport/grpc/server_test.go
index c2901dc0..1f281adc 100644
--- a/transport/grpc/server_test.go
+++ b/transport/grpc/server_test.go
@@ -2,11 +2,9 @@ package grpc
 
 import (
 	"context"
-	"fmt"
+	"strings"
 	"testing"
 	"time"
-
-	"github.com/go-kratos/kratos/v2/internal/host"
 )
 
 type testKey struct{}
@@ -15,7 +13,8 @@ func TestServer(t *testing.T) {
 	ctx := context.Background()
 	ctx = context.WithValue(ctx, testKey{}, "test")
 	srv := NewServer()
-	if e, err := srv.Endpoint(); err != nil || e == nil {
+
+	if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
 		t.Fatal(e, err)
 	}
 
@@ -31,13 +30,12 @@ func TestServer(t *testing.T) {
 }
 
 func testClient(t *testing.T, srv *Server) {
-	port, ok := host.Port(srv.lis)
-	if !ok {
-		t.Fatalf("extract port error: %v", srv.lis)
+	u, err := srv.Endpoint()
+	if err != nil {
+		t.Fatal(err)
 	}
-	endpoint := fmt.Sprintf("127.0.0.1:%d", port)
 	// new a gRPC client
-	conn, err := DialInsecure(context.Background(), WithEndpoint(endpoint))
+	conn, err := DialInsecure(context.Background(), WithEndpoint(u.Host))
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/transport/http/server_test.go b/transport/http/server_test.go
index 12783bef..e0e1894c 100644
--- a/transport/http/server_test.go
+++ b/transport/http/server_test.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"strings"
 	"testing"
 	"time"
 
@@ -33,7 +34,7 @@ func TestServer(t *testing.T) {
 	srv := NewServer()
 	srv.HandleFunc("/index", fn)
 
-	if e, err := srv.Endpoint(); err != nil || e == nil {
+	if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
 		t.Fatal(e, err)
 	}
 
@@ -58,17 +59,17 @@ func testClient(t *testing.T, srv *Server) {
 		{"PATCH", "/index"},
 		{"DELETE", "/index"},
 	}
-	port, ok := host.Port(srv.lis)
-	if !ok {
-		t.Fatalf("extract port error: %v", srv.lis)
+	e, err := srv.Endpoint()
+	if err != nil {
+		t.Fatal(err)
 	}
-	client, err := NewClient(context.Background(), WithEndpoint(fmt.Sprintf("127.0.0.1:%d", port)))
+	client, err := NewClient(context.Background(), WithEndpoint(e.Host))
 	if err != nil {
 		t.Fatal(err)
 	}
 	for _, test := range tests {
 		var res testData
-		url := fmt.Sprintf("http://127.0.0.1:%d%s", port, test.path)
+		url := fmt.Sprintf(e.String() + test.path)
 		req, err := http.NewRequest(test.method, url, nil)
 		if err != nil {
 			t.Fatal(err)