etcd

package module
v1.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 9 Imported by: 0

README

English  |  中文

goner/etcd Component

Component Overview

The goner/etcd component provides service registration and discovery capabilities for the Gone framework based on etcd. This integration offers a reliable solution for service management in distributed systems.

With the goner/etcd component, you can:

  • Register and discover services in distributed environments
  • Implement service health checks and automatic failover
  • Build highly available microservice architectures
  • Leverage etcd's strong consistency for service coordination

Features

Service Registration & Discovery
  • Service Registration: Register service instances to etcd service directory
  • Service Discovery: Get available service instances from etcd
  • Service Monitoring: Watch service instance changes and update service lists in real-time
  • Health Check: TTL-based health check mechanism to automatically maintain service health status
  • Strong Consistency: Utilize etcd's Raft consensus algorithm for data consistency

Configuration Reference

Client Configuration

Basic parameters for etcd client behavior:

Parameter Description Type Default Example
etcd.endpoints etcd server addresses []string ["127.0.0.1:2379"] ["localhost:2379"]
etcd.username etcd authentication username string "" "username"
etcd.password etcd authentication password string "" "password"
etcd.dial-timeout Connection timeout time.Duration "5s" "10s"
etcd.keepalive-ttl Health check TTL time.Duration "10s" "20s"

For more configurations, refer to etcd official documentation

Supported configuration loading and injection

Component name: etcd.config

    gone.
        NewApp(
            //... 
        ).
        Loads(g.NamedThirdComponentLoadFunc("etcd.config", &etcd.Config{
            Endpoints: []string{"localhost:2379"},
            // Other configurations
        }))
Service Registration Configuration

Service registration parameters:

  • gin.http server
Parameter Description Type Default Example
service.name Service name string - "user-service"
service.host Service address string - "192.168.1.100"
service.port Service port int - 8080
service.service-use-subnet Subnet used string 0.0.0.0/0 192.168.1.0/24
  • grpc server | Parameter | Description | Type | Default | Example | |-----------|-------------|------|---------|---------| | service.grpc.name | Service name | string | - | "user-service" | | service.grpc.host | Service address | string | - | "192.168.1.100" | | service.grpc.port | Service port | int | - | 8080 | | service.grpc.service-use-subnet | Subnet used | string | 0.0.0.0/0 | 192.168.1.0/24 |

Implementation Guide

Configuration File Setup

Create default.yaml in project's config directory to define etcd client connection parameters:

etcd:
  # Basic configurations
  endpoints: ["localhost:2379"]  # etcd server addresses
  dialTimeout: "5s"              # Connection timeout
  username: ""                   # Authentication username
  password: ""                   # Authentication password
  autoSyncInterval: "30s"        # Endpoint auto-sync interval
Code Integration
Service Registration & Discovery
func main() {
    // Initialize app and load etcd component
    gone.NewApp(etcd.RegistryLoad).Run(func(params struct {
        registry g.ServiceRegistry `gone:"*"`
        discovery g.ServiceDiscovery `gone:"*"`
    }) {
        // Create service instance
        service := g.NewService(
            "my-service",       // Service name
            "192.168.1.100",   // Service IP
            8080,               // Service port
            map[string]string{  // Metadata
                "version": "1.0.0",
            },
            true,               // Health status
            1.0,                // Weight
        )

        // After registration, component will automatically maintain TTL health check (default TTL: 10s)

        // Register service
        err := params.registry.Register(service)
        if err != nil {
            log.Fatalf("Service registration failed: %v", err)
        }

        // Discover services
        instances, err := params.discovery.GetInstances("target-service")
        if err != nil {
            log.Fatalf("Service discovery failed: %v", err)
        }

        // Watch service changes
        ch, stop, err := params.discovery.Watch("target-service")
        if err != nil {
            log.Fatalf("Failed to watch service: %v", err)
        }

        go func() {
            for services := range ch {
                // Handle service list updates
                fmt.Printf("Service list updated: %v\n", services)
            }
        }()

        // Deregister service and stop watching when app exits
        defer func() {
            _ = params.registry.Deregister(service)
            _ = stop()
        }()
        // Main application logic...
    })
}

Documentation

Overview

Package etcd is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientLoad

func ClientLoad(loader gone.Loader) error

func ProvideEtecd3Client

func ProvideEtecd3Client(_ string, param struct {
	config *etcd3.Config `gone:"config,etcd"`
	conf   *etcd3.Config `gone:"etcd.config" option:"allowNil"`
}) (*etcd3.Client, error)

func RegistryLoad

func RegistryLoad(loader gone.Loader) error

Types

type MockKV

type MockKV struct {
	// contains filtered or unexported fields
}

MockKV is a mock of KV interface.

func NewMockKV

func NewMockKV(ctrl *gomock.Controller) *MockKV

NewMockKV creates a new mock instance.

func (*MockKV) Compact

func (m *MockKV) Compact(ctx context.Context, rev int64, opts ...clientv3.CompactOption) (*clientv3.CompactResponse, error)

Compact mocks base method.

func (*MockKV) Delete

func (m *MockKV) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error)

Delete mocks base method.

func (*MockKV) Do

Do mocks base method.

func (*MockKV) EXPECT

func (m *MockKV) EXPECT() *MockKVMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockKV) Get

func (m *MockKV) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error)

Get mocks base method.

func (*MockKV) Put

func (m *MockKV) Put(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error)

Put mocks base method.

func (*MockKV) Txn

func (m *MockKV) Txn(ctx context.Context) clientv3.Txn

Txn mocks base method.

