Skip to content

Commit

Permalink
feat: add zk and redis mongo env
Browse files Browse the repository at this point in the history
Signed-off-by: Xinwei Xiong (cubxxw) <[email protected]>
  • Loading branch information
cubxxw committed Dec 13, 2023
1 parent 6271a61 commit d61495b
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 466 deletions.
1 change: 1 addition & 0 deletions pkg/common/db/unrelation/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewMongo() (*Mongo, error) {
var mongoClient *mongo.Client
var err error

// Retry connecting to MongoDB
for i := 0; i <= maxRetry; i++ {
ctx, cancel := context.WithTimeout(context.Background(), mongoConnTimeout)
defer cancel()
Expand Down
85 changes: 7 additions & 78 deletions pkg/common/discoveryregister/discoveryregister.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,22 @@
package discoveryregister

import (
"context"
"errors"
"fmt"
"time"

"github.com/OpenIMSDK/tools/discoveryregistry"
openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper"
"github.com/OpenIMSDK/tools/log"
"google.golang.org/grpc"
"github.com/openimsdk/open-im-server/v3/pkg/common/discoveryregister/kubernetes"
"github.com/openimsdk/open-im-server/v3/pkg/common/discoveryregister/zookeeper"

"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/OpenIMSDK/tools/discoveryregistry"
)

// NewDiscoveryRegister creates a new service discovery and registry client based on the provided environment type.
func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistry, error) {
var client discoveryregistry.SvcDiscoveryRegistry
var err error
switch envType {
case "zookeeper":
client, err = openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
openkeeper.WithFreq(time.Hour), openkeeper.WithUserNameAndPassword(
config.Config.Zookeeper.Username,
config.Config.Zookeeper.Password,
), openkeeper.WithRoundRobin(), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))
return zookeeper.NewZookeeperDiscoveryRegister()
case "k8s":
client, err = NewK8sDiscoveryRegister()
return kubernetes.NewK8sDiscoveryRegister()
default:
client = nil
err = errors.New("envType not correct")
return nil, errors.New("envType not correct")
}
return client, err
}

type K8sDR struct {
options []grpc.DialOption
rpcRegisterAddr string
}

func NewK8sDiscoveryRegister() (discoveryregistry.SvcDiscoveryRegistry, error) {
return &K8sDR{}, nil
}

func (cli *K8sDR) Register(serviceName, host string, port int, opts ...grpc.DialOption) error {
cli.rpcRegisterAddr = serviceName
return nil
}
func (cli *K8sDR) UnRegister() error {

return nil
}
func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error {

return nil
}
func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error {

return nil
}

func (cli *K8sDR) GetConfFromRegistry(key string) ([]byte, error) {

return nil, nil
}
func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {

conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...)
return []*grpc.ClientConn{conn}, err
}
func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {

return grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...)
}
func (cli *K8sDR) GetSelfConnTarget() string {

return cli.rpcRegisterAddr
}
func (cli *K8sDR) AddOption(opts ...grpc.DialOption) {
cli.options = append(cli.options, opts...)
}
func (cli *K8sDR) CloseConn(conn *grpc.ClientConn) {
conn.Close()
}

// do not use this method for call rpc
func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn {
fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!")
return nil
}
func (cli *K8sDR) Close() {
return
}
Loading

0 comments on commit d61495b

Please sign in to comment.