You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.1 KiB
60 lines
1.1 KiB
3 years ago
|
package selector
|
||
3 years ago
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/registry"
|
||
|
)
|
||
|
|
||
3 years ago
|
// DefaultNode is slector node
|
||
|
type DefaultNode struct {
|
||
3 years ago
|
addr string
|
||
|
weight *int64
|
||
|
version string
|
||
|
name string
|
||
|
metadata map[string]string
|
||
|
}
|
||
|
|
||
|
// Address is node address
|
||
3 years ago
|
func (n *DefaultNode) Address() string {
|
||
3 years ago
|
return n.addr
|
||
|
}
|
||
|
|
||
|
// ServiceName is node serviceName
|
||
3 years ago
|
func (n *DefaultNode) ServiceName() string {
|
||
3 years ago
|
return n.name
|
||
|
}
|
||
|
|
||
|
// InitialWeight is node initialWeight
|
||
3 years ago
|
func (n *DefaultNode) InitialWeight() *int64 {
|
||
3 years ago
|
return n.weight
|
||
|
}
|
||
|
|
||
|
// Version is node version
|
||
3 years ago
|
func (n *DefaultNode) Version() string {
|
||
3 years ago
|
return n.version
|
||
|
}
|
||
|
|
||
|
// Metadata is node metadata
|
||
3 years ago
|
func (n *DefaultNode) Metadata() map[string]string {
|
||
3 years ago
|
return n.metadata
|
||
|
}
|
||
|
|
||
3 years ago
|
// NewNode new node
|
||
|
func NewNode(addr string, ins *registry.ServiceInstance) Node {
|
||
|
n := &DefaultNode{
|
||
3 years ago
|
addr: addr,
|
||
|
}
|
||
|
if ins != nil {
|
||
|
n.name = ins.Name
|
||
|
n.version = ins.Version
|
||
|
n.metadata = ins.Metadata
|
||
|
if str, ok := ins.Metadata["weight"]; ok {
|
||
|
if weight, err := strconv.ParseInt(str, 10, 64); err == nil {
|
||
|
n.weight = &weight
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return n
|
||
|
}
|