etcd

package module
v2.0.0-...-459cd1e Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 10 Imported by: 2

README

GoFrame Etcd Registry

Use etcd as service registration and discovery management.

Installation

go get -u -v github.com/wowqhb/gf/contrib/registry/etcd/v2

suggested using go.mod:

require github.com/wowqhb/gf/contrib/registry/etcd/v2 latest

Example

Reference example

server

package main

import (
	"github.com/wowqhb/gf/contrib/registry/etcd/v2"
	"github.com/wowqhb/gf/v2/frame/g"
	"github.com/wowqhb/gf/v2/net/ghttp"
	"github.com/wowqhb/gf/v2/net/gsvc"
)

func main() {
	gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))

	s := g.Server(`hello.svc`)
	s.BindHandler("/", func(r *ghttp.Request) {
		g.Log().Info(r.Context(), `request received`)
		r.Response.Write(`Hello world`)
	})
	s.Run()
}

client

package main

import (
	"fmt"
	"time"

	"github.com/wowqhb/gf/contrib/registry/etcd/v2"
	"github.com/wowqhb/gf/v2/frame/g"
	"github.com/wowqhb/gf/v2/net/gsel"
	"github.com/wowqhb/gf/v2/net/gsvc"
	"github.com/wowqhb/gf/v2/os/gctx"
)

func main() {
	gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
	gsel.SetBuilder(gsel.NewBuilderRoundRobin())

	client := g.Client()
	for i := 0; i < 100; i++ {
		res, err := client.Get(gctx.New(), `http://hello.svc/`)
		if err != nil {
			panic(err)
		}
		fmt.Println(res.ReadAllString())
		res.Close()
		time.Sleep(time.Second)
	}
}

License

GoFrame etcd is licensed under the MIT License, 100% free and open-source, forever.

Documentation

Overview

Package etcd implements service Registry and Discovery using etcd.

Index

Constants

View Source
const (
	// DefaultKeepAliveTTL is the default keepalive TTL.
	DefaultKeepAliveTTL = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	Logger       glog.ILogger
	KeepaliveTTL time.Duration
}

Option is the option for the etcd registry.

type Registry

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

Registry implements gsvc.Registry interface.

func New

func New(address string, option ...Option) *Registry

New creates and returns a new etcd registry.

func NewWithClient

func NewWithClient(client *etcd3.Client, option ...Option) *Registry

NewWithClient creates and returns a new etcd registry with the given client.

func (*Registry) Deregister

func (r *Registry) Deregister(ctx context.Context, service gsvc.Service) error

Deregister implements the gsvc.Deregister interface.

func (*Registry) Register

func (r *Registry) Register(ctx context.Context, service gsvc.Service) (gsvc.Service, error)

Register implements the gsvc.Register interface.

func (*Registry) Search

func (r *Registry) Search(ctx context.Context, in gsvc.SearchInput) ([]gsvc.Service, error)

Search is the etcd discovery search function.

func (*Registry) Watch

func (r *Registry) Watch(ctx context.Context, key string) (gsvc.Watcher, error)

Watch is the etcd discovery watch function.

Jump to

Keyboard shortcuts

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