type MockKVMockRecorder

type MockKVMockRecorder struct {
	// contains filtered or unexported fields
}

MockKVMockRecorder is the mock recorder for MockKV.

func (*MockKVMockRecorder) Compact

func (mr *MockKVMockRecorder) Compact(ctx, rev any, opts ...any) *gomock.Call

Compact indicates an expected call of Compact.

func (*MockKVMockRecorder) Delete

func (mr *MockKVMockRecorder) Delete(ctx, key any, opts ...any) *gomock.Call

Delete indicates an expected call of Delete.

func (*MockKVMockRecorder) Do

func (mr *MockKVMockRecorder) Do(ctx, op any) *gomock.Call

Do indicates an expected call of Do.

func (*MockKVMockRecorder) Get

func (mr *MockKVMockRecorder) Get(ctx, key any, opts ...any) *gomock.Call

Get indicates an expected call of Get.

func (*MockKVMockRecorder) Put

func (mr *MockKVMockRecorder) Put(ctx, key, val any, opts ...any) *gomock.Call

Put indicates an expected call of Put.

func (*MockKVMockRecorder) Txn

func (mr *MockKVMockRecorder) Txn(ctx any) *gomock.Call

Txn indicates an expected call of Txn.

type MockLease

type MockLease struct {
	// contains filtered or unexported fields
}

MockLease is a mock of Lease interface.

func NewMockLease

func NewMockLease(ctrl *gomock.Controller) *MockLease

NewMockLease creates a new mock instance.

func (*MockLease) Close

func (m *MockLease) Close() error

Close mocks base method.

func (*MockLease) EXPECT

func (m *MockLease) EXPECT() *MockLeaseMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockLease) Grant

Grant mocks base method.

func (*MockLease) KeepAlive

func (m *MockLease) KeepAlive(ctx context.Context, id clientv3.LeaseID) (<-chan *clientv3.LeaseKeepAliveResponse, error)

KeepAlive mocks base method.

func (*MockLease) KeepAliveOnce

KeepAliveOnce mocks base method.

func (*MockLease) Leases

Leases mocks base method.

func (*MockLease) Revoke

Revoke mocks base method.

func (*MockLease) TimeToLive

TimeToLive mocks base method.

type MockLeaseMockRecorder

type MockLeaseMockRecorder struct {
	// contains filtered or unexported fields
}

MockLeaseMockRecorder is the mock recorder for MockLease.

func (*MockLeaseMockRecorder) Close

func (mr *MockLeaseMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockLeaseMockRecorder) Grant

func (mr *MockLeaseMockRecorder) Grant(ctx, ttl any) *gomock.Call

Grant indicates an expected call of Grant.

func (*MockLeaseMockRecorder) KeepAlive

func (mr *MockLeaseMockRecorder) KeepAlive(ctx, id any) *gomock.Call

KeepAlive indicates an expected call of KeepAlive.

func (*MockLeaseMockRecorder) KeepAliveOnce

func (mr *MockLeaseMockRecorder) KeepAliveOnce(ctx, id any) *gomock.Call

KeepAliveOnce indicates an expected call of KeepAliveOnce.

func (*MockLeaseMockRecorder) Leases

func (mr *MockLeaseMockRecorder) Leases(ctx any) *gomock.Call

Leases indicates an expected call of Leases.

func (*MockLeaseMockRecorder) Revoke

func (mr *MockLeaseMockRecorder) Revoke(ctx, id any) *gomock.Call

Revoke indicates an expected call of Revoke.

func (*MockLeaseMockRecorder) TimeToLive

func (mr *MockLeaseMockRecorder) TimeToLive(ctx, id any, opts ...any) *gomock.Call

TimeToLive indicates an expected call of TimeToLive.

type MockWatcher

type MockWatcher struct {
	// contains filtered or unexported fields
}

MockWatcher is a mock of Watcher interface.

func NewMockWatcher

func NewMockWatcher(ctrl *gomock.Controller) *MockWatcher

NewMockWatcher creates a new mock instance.

func (*MockWatcher) Close

func (m *MockWatcher) Close() error

Close mocks base method.

func (*MockWatcher) EXPECT

func (m *MockWatcher) EXPECT() *MockWatcherMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockWatcher) RequestProgress

func (m *MockWatcher) RequestProgress(ctx context.Context) error

RequestProgress mocks base method.

func (*MockWatcher) Watch

func (m *MockWatcher) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan

Watch mocks base method.

type MockWatcherMockRecorder

type MockWatcherMockRecorder struct {
	// contains filtered or unexported fields
}

MockWatcherMockRecorder is the mock recorder for MockWatcher.

func (*MockWatcherMockRecorder) Close

func (mr *MockWatcherMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*MockWatcherMockRecorder) RequestProgress

func (mr *MockWatcherMockRecorder) RequestProgress(ctx any) *gomock.Call

RequestProgress indicates an expected call of RequestProgress.

func (*MockWatcherMockRecorder) Watch

func (mr *MockWatcherMockRecorder) Watch(ctx, key any, opts ...any) *gomock.Call

Watch indicates an expected call of Watch.

type Registry

type Registry struct {
	gone.Flag
	// contains filtered or unexported fields
}

func (*Registry) Deregister

func (r *Registry) Deregister(instance g.Service) error

func (*Registry) GetInstances

func (r *Registry) GetInstances(serviceName string) ([]g.Service, error)

func (*Registry) Register

func (r *Registry) Register(instance g.Service) error

func (*Registry) Watch

func (r *Registry) Watch(serviceName string) (<-chan []g.Service, func() error, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL