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:
parent
e989bb04e3
commit
5d36b6e67c
24
app.go
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…
Reference in New Issue
Block a user