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

* fix: unable to get the correct port when registering the service
pull/928/head
Tony Chen 4 years ago committed by GitHub
parent e989bb04e3
commit 5d36b6e67c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      app.go

@ -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 {

Loading…
Cancel
Save