Documentation
¶
Index ¶
Examples ¶
Constants ¶
const ( MetaWeight = "weight" MetaCluster = "cluster" MetaZone = "zone" MetaColor = "color" )
metadata common key
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Builder ¶
Builder resolver builder.
type Config ¶
Config discovery configures.
type Discovery ¶
type Discovery struct {
// contains filtered or unexported fields
}
Discovery is discovery client.
func (*Discovery) Build ¶
Build disovery resovler builder.
func (*Discovery) Close ¶
Close stop all running process including discovery and register
func (*Discovery) Register ¶
func (d *Discovery) Register(ins *Instance) (cancelFunc context.CancelFunc, err error)
Register Register an instance with discovery and renew automatically
Example ¶
This Example register a server provider into discovery.
package main
import (
"fmt"
"time"
"e.coding.net/rtZero/smartgo/discovery/naming"
)
func main() {
conf := &naming.Config{
Nodes: []string{"127.0.0.1:7171"}, // NOTE: 配置种子节点(1个或多个),client内部可根据/discovery/nodes节点获取全部node(方便后面增减节点)
Zone: "sh1",
Env: "test",
}
dis := naming.New(conf)
ins := &naming.Instance{
Zone: "sh1",
Env: "test",
AppID: "provider",
// Hostname:"", // NOTE: hostname 不需要,会优先使用discovery new时Config配置的值,如没有则从os.Hostname方法获取!!!
Addrs: []string{"http://172.0.0.1:8888", "grpc://172.0.0.1:9999"},
LastTs: time.Now().Unix(),
Metadata: map[string]string{"weight": "10"},
}
cancel, _ := dis.Register(ins)
defer cancel() // NOTE: 注意一般在进程退出的时候执行,会调用discovery的cancel接口,使实例从discovery移除
fmt.Println("register")
// Unordered output4
}
type Instance ¶
type Instance struct {
// Region is region.
Region string `json:"region"`
// Zone is IDC.
Zone string `json:"zone"`
// Env prod/pre、uat/fat1
Env string `json:"env"`
// AppID is mapping servicetree appid.
AppID string `json:"appid"`
// Hostname is hostname from docker.
Hostname string `json:"hostname"`
// Addrs is the address of app instance
// format: scheme://host
Addrs []string `json:"addrs"`
// Version is publishing version.
Version string `json:"version"`
// LastTs is instance latest updated timestamp
LastTs int64 `json:"latest_timestamp"`
// Metadata is the information associated with Addr, which may be used
// to make load balancing decision.
Metadata map[string]string `json:"metadata"`
}
Instance represents a server the client connects to.
type InstancesInfo ¶
type InstancesInfo struct {
Instances map[string][]*Instance `json:"instances"`
LastTs int64 `json:"latest_timestamp"`
Scheduler []Zone `json:"scheduler"`
}
InstancesInfo instance info.
func (*InstancesInfo) UseScheduler ¶
func (insInf *InstancesInfo) UseScheduler(zone string) (inss []*Instance)
UseScheduler use scheduler info on instances. if instancesInfo contains scheduler info about zone, return releated zone's instances weighted by scheduler. if not,only zone instances be returned.
type Registry ¶
type Registry interface {
Register(ins *Instance) (cancel context.CancelFunc, err error)
Close() error
}
Registry Register an instance and renew automatically.
type Resolve ¶
type Resolve struct {
// contains filtered or unexported fields
}
Resolve discveory resolver.
func (*Resolve) Fetch ¶
func (r *Resolve) Fetch() (ins *InstancesInfo, ok bool)
Fetch fetch resolver instance.
type Resolver ¶
type Resolver interface {
Fetch() (*InstancesInfo, bool)
Watch() <-chan struct{}
Close() error
}
Resolver resolve naming service
Source Files
¶
- client.go
- naming.go