discover

package module
v1.7.4 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 19 Imported by: 7

README

a simple discover based redis

configuration
type Config struct {
    Registry       string            // a redis url, default is redis://:@127.0.0.1:6379/15
    App            string            // register to a app service
    Calls          map[string]string // defines which apps will call
}
configure App will be registered for request
configure Calls will subscribe app nodes for call

Calls value will detection value type to config

1 or 2 for http version

s for https

number + time unit (for example: 10s、300ms) for timeout

other for Access-Token header

type callInfoType struct {
	Timeout     time.Duration
	HttpVersion int
	Token       string
	SSL         bool
}
for _, v := range u.SplitTrim(callConf, ":") {
    if v == "1" || v == "2" {
        callInfo.HttpVersion = u.Int(v)
    } else if v == "s" {
        callInfo.SSL = true
    } else if numberMatcher.MatchString(v) {
        callInfo.Timeout = u.Duration(v)
    } else {
        callInfo.Token = v
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = struct {
	Registry       string            // redis://:@127.0.0.1:6379/15
	App            string            // register to a app service
	Weight         int               // 100
	Calls          map[string]string // defines which apps will call
	CallRetryTimes int               // 10
	IpPrefix       string            // 指定使用的IP网段,默认排除 172.17
}{}

Functions

func AddExternalApp

func AddExternalApp(app string, callConf string) bool

func EasyStart added in v0.1.17

func EasyStart() (string, int)

外部框架使用discover

func Init added in v0.1.0

func Init()

func IsClient

func IsClient() bool

func IsServer

func IsServer() bool

func Restart

func Restart() bool

func SetLoadBalancer

func SetLoadBalancer(lb LoadBalancer)

设置一个负载均衡算法

func SetLogger added in v0.6.7

func SetLogger(logger *log.Logger)

func SetRoute added in v0.1.17

func SetRoute(route func(appClient *AppClient, request *http.Request))

func Start

func Start(addr string) bool

func Stop

func Stop()

func Wait

func Wait()

Types

type AppClient

type AppClient struct {
	Logger  *log.Logger
	App     string
	Method  string
	Path    string
	Data    *map[string]interface{}
	Headers *map[string]string
	// contains filtered or unexported fields
}

func (*AppClient) CheckApp added in v0.5.42

func (appClient *AppClient) CheckApp(app string) bool

func (*AppClient) Next

func (appClient *AppClient) Next(app string, request *http.Request) *NodeInfo

func (*AppClient) NextWithNode

func (appClient *AppClient) NextWithNode(app, withNode string, request *http.Request) *NodeInfo

type Caller

type Caller struct {
	Request *http.Request
	NoBody  bool
	// contains filtered or unexported fields
}

func NewCaller added in v0.0.7

func NewCaller(request *http.Request, logger *log.Logger) *Caller

func (*Caller) Delete

func (caller *Caller) Delete(app, path string, data interface{}, headers ...string) *httpclient.Result

func (*Caller) Do

func (caller *Caller) Do(method, app, path string, data interface{}, headers ...string) *httpclient.Result

func (*Caller) DoWithNode

func (caller *Caller) DoWithNode(method, app, withNode, path string, data interface{}, headers ...string) (*httpclient.Result, string)

func (*Caller) Get

func (caller *Caller) Get(app, path string, headers ...string) *httpclient.Result

func (*Caller) Head

func (caller *Caller) Head(app, path string, headers ...string) *httpclient.Result

func (*Caller) Post

func (caller *Caller) Post(app, path string, data interface{}, headers ...string) *httpclient.Result

func (*Caller) Put

func (caller *Caller) Put(app, path string, data interface{}, headers ...string) *httpclient.Result

type DefaultLoadBalancer

type DefaultLoadBalancer struct{}

func (*DefaultLoadBalancer) Next

func (lba *DefaultLoadBalancer) Next(appClient *AppClient, nodes []*NodeInfo, request *http.Request) *NodeInfo

func (*DefaultLoadBalancer) Response

func (lba *DefaultLoadBalancer) Response(appClient *AppClient, node *NodeInfo, err error, response *http.Response, responseTimeing int64)

type LoadBalancer

type LoadBalancer interface {

	// 每个请求完成后提供信息
	Response(appClient *AppClient, node *NodeInfo, err error, response *http.Response, responseTimeing int64)

	// 请求时根据节点的得分取最小值发起请求
	Next(appClient *AppClient, nodes []*NodeInfo, request *http.Request) *NodeInfo
}

type NodeInfo

type NodeInfo struct {
	Addr        string
	Weight      int
	UsedTimes   uint64
	FailedTimes int
	Data        sync.Map
}

Jump to

Keyboard shortcuts

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