Files
open-im-server/pkg/common/discoveryregister/discoveryregister.go
T

58 lines
2.0 KiB
Go
Raw Normal View History

// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package discoveryregister
import (
2024-03-06 15:58:05 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
2024-05-10 21:30:12 +08:00
getcd "github.com/openimsdk/open-im-server/v3/pkg/common/discoveryregister/etcd"
"github.com/openimsdk/open-im-server/v3/pkg/common/discoveryregister/kubernetes"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/discovery"
"github.com/openimsdk/tools/discovery/zookeeper"
"github.com/openimsdk/tools/errs"
"time"
)
2024-04-19 22:23:08 +08:00
const (
zookeeperConst = "zookeeper"
kubenetesConst = "k8s"
directConst = "direct"
2024-05-10 21:30:12 +08:00
etcd = "etcd"
2024-04-19 22:23:08 +08:00
)
2024-04-19 22:23:08 +08:00
// NewDiscoveryRegister creates a new service discovery and registry client based on the provided environment type.
2024-05-10 21:30:12 +08:00
func NewDiscoveryRegister(zookeeperConfig *config.ZooKeeper, share *config.Share) (discovery.SvcDiscoveryRegistry1, error) {
2024-04-19 22:23:08 +08:00
switch share.Env {
case zookeeperConst:
2024-05-10 21:30:12 +08:00
2024-04-19 22:23:08 +08:00
return zookeeper.NewZkClient(
zookeeperConfig.Address,
zookeeperConfig.Schema,
zookeeper.WithFreq(time.Hour),
zookeeper.WithUserNameAndPassword(zookeeperConfig.Username, zookeeperConfig.Password),
zookeeper.WithRoundRobin(),
zookeeper.WithTimeout(10),
)
case kubenetesConst:
return kubernetes.NewK8sDiscoveryRegister(share.RpcRegisterName.MessageGateway)
2024-05-10 21:30:12 +08:00
case etcd:
2024-05-10 21:30:12 +08:00
return getcd.NewSvcDiscoveryRegistry("openim", []string{"http://localhost:2379"})
2024-04-19 22:23:08 +08:00
case directConst:
//return direct.NewConnDirect(config)
default:
2024-04-19 22:23:08 +08:00
return nil, errs.New("unsupported discovery type", "type", share.Env).Wrap()
}
2024-04-19 22:23:08 +08:00
return nil, nil
}