consul

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 17 Imported by: 0

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

This section is empty.

Variables

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

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 NewConsulApp

func NewConsulApp(consulUrl 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 NewConsulAppWithCfg

func NewConsulAppWithCfg(cfg interface{}, 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 (*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"`

	ServicePort int    `json:"service_port" yaml:"service_port"`
	ServiceIP   string `json:"service_ip" yaml:"service_ip"`
	ServiceName string `json:"service_name" yaml:"service_name"`

	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 {
	*ConsulAppInfo
	// contains filtered or unexported fields
}

func NewConsulOp

func NewConsulOp(consulUrl string) (*ConsulOperator, error)

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