feat: add RegisterIP to API config and implement Redis server registration

pull/3759/head
withchao 2 months ago
parent d172cfe29d
commit ad2735a1cb

@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"flag" "flag"
"fmt" "fmt"
"net" "net"
@ -12,6 +13,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"strconv"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@ -41,6 +43,7 @@ import (
"github.com/openimsdk/tools/log" "github.com/openimsdk/tools/log"
"github.com/openimsdk/tools/system/program" "github.com/openimsdk/tools/system/program"
"github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/network"
) )
func init() { func init() {
@ -75,6 +78,7 @@ func main() {
putCmd(cmd, true, msgtransfer.Start) putCmd(cmd, true, msgtransfer.Start)
putCmd(cmd, true, api.Start) putCmd(cmd, true, api.Start)
putCmd(cmd, true, cron.Start) putCmd(cmd, true, cron.Start)
putCmd(cmd, true, startRedisServerRegister)
ctx := context.Background() ctx := context.Background()
if err := cmd.run(ctx); err != nil { if err := cmd.run(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "server exit %s", err) _, _ = fmt.Fprintf(os.Stderr, "server exit %s", err)
@ -434,3 +438,36 @@ func (x *cmdManger) Running() []string {
} }
return names return names
} }
type serverConfig struct {
API config.API
Share config.Share
RedisConfig config.Redis
Index config.Index
}
func startRedisServerRegister(ctx context.Context, cfg *serverConfig, client discovery.SvcDiscoveryRegistry, service grpc.ServiceRegistrar) error {
apiPort, err := datautil.GetElemByIndex(cfg.API.Api.Ports, int(cfg.Index))
if err != nil {
return err
}
if apiPort <= 0 {
return errors.New("standalone api port is 0")
}
registerIP, err := network.GetRpcRegisterIP(cfg.API.Api.RegisterIP)
if err != nil {
return err
}
addr := net.JoinHostPort(registerIP, strconv.Itoa(apiPort))
inprocess.SetLocalTarget(addr)
timer := time.NewTimer(time.Second * 5)
defer timer.Stop()
for {
select {
case <-timer.C:
case <-ctx.Done():
return context.Cause(ctx)
}
}
}

@ -3,6 +3,8 @@ api:
listenIP: 0.0.0.0 listenIP: 0.0.0.0
# Listening ports; if multiple are configured, multiple instances will be launched, must be consistent with the number of prometheus.ports # Listening ports; if multiple are configured, multiple instances will be launched, must be consistent with the number of prometheus.ports
ports: [ 10002 ] ports: [ 10002 ]
# Fallback for IP resolution issues in multi-instance standalone mode.
# registerIP:
# API compression level; 0: default compression, 1: best compression, 2: best speed, -1: no compression # API compression level; 0: default compression, 1: best compression, 2: best speed, -1: no compression
compressionLevel: 0 compressionLevel: 0

@ -23,13 +23,14 @@ import (
"strconv" "strconv"
"time" "time"
"google.golang.org/grpc"
conf "github.com/openimsdk/open-im-server/v3/pkg/common/config" conf "github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/tools/discovery" "github.com/openimsdk/tools/discovery"
"github.com/openimsdk/tools/log" "github.com/openimsdk/tools/log"
"github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/network" "github.com/openimsdk/tools/utils/network"
"github.com/openimsdk/tools/utils/runtimeenv" "github.com/openimsdk/tools/utils/runtimeenv"
"google.golang.org/grpc"
) )
type Config struct { type Config struct {
@ -77,18 +78,6 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
apiCancel(err) apiCancel(err)
}() }()
//if config.Discovery.Enable == conf.ETCD {
// cm := disetcd.NewConfigManager(client.(*etcd.SvcDiscoveryRegistryImpl).GetClient(), config.GetConfigNames())
// cm.Watch(ctx)
//}
//sigs := make(chan os.Signal, 1)
//signal.Notify(sigs, syscall.SIGTERM)
//select {
//case val := <-sigs:
// log.ZDebug(ctx, "recv exit", "signal", val.String())
// cancel(fmt.Errorf("signal %s", val.String()))
//case <-ctx.Done():
//}
<-apiCtx.Done() <-apiCtx.Done()
exitCause := context.Cause(apiCtx) exitCause := context.Cause(apiCtx)
log.ZWarn(ctx, "api server exit", exitCause) log.ZWarn(ctx, "api server exit", exitCause)

@ -142,6 +142,7 @@ type API struct {
Api struct { Api struct {
ListenIP string `yaml:"listenIP"` ListenIP string `yaml:"listenIP"`
Ports []int `yaml:"ports"` Ports []int `yaml:"ports"`
RegisterIP string `yaml:"registerIP"`
CompressionLevel int `yaml:"compressionLevel"` CompressionLevel int `yaml:"compressionLevel"`
} `yaml:"api"` } `yaml:"api"`
Prometheus struct { Prometheus struct {

Loading…
Cancel
Save