etcd

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

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

Go to latest
Published: Aug 27, 2019 License: BSD-2-Clause Imports: 7 Imported by: 0

README

Introduction

Build Status

Implement service discovery based on etcd By Go

Quick Start Etcd

$ wget https://github.com/coreos/etcd/releases/download/v3.1.5/etcd-v3.1.5-linux-amd64.tar.gz
$ tar xzvf etcd-v3.1.5-linux-amd64.tar.gz
$ mv etcd-v3.1.5-linux-amd64 /opt/etcd

$ go get github.com/mattn/goreman
$ goreman -f ProcFile (本仓库提供) start

Quick-start

go get github.com/zheng-ji/discover-etcd

Example

# 服务发现
package main

import (
	"fmt"
	"github.com/zheng-ji/discover-etcd"
	"log"
	"time"
)

func main() {
	m, err := etcd.OnWatch([]string{
		"http://127.0.0.1:12379",
		"http://127.0.0.1:22379",
		"http://127.0.0.1:32379"}, "services/")
	if err != nil {
		log.Fatal(err)
	}

	for {
		m.Nodes.Range(func(k, v interface{}) bool {
			fmt.Printf("node:%s, ip=%s ext=%s\n",
                k, v.(*etcd.Node).Meta.IP, v.(*etcd.Node).Meta.Ext)
			return true
		})
		time.Sleep(time.Second * 5)
	}
}
# 服务注册
package main

import (
	"fmt"
	"github.com/zheng-ji/discover-etcd"
	"log"
	"time"
)

func main() {

	serviceName := "s-test"
	serviceInfo := etcd.ServiceMeta{IP: "127.0.0.1", Ext: "Your Custome Data"} 
	s, err := etcd.Register(serviceName, serviceInfo, []string{
		"http://127.0.0.1:12379",
		"http://127.0.0.1:22379",
		"http://127.0.0.1:32379",
	})

	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("name:%s, ip:%s\n", s.Name, s.Meta.IP)

	go func() {
		time.Sleep(time.Second * 20)
		s.Stop()
	}()

	s.Start()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Master

type Master struct {
	Path   string
	Nodes  *sync.Map
	Client *clientv3.Client
}

func OnWatch

func OnWatch(endpoints []string, watchPath string) (*Master, error)

func NewMaster(endpoints []string, watchPath string) (*Master, error) { Monitor Nodes

func (*Master) AddNode

func (m *Master) AddNode(key string, info *ServiceMeta)

func (*Master) WatchNodes

func (m *Master) WatchNodes()

type Node

type Node struct {
	Key  string
	Meta ServiceMeta
}

node is a client

type Service

type Service struct {
	Name string
	Meta ServiceMeta
	// contains filtered or unexported fields
}

func Register

func Register(name string, info ServiceMeta, endpoints []string) (*Service, error)

Register Service

func (*Service) Start

func (s *Service) Start() error

func (*Service) Stop

func (s *Service) Stop()

type ServiceMeta

type ServiceMeta struct {
	IP  string
	Ext interface{}
}

the detail of service

func GetServiceMeta

func GetServiceMeta(ev *clientv3.Event) *ServiceMeta

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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