etcd

package module
v0.0.0-...-dba42a8 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

due-registry-etcd

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

1.安装

go get github.com/jyjxyz/gserver/registry/etcd@latest

2.开始使用

package main

import (
	"context"
	"time"
	
	"github.com/jyjxyz/gserver/cluster"
	"github.com/jyjxyz/gserver/log"
	"github.com/jyjxyz/gserver/registry"
	"github.com/jyjxyz/gserver/registry/etcd"
)

func main() {
	reg := etcd.NewRegistry()
	
	watch(reg, string(cluster.Node), 1)
	watch(reg, string(cluster.Node), 2)
	
	ins := &registry.ServiceInstance{
		ID:       "test-1",
		Name:     "login-server",
		Kind:     cluster.Node,
		Alias:    "mahjong",
		State:    cluster.Work,
		Endpoint: "grpc://127.0.0.1:6339",
	}
	
	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(20 * 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(40 * 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) 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) Stop

func (r *Registry) Stop() error

Stop 停止注册服务

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