etcd

package module
v2.0.0-...-5372cc7 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

注册中心-etcd

1.功能
  • 支持完整的服务注册发现接口
  • 支持多线程模式下服务实例的注册与监听
  • 支持服务实例的更新操作
  • 支持内建客户端和注入外部客户端两种连接方式
  • 支持心跳重试机制
2.快速开始

1.安装

go get github.com/cnsync/simple-game/registry/etcd/v2@latest

2.etc配置项

# 注册中心
[registry]
    # etcd注册中心
    [registry.etcd]
        # 客户端连接地址
        addrs = ["127.0.0.1:2379"]
        # 客户端拨号超时时间(秒)
        dialTimeout = 5
        # 命名空间
        namespace = "services"
        # 超时时间(秒)
        timeout = 3
        # 心跳重试次数
        retryTimes = 3
        # 心跳重试间隔(秒)
        retryInterval = 10

3.开始使用

package main

import (
	"context"
	"github.com/cnsync/simple-game/registry/etcd/v2"
	"github.com/cnsync/simple-game/v2/cluster"
	"github.com/cnsync/simple-game/v2/log"
	"github.com/cnsync/simple-game/v2/registry"
	"github.com/cnsync/simple-game/v2/utils/xuuid"
	"time"
)

func main() {
	var (
		reg   = etcd.NewRegistry()
		id, _ = xuuid.UUID()
		name  = "game-server"
		alias = "mahjong"
		ins   = &registry.ServiceInstance{
			ID:       id,
			Name:     name,
			Kind:     cluster.Node,
			Alias:    alias,
			State:    cluster.Work,
			Endpoint: "grpc://127.0.0.1:6339",
		}
	)

	// 监听
	watch(reg, name, 1)
	watch(reg, name, 2)

	// 注册服务
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	err := reg.Register(ctx, ins)
	cancel()
	if err != nil {
		log.Fatal(err)
	}

	time.Sleep(2 * time.Second)

	// 更新服务
	ins.State = cluster.Busy
	ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
	err = reg.Register(ctx, ins)
	cancel()
	if err != nil {
		log.Fatal(err)
	}

	time.Sleep(5 * time.Second)

	// 解注册服务
	ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
	err = reg.Deregister(ctx, ins)
	cancel()
	if err != nil {
		log.Fatal(err)
	}

	time.Sleep(10 * time.Second)
}

func watch(reg *etcd.Registry, serviceName string, goroutineID int) {
	ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
	watcher, err := reg.Watch(ctx, serviceName)
	cancel()
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		for {
			services, err := watcher.Next()
			if err != nil {
				log.Fatalf("goroutine %d: %v", goroutineID, err)
				return
			}

			log.Infof("goroutine %d: new event entity", goroutineID)

			for _, service := range services {
				log.Infof("goroutine %d: %+v", goroutineID, service)
			}
		}
	}()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(o *options)

func WithAddrs

func WithAddrs(addrs ...string) Option

WithAddrs 设置客户端连接地址

func WithClient

func WithClient(client *clientv3.Client) Option

WithClient 设置外部客户端

func WithContext

func WithContext(ctx context.Context) Option

WithContext 设置上下文

func WithDialTimeout

func WithDialTimeout(dialTimeout time.Duration) Option

WithDialTimeout 设置客户端拨号超时时间

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace 设置命名空间

func WithRetryInterval

func WithRetryInterval(retryInterval time.Duration) Option

WithRetryInterval 设置心跳重试间隔时间

func WithRetryTimes

func WithRetryTimes(retryTimes int) Option

WithRetryTimes 设置心跳重试次数

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout 设置上下文超时时间

type Registry

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

func NewRegistry

func NewRegistry(opts ...Option) *Registry

func (*Registry) Close

func (r *Registry) Close() error

Close 关闭服务注册发现

func (*Registry) Deregister

func (r *Registry) Deregister(ctx context.Context, ins *registry.ServiceInstance) error

Deregister 解注册服务实例

func (*Registry) Register

func (r *Registry) Register(ctx context.Context, ins *registry.ServiceInstance) error

Register 注册服务实例

func (*Registry) Services

func (r *Registry) Services(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error)

Services 获取服务实例列表

func (*Registry) Watch

func (r *Registry) Watch(ctx context.Context, serviceName string) (registry.Watcher, error)

Watch 监听相同服务名的服务实例变化

Jump to

Keyboard shortcuts

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