consul

package module
v0.0.0-...-6f27c9e Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2020 License: MIT Imports: 16 Imported by: 1

README

consul

feature

  1. register service
  2. deregister service
  3. set key/value
  4. aquire lock

samples

1.basics

	consulapi := consul.NewConsulOp()
	consulapi.Fix()
	consulapi.Ping()
	err := consulapi.Put("AAA",[]byte("1"))
	dat, err := consulapi.Get("AAA")
	err = consulapi.Delete("AAA")
	consulapi.Aquire("lock/test")
	consulapi.Release("lock/test")
	consulapi.RegisterService()
	consulapi.DeregisterService()	

2.consul app

2.1 create consul app struct pointer
2.2 create an service to consul
2.3 register service :8081 as an profile 
	capp,err := consul.NewApp(":8081")

		service := newService()
		...//you code

	capp.Wait(func (stopSig os.Signal){
		service.Stop()
	})


3.consul app with cfg

3.1 consul app get ${APPNAME}.yml from consul
3.2 if not exist or error, load ${APPNAME}.yml from local
3.3 if load ${APPNAME}.yml not exist on local, make default cfg ${APPNAME}.yml to local
3.4 create an service to consul 
consul app will auto register a service with appname

	var cfg YourAppCfg
	capp,err := consul.NewAppWithCfg(&cfg, "", ":8081")

		service := newService()
		...//you code

	capp.Wait(func (stopSig os.Signal){
		service.Stop()
	})


Documentation

Index

Constants

View Source
const (
	CONSUL_HEALTH_PATH = "health"
)

Variables

View Source
var (
	ErrNotExist = errors.New("NOT EXIST")
)

Functions

func ReadTxt

func ReadTxt(c *ConsulOperator, file string) ([]byte, error)

load an conf file text form file(FQDN) try consul first, if not exist, try local file

Types

type ConsulApp

type ConsulApp struct {
	*ConsulOperator
}

func NewApp

func NewApp(healthHost, agent string) (*ConsulApp, error)

create a consul app arg heathHost is must parameter, a health http address of app arg agent is option, if empty using default 127.0.0.1:8500

func NewAppWithCfg

func NewAppWithCfg(cfg interface{}, healthHost string) (*ConsulApp, error)

create a consul app with load cfg, using 127.0.0.1:8500 cfg is required parameter, the cfg address arg heathHost is must parameter, a health http address of app

func NewAppWithCfgEx

func NewAppWithCfgEx(cfg interface{}, healthHost, consulUrl string) (*ConsulApp, error)

create a consul app with load cfg, using 127.0.0.1:8500 cfg is required parameter, the cfg address arg heathHost is must parameter, a health http address of app arg consulUrl is an FQDN format string which contains consul host,port,configpath, if configpath is empty, ${appname}.yml, config/${appname} will be tried to load in order

func NewAppWithCustomCfg

func NewAppWithCustomCfg(cfg interface{}, confName, healthHost string) (*ConsulApp, error)

create a consul app with load cfg, using 127.0.0.1:8500 cfg is required parameter, the cfg address arg heathHost is must parameter, a health http address of app arg consulUrl is an FQDN format string which contains consul host,port,configpath

func (*ConsulApp) Wait

func (c *ConsulApp) Wait(stopcall func(os.Signal), signals ...os.Signal)

wait for main function return and register app to service to consul

type ConsulAppInfo

type ConsulAppInfo struct {
	ConsulHost string `json:"consul_host" yaml:"consul_host"`
	ConsulPort int    `json:"consul_port" yaml:"consul_port"`

	Config string     `json:"config" yaml:"config"`
	Values url.Values `json:"values" yaml:"values"`

	CheckInterval string `json:"check_interval" yaml:"check_interval"`
	CheckHTTP     string `json:"check_http" yaml:"check_http"`
	CheckTCP      string `json:"check_tcp" yaml:"check_tcp"`
}

func ParseConsulUrl

func ParseConsulUrl(consulUrl string) (*ConsulAppInfo, error)

type ConsulOperator

type ConsulOperator struct {
	Agent    string `json:"agent,omitempty" yaml:"agent,omitempty"`
	IP       string `json:"ip" yaml:"ip"`
	Port     int    `json:"port" yaml:"port"`
	Name     string `json:"Name" yaml:"Name"`
	Path     string `json:"path,omitempty" yaml:"path,omitempty"`
	Interval string `json:"interval,omitempty" yaml:"interval,omitempty"`
	// contains filtered or unexported fields
}

func NewConsulOp

func NewConsulOp(agent string) *ConsulOperator

func (*ConsulOperator) Acquire

func (c *ConsulOperator) Acquire(key string, stopChan <-chan struct{}) error

func (*ConsulOperator) Delete

func (c *ConsulOperator) Delete(name string) error

func (*ConsulOperator) DeregisterService

func (c *ConsulOperator) DeregisterService() error

func (*ConsulOperator) Fix

func (c *ConsulOperator) Fix()

func (*ConsulOperator) Get

func (c *ConsulOperator) Get(name string) ([]byte, error)

func (*ConsulOperator) GetEx

func (c *ConsulOperator) GetEx(name string) ([]byte, uint64, error)

func (*ConsulOperator) ListService

func (c *ConsulOperator) ListService(name string) ([]*consulapi.CatalogService, error)

func (*ConsulOperator) ListServices

func (c *ConsulOperator) ListServices() (map[string][]string, error)

func (*ConsulOperator) Ping

func (c *ConsulOperator) Ping() error

func (*ConsulOperator) PrintServices

func (c *ConsulOperator) PrintServices(name string) error

func (*ConsulOperator) Put

func (c *ConsulOperator) Put(name string, value []byte) error

func (*ConsulOperator) RegisterService

func (c *ConsulOperator) RegisterService() error

func (*ConsulOperator) Release

func (c *ConsulOperator) Release(key string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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