etcd

package module
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 12 Imported by: 15

README

GoFrame Etcd Registry

Use etcd as service registration and discovery management.

Installation

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

suggested using go.mod:

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

Example

Reference example

server

package main

import (
	"github.com/gogf/gf/contrib/registry/etcd/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/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/gogf/gf/contrib/registry/etcd/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/gsel"
	"github.com/gogf/gf/v2/net/gsvc"
	"github.com/gogf/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

func New

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

New creates and returns a new etcd registry. Support Etcd Address format: ip:port,ip:port...,ip:port@username:password

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 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 off-lines and removes `service` from the Registry.

func (*Registry) Register

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

Register registers `service` to Registry. Note that it returns a new Service if it changes the input Service with custom one.

func (*Registry) Search

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

Search searches and returns services with specified condition.

func (*Registry) Watch

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

Watch watches specified condition changes. The `key` is the prefix of service key.

type Service added in v2.3.3

type Service struct {
	gsvc.Service
	Endpoints gsvc.Endpoints
	Metadata  gsvc.Metadata
}

Service wrapper.

func NewService added in v2.3.3

func NewService(service gsvc.Service) *Service

NewService creates and returns local Service from gsvc.Service interface object.

func (*Service) GetEndpoints added in v2.3.3

func (s *Service) GetEndpoints() gsvc.Endpoints

GetEndpoints returns the Endpoints of service. The Endpoints contain multiple host/port information of service.

func (*Service) GetMetadata added in v2.3.3

func (s *Service) GetMetadata() gsvc.Metadata

GetMetadata returns the Metadata map of service. The Metadata is key-value pair map specifying extra attributes of a service.

func (*Service) GetValue added in v2.3.3

func (s *Service) GetValue() string

GetValue formats and returns the value of the service. The result value is commonly used for key-value registrar server.

Jump to

Keyboard shortcuts

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