client

package
v2.4.4+incompatible Latest Latest
Warning

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

Go to latest
Published: May 11, 2018 License: MIT Imports: 12 Imported by: 10

README

数据类型

type Response struct {
	Id       int64
	Event    string
	Data     []byte
}

type Client struct {
}

func New(addr string) *Client   // addr为lainlet地址, 如"192.168.77.21:9001"

func (c *Client) Get(uri string, timeout time.Duration) ([]byte, error)  // get请求

func (c *Client) Watch(uri string, ctx context.Context) (<-chan *Response, error) // watch请求

func (c *Client) Do(uri string, timeout time.Duration, watch bool) (io.ReadCloser, error) // rawrequest

Demo

package main

import (
	"fmt"
	"golang.org/x/net/context"
	api "github.com/laincloud/lainlet/api/v2"
	"github.com/laincloud/lainlet/client"
	"log"
	"os"
	"time"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("lainlet addr required")
		return
	}
	info := new(api.GeneralCoreInfo)

	// get request
	c := client.New(os.Args[1])
	data, err := c.Get("/coreinfowatcher/?appname=registry", 0)
	if err != nil {
		panic(err)
	}
	info.Decode(data)
	fmt.Println(info.Data)

	// watch request
	ctx, _ := context.WithTimeout(context.Background(), time.Second*30) // 30 seconds timeout
	ch, err := c.Watch("/coreinfowatcher/?appname=registry&heartbeat=5", ctx)
	if err != nil {
		panic(err)
	}
	for event := range ch {
		fmt.Println("Get a event:")
		fmt.Println("    ", event.Id)
		fmt.Println("    ", event.Event)
		if event.Id != 0 { // id == 0 means error-event or heartbeat
			if err := info.Decode(event.Data); err != nil {
				log.Println(err.Error())
			} else {
				fmt.Println("    ", info.Data)
			}
		}
	}
}

Documentation

Index

Constants

View Source
const (
	ERROR     string = "error"
	UPDATE           = "update"
	DELETE           = "delete"
	INIT             = "init"
	HEARTBEAT        = "heartbeat"
)

all the kinds of event

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func New

func New(addr string) *Client

create a new client, addr is lainlet address such as "127.0.0.1:9001"

func (*Client) Do

func (c *Client) Do(uri string, timeout time.Duration, watch bool) (io.ReadCloser, error)

send a http request

func (*Client) Get

func (c *Client) Get(uri string, timeout time.Duration) ([]byte, error)

get data from the given uri, return the []byte read from http response body. It return error if fail to send http request to lainlet

func (*Client) Watch

func (c *Client) Watch(uri string, ctx context.Context) (<-chan *Response, error)

watch request to the given uri. The return channel will be closed when the context was canceled or the http connection was closed. it return error when fail to send request to lainlet.

type Config

type Config map[string]string

The Data type return by /v2/configwatcher

type ContainerInfo

type ContainerInfo container.Info

type Containers

type Containers map[string]ContainerInfo

The Data type return by /v2/containers

type CoreInfo

type CoreInfo v2.CoreInfo

type CoreInfos

type CoreInfos map[string]CoreInfo

The Data type return by /v2/coreinfowatcher

type Depends

type Depends map[string]map[string]map[string]DependsItem

The Data type return by /v2/depends

type DependsItem

type DependsItem v2.DependsItem

type JSONVirtualIpPortConfig

type JSONVirtualIpPortConfig struct {
	App      string `json:"app"`
	Proc     string `json:"proc"`
	Port     string `json:"port"`
	Proto    string `json:"proto"`
	ProcType string `json:"proctype"`
}

The Data type return by /v2/configwatcher?target=vips

type LocalSpecs

type LocalSpecs []string

The Data type return by /v2/localspecquery

type NodeInfo

type NodeInfo nodes.NodeInfo

type NodesInfo

type NodesInfo map[string]NodeInfo

The Data type return by /v2/nodes

type PodGroup

type PodGroup v2.PodGroup

type PodGroups

type PodGroups []PodGroup

The Data type return by /v2/podgroupwatcher

type ProcInfo

type ProcInfo v2.ProcInfo

type Proxy

type Proxy map[string]ProcInfo

The Data type return by /v2/proxywatcher

type Response

type Response struct {
	Id    int64  // The event Id, start from 1, and incresed by 1 when event is UPDATE or DELETE, it's always 0 when event is ERROR or HEARTBEAT
	Event string // the event name

	// the returned data return by watch request, the Data is a json-format.
	// you can import `github.com/laincloud/api/v2`, and using the coresponding type to Decode.
	Data []byte
	// contains filtered or unexported fields
}

the response returned by watch action

Jump to

Keyboard shortcuts

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