1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-07 23:02:12 +02:00

kratos: fix: unable to get the correct port when registering the service (#925)

* fix: unable to get the correct port when registering the service
This commit is contained in:
Tony Chen 2021-05-17 23:04:22 +08:00 committed by GitHub
parent e989bb04e3
commit 5d36b6e67c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

24
app.go
View File

@ -5,7 +5,9 @@ import (
"errors"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
@ -64,6 +66,9 @@ func (a *App) Run() error {
return srv.Start()
})
}
if err := a.waitForReady(ctx); err != nil {
return err
}
if a.opts.registrar != nil {
if err := a.opts.registrar.Register(a.opts.ctx, a.instance); err != nil {
return err
@ -100,6 +105,25 @@ func (a *App) Stop() error {
return nil
}
func (a *App) waitForReady(ctx context.Context) error {
retry:
for _, srv := range a.opts.servers {
if ctx.Err() != nil {
return ctx.Err()
}
e, err := srv.Endpoint()
if err != nil {
return err
}
if strings.HasSuffix(e, ":0") {
time.Sleep(time.Millisecond * 100)
goto retry
}
}
a.instance = buildInstance(a.opts)
return nil
}
func buildInstance(o options) *registry.ServiceInstance {
if len(o.endpoints) == 0 {
for _, srv := range o.servers {