mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-09-13 13:32:04 +08:00
feat: add RegisterIP to API config and implement Redis server registration
This commit is contained in:
parent
d172cfe29d
commit
ad2735a1cb
37
cmd/main.go
37
cmd/main.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
@ -12,6 +13,7 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
@ -41,6 +43,7 @@ import (
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/system/program"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
"github.com/openimsdk/tools/utils/network"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -75,6 +78,7 @@ func main() {
|
||||
putCmd(cmd, true, msgtransfer.Start)
|
||||
putCmd(cmd, true, api.Start)
|
||||
putCmd(cmd, true, cron.Start)
|
||||
putCmd(cmd, true, startRedisServerRegister)
|
||||
ctx := context.Background()
|
||||
if err := cmd.run(ctx); err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "server exit %s", err)
|
||||
@ -434,3 +438,36 @@ func (x *cmdManger) Running() []string {
|
||||
}
|
||||
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
|
||||
# Listening ports; if multiple are configured, multiple instances will be launched, must be consistent with the number of prometheus.ports
|
||||
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
|
||||
compressionLevel: 0
|
||||
|
||||
|
||||
@ -23,13 +23,14 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
conf "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/openimsdk/tools/discovery"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/utils/datautil"
|
||||
"github.com/openimsdk/tools/utils/network"
|
||||
"github.com/openimsdk/tools/utils/runtimeenv"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@ -77,18 +78,6 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
|
||||
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()
|
||||
exitCause := context.Cause(apiCtx)
|
||||
log.ZWarn(ctx, "api server exit", exitCause)
|
||||
|
||||
@ -142,6 +142,7 @@ type API struct {
|
||||
Api struct {
|
||||
ListenIP string `yaml:"listenIP"`
|
||||
Ports []int `yaml:"ports"`
|
||||
RegisterIP string `yaml:"registerIP"`
|
||||
CompressionLevel int `yaml:"compressionLevel"`
|
||||
} `yaml:"api"`
|
||||
Prometheus struct {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user