Add etcd as a service discovery mechanism

This commit is contained in:
skiffer-git 2024-05-11 18:06:54 +08:00
parent 7e3722c9c4
commit 676320454a

View File

@ -17,12 +17,14 @@ 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
rpcRegisterTarget string
rootDirectory string rootDirectory string
} }
@ -101,7 +103,9 @@ func (r *SvcDiscoveryRegistryImpl) GetConn(ctx context.Context, serviceName stri
// GetSelfConnTarget returns the connection target for the current service // GetSelfConnTarget returns the connection target for the current service
func (r *SvcDiscoveryRegistryImpl) GetSelfConnTarget() string { func (r *SvcDiscoveryRegistryImpl) GetSelfConnTarget() string {
return fmt.Sprintf("etcd:///%s", r.serviceKey) return r.rpcRegisterTarget
// return fmt.Sprintf("etcd:///%s", r.serviceKey)
} }
// AddOption appends gRPC dial options to the existing options // AddOption appends gRPC dial options to the existing options
@ -131,7 +135,9 @@ func (r *SvcDiscoveryRegistryImpl) Register(serviceName, host string, port int,
} }
r.leaseID = leaseResp.ID r.leaseID = leaseResp.ID
endpoint := endpoints.Endpoint{Addr: fmt.Sprintf("%s:%d", host, port)} r.rpcRegisterTarget = fmt.Sprintf("%s:%d", host, port)
endpoint := endpoints.Endpoint{Addr: r.rpcRegisterTarget}
err = em.AddEndpoint(context.TODO(), r.serviceKey, endpoint, clientv3.WithLease(leaseResp.ID)) err = em.AddEndpoint(context.TODO(), r.serviceKey, endpoint, clientv3.WithLease(leaseResp.ID))
if err != nil { if err != nil {
return err return err