mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-05-22 04:39:19 +08:00
Add etcd as a service discovery mechanism
This commit is contained in:
parent
a4d6b2a3b9
commit
db906fcea0
@ -1,16 +1,13 @@
|
|||||||
|
|
||||||
enable: "etcd"
|
enable: "etcd"
|
||||||
etcd:
|
etcd:
|
||||||
|
rootDirectory: openim
|
||||||
address: [ localhost:12379 ]
|
address: [ localhost:12379 ]
|
||||||
username: ''
|
username: ''
|
||||||
password: ''
|
password: ''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
zookeeper:
|
zookeeper:
|
||||||
schema: openim
|
schema: openim
|
||||||
address: [ localhost:12181 ]
|
address: [ localhost:12181 ]
|
||||||
username: ''
|
username: ''
|
||||||
password: ''
|
password: ''
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,13 +56,14 @@ type MsgTransfer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
MsgTransfer config.MsgTransfer
|
MsgTransfer config.MsgTransfer
|
||||||
RedisConfig config.Redis
|
RedisConfig config.Redis
|
||||||
MongodbConfig config.Mongo
|
MongodbConfig config.Mongo
|
||||||
KafkaConfig config.Kafka
|
KafkaConfig config.Kafka
|
||||||
Share config.Share
|
ZookeeperConfig config.ZooKeeper
|
||||||
WebhooksConfig config.Webhooks
|
Share config.Share
|
||||||
Discovery config.Discovery
|
WebhooksConfig config.Webhooks
|
||||||
|
Discovery config.Discovery
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(ctx context.Context, index int, config *Config) error {
|
func Start(ctx context.Context, index int, config *Config) error {
|
||||||
|
@ -48,9 +48,10 @@ type conversationServer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
RpcConfig config.Conversation
|
RpcConfig config.Conversation
|
||||||
RedisConfig config.Redis
|
RedisConfig config.Redis
|
||||||
MongodbConfig config.Mongo
|
MongodbConfig config.Mongo
|
||||||
|
// ZookeeperConfig config.ZooKeeper
|
||||||
NotificationConfig config.Notification
|
NotificationConfig config.Notification
|
||||||
Share config.Share
|
Share config.Share
|
||||||
LocalCacheConfig config.LocalCache
|
LocalCacheConfig config.LocalCache
|
||||||
|
@ -24,33 +24,31 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
zookeeperConst = "zookeeper"
|
|
||||||
kubenetesConst = "k8s"
|
|
||||||
directConst = "direct"
|
|
||||||
etcdConst = "etcd"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewDiscoveryRegister creates a new service discovery and registry client based on the provided environment type.
|
// NewDiscoveryRegister creates a new service discovery and registry client based on the provided environment type.
|
||||||
func NewDiscoveryRegister(zookeeperConfig *config.ZooKeeper, share *config.Share) (discovery.SvcDiscoveryRegistry, error) {
|
func NewDiscoveryRegister(discovery *config.Discovery, share *config.Share) (discovery.SvcDiscoveryRegistry, error) {
|
||||||
switch share.Env {
|
switch discovery.Enable {
|
||||||
case zookeeperConst:
|
case "zookeeper":
|
||||||
return zookeeper.NewZkClient(
|
return zookeeper.NewZkClient(
|
||||||
zookeeperConfig.Address,
|
discovery.ZooKeeper.Address,
|
||||||
zookeeperConfig.Schema,
|
discovery.ZooKeeper.Schema,
|
||||||
zookeeper.WithFreq(time.Hour),
|
zookeeper.WithFreq(time.Hour),
|
||||||
zookeeper.WithUserNameAndPassword(zookeeperConfig.Username, zookeeperConfig.Password),
|
zookeeper.WithUserNameAndPassword(discovery.ZooKeeper.Username, discovery.ZooKeeper.Password),
|
||||||
zookeeper.WithRoundRobin(),
|
zookeeper.WithRoundRobin(),
|
||||||
zookeeper.WithTimeout(10),
|
zookeeper.WithTimeout(10),
|
||||||
)
|
)
|
||||||
case kubenetesConst:
|
case "k8s":
|
||||||
return kubernetes.NewK8sDiscoveryRegister(share.RpcRegisterName.MessageGateway)
|
return kubernetes.NewK8sDiscoveryRegister(share.RpcRegisterName.MessageGateway)
|
||||||
case etcdConst:
|
case "etcd":
|
||||||
return getcd.NewSvcDiscoveryRegistry("etcd", []string{"localhost:2379"})
|
return getcd.NewSvcDiscoveryRegistry(
|
||||||
case directConst:
|
discovery.Etcd.RootDirectory,
|
||||||
|
discovery.Etcd.Address,
|
||||||
|
getcd.WithDialTimeout(10*time.Second),
|
||||||
|
getcd.WithMaxCallSendMsgSize(20*1024*1024),
|
||||||
|
getcd.WithUsernameAndPassword(discovery.Etcd.Username, discovery.Etcd.Password))
|
||||||
|
case "direct":
|
||||||
//return direct.NewConnDirect(config)
|
//return direct.NewConnDirect(config)
|
||||||
default:
|
default:
|
||||||
return nil, errs.New("unsupported discovery type", "type", share.Env).Wrap()
|
return nil, errs.New("unsupported discovery type", "type", discovery.Enable).Wrap()
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -8,27 +8,38 @@ import (
|
|||||||
"go.etcd.io/etcd/client/v3/naming/resolver"
|
"go.etcd.io/etcd/client/v3/naming/resolver"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
gresolver "google.golang.org/grpc/resolver"
|
gresolver "google.golang.org/grpc/resolver"
|
||||||
|
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ZkOption defines a function type for modifying clientv3.Config
|
||||||
|
type ZkOption func(*clientv3.Config)
|
||||||
|
|
||||||
// SvcDiscoveryRegistryImpl implementation
|
// SvcDiscoveryRegistryImpl implementation
|
||||||
type SvcDiscoveryRegistryImpl struct {
|
type SvcDiscoveryRegistryImpl struct {
|
||||||
client *clientv3.Client
|
client *clientv3.Client
|
||||||
resolver gresolver.Builder
|
resolver gresolver.Builder
|
||||||
dialOptions []grpc.DialOption
|
dialOptions []grpc.DialOption
|
||||||
serviceKey string
|
serviceKey string
|
||||||
endpointMgr endpoints.Manager
|
endpointMgr endpoints.Manager
|
||||||
leaseID clientv3.LeaseID
|
leaseID clientv3.LeaseID
|
||||||
schema string
|
rootDirectory string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSvcDiscoveryRegistry(schema string, endpoints []string) (*SvcDiscoveryRegistryImpl, error) {
|
// NewSvcDiscoveryRegistry creates a new service discovery registry implementation
|
||||||
|
func NewSvcDiscoveryRegistry(rootDirectory string, endpoints []string, options ...ZkOption) (*SvcDiscoveryRegistryImpl, error) {
|
||||||
cfg := clientv3.Config{
|
cfg := clientv3.Config{
|
||||||
Endpoints: endpoints,
|
Endpoints: endpoints,
|
||||||
DialTimeout: 5 * time.Second,
|
DialTimeout: 5 * time.Second,
|
||||||
|
// Increase keep-alive queue capacity and message size
|
||||||
|
PermitWithoutStream: true,
|
||||||
|
MaxCallSendMsgSize: 10 * 1024 * 1024, // 10 MB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply provided options to the config
|
||||||
|
for _, opt := range options {
|
||||||
|
opt(&cfg)
|
||||||
|
}
|
||||||
|
|
||||||
client, err := clientv3.New(cfg)
|
client, err := clientv3.New(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -38,17 +49,42 @@ func NewSvcDiscoveryRegistry(schema string, endpoints []string) (*SvcDiscoveryRe
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &SvcDiscoveryRegistryImpl{
|
return &SvcDiscoveryRegistryImpl{
|
||||||
client: client,
|
client: client,
|
||||||
resolver: r,
|
resolver: r,
|
||||||
schema: schema,
|
rootDirectory: rootDirectory,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithDialTimeout sets a custom dial timeout for the etcd client
|
||||||
|
func WithDialTimeout(timeout time.Duration) ZkOption {
|
||||||
|
return func(cfg *clientv3.Config) {
|
||||||
|
cfg.DialTimeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMaxCallSendMsgSize sets a custom max call send message size for the etcd client
|
||||||
|
func WithMaxCallSendMsgSize(size int) ZkOption {
|
||||||
|
return func(cfg *clientv3.Config) {
|
||||||
|
cfg.MaxCallSendMsgSize = size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithUsernameAndPassword sets a username and password for the etcd client
|
||||||
|
func WithUsernameAndPassword(username, password string) ZkOption {
|
||||||
|
return func(cfg *clientv3.Config) {
|
||||||
|
cfg.Username = username
|
||||||
|
cfg.Password = password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUserIdHashGatewayHost returns the gateway host for a given user ID hash
|
||||||
func (r *SvcDiscoveryRegistryImpl) GetUserIdHashGatewayHost(ctx context.Context, userId string) (string, error) {
|
func (r *SvcDiscoveryRegistryImpl) GetUserIdHashGatewayHost(ctx context.Context, userId string) (string, error) {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetConns returns gRPC client connections for a given service name
|
||||||
func (r *SvcDiscoveryRegistryImpl) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {
|
func (r *SvcDiscoveryRegistryImpl) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {
|
||||||
target := fmt.Sprintf("%s:///%s", r.schema, serviceName)
|
target := fmt.Sprintf("etcd:///%s", serviceName)
|
||||||
conn, err := grpc.DialContext(ctx, target, append(append(r.dialOptions, opts...), grpc.WithResolvers(r.resolver))...)
|
conn, err := grpc.DialContext(ctx, target, append(append(r.dialOptions, opts...), grpc.WithResolvers(r.resolver))...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -56,34 +92,39 @@ func (r *SvcDiscoveryRegistryImpl) GetConns(ctx context.Context, serviceName str
|
|||||||
return []*grpc.ClientConn{conn}, nil
|
return []*grpc.ClientConn{conn}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetConn returns a single gRPC client connection for a given service name
|
||||||
func (r *SvcDiscoveryRegistryImpl) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
func (r *SvcDiscoveryRegistryImpl) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||||
target := fmt.Sprintf("%s:///%s", r.schema, serviceName)
|
target := fmt.Sprintf("etcd:///%s", serviceName)
|
||||||
return grpc.DialContext(ctx, target, append(append(r.dialOptions, opts...), grpc.WithResolvers(r.resolver))...)
|
return grpc.DialContext(ctx, target, append(append(r.dialOptions, opts...), grpc.WithResolvers(r.resolver))...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSelfConnTarget returns the connection target for the current service
|
||||||
func (r *SvcDiscoveryRegistryImpl) GetSelfConnTarget() string {
|
func (r *SvcDiscoveryRegistryImpl) GetSelfConnTarget() string {
|
||||||
return fmt.Sprintf("%s:///%s", r.schema, r.serviceKey)
|
return fmt.Sprintf("etcd:///%s", r.serviceKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddOption appends gRPC dial options to the existing options
|
||||||
func (r *SvcDiscoveryRegistryImpl) AddOption(opts ...grpc.DialOption) {
|
func (r *SvcDiscoveryRegistryImpl) AddOption(opts ...grpc.DialOption) {
|
||||||
r.dialOptions = append(r.dialOptions, opts...)
|
r.dialOptions = append(r.dialOptions, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CloseConn closes a given gRPC client connection
|
||||||
func (r *SvcDiscoveryRegistryImpl) CloseConn(conn *grpc.ClientConn) {
|
func (r *SvcDiscoveryRegistryImpl) CloseConn(conn *grpc.ClientConn) {
|
||||||
if err := conn.Close(); err != nil {
|
if err := conn.Close(); err != nil {
|
||||||
log.Printf("Failed to close connection: %v", err)
|
fmt.Printf("Failed to close connection: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register registers a new service endpoint with etcd
|
||||||
func (r *SvcDiscoveryRegistryImpl) Register(serviceName, host string, port int, opts ...grpc.DialOption) error {
|
func (r *SvcDiscoveryRegistryImpl) Register(serviceName, host string, port int, opts ...grpc.DialOption) error {
|
||||||
r.serviceKey = fmt.Sprintf("%s/%s-%d", serviceName, host, port)
|
r.serviceKey = fmt.Sprintf("%s/%s/%s-%d", r.rootDirectory, serviceName, host, port)
|
||||||
em, err := endpoints.NewManager(r.client, serviceName)
|
em, err := endpoints.NewManager(r.client, r.rootDirectory+"/"+serviceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.endpointMgr = em
|
r.endpointMgr = em
|
||||||
|
|
||||||
leaseResp, err := r.client.Grant(context.Background(), 30)
|
leaseResp, err := r.client.Grant(context.Background(), 60) // Increase TTL time
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -100,10 +141,11 @@ func (r *SvcDiscoveryRegistryImpl) Register(serviceName, host string, port int,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// keepAliveLease maintains the lease alive by sending keep-alive requests
|
||||||
func (r *SvcDiscoveryRegistryImpl) keepAliveLease(leaseID clientv3.LeaseID) {
|
func (r *SvcDiscoveryRegistryImpl) keepAliveLease(leaseID clientv3.LeaseID) {
|
||||||
ch, err := r.client.KeepAlive(context.Background(), leaseID)
|
ch, err := r.client.KeepAlive(context.Background(), leaseID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to keep lease alive: %v", err)
|
fmt.Printf("Failed to keep lease alive: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,12 +153,13 @@ func (r *SvcDiscoveryRegistryImpl) keepAliveLease(leaseID clientv3.LeaseID) {
|
|||||||
if ka != nil {
|
if ka != nil {
|
||||||
fmt.Printf("Received lease keep-alive response: %v\n", ka)
|
fmt.Printf("Received lease keep-alive response: %v\n", ka)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Lease keep-alive response channel closed")
|
fmt.Printf("Lease keep-alive response channel closed\n")
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnRegister removes the service endpoint from etcd
|
||||||
func (r *SvcDiscoveryRegistryImpl) UnRegister() error {
|
func (r *SvcDiscoveryRegistryImpl) UnRegister() error {
|
||||||
if r.endpointMgr == nil {
|
if r.endpointMgr == nil {
|
||||||
return fmt.Errorf("endpoint manager is not initialized")
|
return fmt.Errorf("endpoint manager is not initialized")
|
||||||
@ -124,6 +167,7 @@ func (r *SvcDiscoveryRegistryImpl) UnRegister() error {
|
|||||||
return r.endpointMgr.DeleteEndpoint(context.TODO(), r.serviceKey)
|
return r.endpointMgr.DeleteEndpoint(context.TODO(), r.serviceKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes the etcd client connection
|
||||||
func (r *SvcDiscoveryRegistryImpl) Close() {
|
func (r *SvcDiscoveryRegistryImpl) Close() {
|
||||||
if r.client != nil {
|
if r.client != nil {
|
||||||
_ = r.client.Close()
|
_ = r.client.Close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user