Documentation
¶
Index ¶
- Constants
- Variables
- func RegisterBalancer(policy string, factory BalancerFactory)
- func WithGRPCDialOptions(options ...grpc.DialOption) func(*Registry)
- func WithKeyPrefix(prefix string) func(*Registry)
- func WithLeaseTTL(seconds int) func(*Registry)
- type Balancer
- type BalancerFactory
- type GRPCEntry
- type GRPCServiceDesc
- type NodeEntry
- type NodeState
- type Registry
- func (r *Registry) AllocGRPCNode(serviceCode int32, sess Session) (nodeID ulid.ULID, err error)
- func (r *Registry) Close()
- func (r *Registry) DumpGRPCResolver() map[string]any
- func (r *Registry) ForeachNodes(f func(NodeEntry) bool)
- func (r *Registry) GetGRPCConn(nodeID ulid.ULID) (conn *grpc.ClientConn, err error)
- func (r *Registry) GetGRPCDesc(serviceCode int32) (GRPCServiceDesc, bool)
- func (r *Registry) GetGatewayClient(nodeID ulid.ULID) (nh.GatewayClient, error)
- func (r *Registry) GetNodeClient(nodeID ulid.ULID) (nh.NodeClient, error)
- func (r *Registry) PickGRPCNode(serviceCode int32) (nodeID ulid.ULID, err error)
- func (r *Registry) Put(entry NodeEntry) error
- func (r *Registry) SubscribeDelete(ctx context.Context, handler func(entry NodeEntry))
- func (r *Registry) SubscribeUpdate(ctx context.Context, handler func(entry NodeEntry))
- type Session
Constants ¶
View Source
const ( // BalancerRandom 随机 BalancerRandom = "random" // BalancerRoundRobin 加权轮询 BalancerRoundRobin = "roundRobin" // BalancerIPHash 根据客户端IP地址哈希 BalancerIPHash = "ipHash" // BalancerIDHash 根据客户端ID哈希 BalancerIDHash = "idHash" // BalancerOldest 使用最早启动的那个节点 // // 可用于单点服务的备用节点高可用切换 // // 当最初使用的主节点下线后,会按照启动顺序,依次启用后续的备用节点 BalancerOldest = "oldest" // BalancerNewest 使用最新启动的那个节点 BalancerNewest = "newest" )
View Source
const ( // NodeOK 正常 NodeOK NodeState = "ok" // NodeLazy 不接受新的请求 NodeLazy NodeState = "lazy" // NodeDown 下线,不接受任何请求 NodeDown NodeState = "down" // AutoAllocate 自动分配,第一次请求时,如果还没有分配,会根据负载均衡策略自动选择一个可用节点 AutoAllocate = "auto" // ServerAllocate 服务端分配,只有服务器端分配好节点之后,客户端才能够访问 ServerAllocate = "server" // ClientAllocate 客户端分配,客户端请求时附带的nodeID会被记录下来,后续即使不指定nodeID,也会被分配到同一个节点 ClientAllocate = "client" )
Variables ¶
View Source
var ( // ErrNodeNotFoundOrDown 没有可用节点或节点已下线 ErrNodeNotFoundOrDown = errors.New("node not found or down") // ErrNoNodeAvailable 没有可用节点 ErrNoNodeAvailable = errors.New("no node available") )
Functions ¶
func RegisterBalancer ¶
func RegisterBalancer(policy string, factory BalancerFactory)
RegisterBalancer 注册负载均衡器
func WithGRPCDialOptions ¶
func WithGRPCDialOptions(options ...grpc.DialOption) func(*Registry)
WithGRPCDialOptions 设置grpc.DialOption
Types ¶
type BalancerFactory ¶
BalancerFactory 负载均衡器工厂
type GRPCEntry ¶
type GRPCEntry struct {
// 监听地址 host:port
Endpoint string `json:"endpoint"`
// grpc服务列表
Services []GRPCServiceDesc `json:"services"`
}
GRPCEntry gRPC服务发现条目
type GRPCServiceDesc ¶
type GRPCServiceDesc struct {
// 服务名称
//
// example: helloworld.Greeter
Name string `json:"name"`
// 服务代码枚举值,每个服务的代码值必须唯一
//
// 网关会根据客户端请求消息内的service_code字段,将请求转发到对应的服务
Code int32 `json:"code"`
// 服务路径,网关在构造grpc请求时,用于方法地址构造
//
// example: /helloworld.Greeter
Path string `json:"path"`
// 是否允许客户端访问
Public bool `json:"public,omitempty"`
// 负载均衡策略
Balancer string `json:"balancer"`
// Weight 节点权重,用于负载均衡
Weight int `json:"weight,omitempty"`
// Stateful 是否有状态服务
Stateful bool `json:"stateful,omitempty"`
// Allocation 有状态节点分配方式
Allocation string `json:"allocation,omitempty"`
}
GRPCServiceDesc gRPC服务
type NodeEntry ¶
type NodeEntry struct {
// 节点ID,集群内唯一
ID ulid.ULID `json:"id"`
// 节点名称,仅用于显示
Name string `json:"name"`
// 节点状态
State NodeState `json:"state"`
// 网关入口URL
//
// Example:
// - tcp://0.0.0.0:8222
// - ws://0.0.0.0:8222/grpc
Entrance string `json:"entrance,omitempty"`
// prometheus监控指标URL
//
// Example: http://127.0.0.1:12345/metrics
Metrics string `json:"metrics,omitempty"`
// grpc服务信息
GRPC GRPCEntry `json:"grpc"`
// git版本
GitVersion string `json:"git_version,omitempty"`
}
NodeEntry 节点服务发现条目
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry 服务注册表
func NewRegistry ¶
NewRegistry 创建服务注册表
func (*Registry) AllocGRPCNode ¶
AllocGRPCNode 根据负载均衡策略给客户端会话分配可用节点
func (*Registry) DumpGRPCResolver ¶
DumpGRPCResolver 导出grpc服务解析器数据
func (*Registry) GetGRPCConn ¶
GetGRPCConn 获取指定节点的grpc连接
func (*Registry) GetGRPCDesc ¶
func (r *Registry) GetGRPCDesc(serviceCode int32) (GRPCServiceDesc, bool)
GetGRPCDesc 获取grpc服务描述
func (*Registry) GetGatewayClient ¶
GetGatewayClient 获取网关grpc服务客户端
func (*Registry) GetNodeClient ¶
GetNodeClient 获取节点grpc服务客户端
func (*Registry) PickGRPCNode ¶
PickGRPCNode 随机选择一个可用节点
func (*Registry) SubscribeDelete ¶
SubscribeDelete 订阅节点删除
Click to show internal directories.
Click to hide internal directories.