解决grpc start时异步导致端口获取为0的问题

This commit is contained in:
suguo.yao 2021-09-16 13:55:01 +08:00
parent 14cb71f306
commit bc876f20d6
1 changed files with 6 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"sync"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
@ -49,8 +50,9 @@ func Port() int {
func Start() { func Start() {
//注册反射 用于grpcurl调试 //注册反射 用于grpcurl调试
reflection.Register(rpc) reflection.Register(rpc)
wg := sync.WaitGroup{}
// grpc服务启动本局IP支持 wg.Add(1)
// grpc服务启动
go func() { go func() {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", conf.Address, conf.Port)) addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", conf.Address, conf.Port))
if err != nil { if err != nil {
@ -67,5 +69,7 @@ func Start() {
if err != nil { if err != nil {
log.Fatal("fail to open microservice: ", err) log.Fatal("fail to open microservice: ", err)
} }
wg.Done()
}() }()
wg.Wait()
} }