mirror of
https://github.com/go-kratos/kratos.git
synced 2026-05-16 09:48:28 +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:
@@ -5,7 +5,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-kratos/kratos/v2/log"
|
"github.com/go-kratos/kratos/v2/log"
|
||||||
"github.com/go-kratos/kratos/v2/registry"
|
"github.com/go-kratos/kratos/v2/registry"
|
||||||
@@ -64,6 +66,9 @@ func (a *App) Run() error {
|
|||||||
return srv.Start()
|
return srv.Start()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if err := a.waitForReady(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if a.opts.registrar != nil {
|
if a.opts.registrar != nil {
|
||||||
if err := a.opts.registrar.Register(a.opts.ctx, a.instance); err != nil {
|
if err := a.opts.registrar.Register(a.opts.ctx, a.instance); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -100,6 +105,25 @@ func (a *App) Stop() error {
|
|||||||
return nil
|
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 {
|
func buildInstance(o options) *registry.ServiceInstance {
|
||||||
if len(o.endpoints) == 0 {
|
if len(o.endpoints) == 0 {
|
||||||
for _, srv := range o.servers {
|
for _, srv := range o.servers {
|
||||||
|
|||||||
Reference in New Issue
Block a user