mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
Add etcd as a service discovery mechanism
This commit is contained in:
parent
3074604c86
commit
f51cdadb57
@ -87,12 +87,27 @@ func (r *SvcDiscoveryRegistryImpl) GetUserIdHashGatewayHost(ctx context.Context,
|
|||||||
|
|
||||||
// GetConns returns gRPC client connections for a given service name
|
// 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("etcd:///%s/%s", r.rootDirectory, serviceName)
|
var conns []*grpc.ClientConn
|
||||||
|
// Construct the full key for the service
|
||||||
|
fullServiceKey := fmt.Sprintf("%s/%s", r.rootDirectory, serviceName)
|
||||||
|
|
||||||
|
// List all endpoints for the service
|
||||||
|
resp, err := r.client.Get(ctx, fullServiceKey, clientv3.WithPrefix())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, kv := range resp.Kvs {
|
||||||
|
endpoint := string(kv.Key[len(fullServiceKey)+1:]) // Extract the endpoint address
|
||||||
|
target := fmt.Sprintf("etcd://%s", endpoint)
|
||||||
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
|
||||||
}
|
}
|
||||||
return []*grpc.ClientConn{conn}, nil
|
conns = append(conns, conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
return conns, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConn returns a single gRPC client connection for a given service name
|
// GetConn returns a single gRPC client connection for a given service name
|
||||||
|
Loading…
x
Reference in New Issue
Block a user