balance.go 5.0 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11
package entity

import (
	"bytes"
	"strconv"
	"strings"
	"unicode"

	jsoniter "github.com/json-iterator/go"
)

Y
Your Name 已提交
12
//BalanceInfoEntity 负载信息实体
E
eoLinker API Management 已提交
13 14 15 16 17 18 19 20 21 22 23
type BalanceInfoEntity struct {
	Name string
	Desc string

	DefaultConfig    string
	ClusterConfig    string
	OldVersionConfig string

	CreateTime string
	UpdateTime string
}
Y
Your Name 已提交
24 25

//BalanceInfo 负载信息
E
eoLinker API Management 已提交
26 27 28 29 30 31 32 33 34
type BalanceInfo struct {
	Name string `json:"balanceName"`
	Desc string `json:"balanceDesc"`

	Default    *BalanceConfig            `json:"defaultConfig"`
	Cluster    map[string]*BalanceConfig `json:"clusterConfig"`
	CreateTime string                    `json:"createTime"`
	UpdateTime string                    `json:"updateTime"`
}
Y
Your Name 已提交
35 36

//BalanceConfig 负载配置
E
eoLinker API Management 已提交
37
type BalanceConfig struct {
Y
Your Name 已提交
38
	DiscoveryID      int64                  `json:"serviceDiscoveryID"`
E
eoLinker API Management 已提交
39 40 41 42 43 44
	ServiceName      string                 `json:"serviceName"`
	Servers          []*BalanceServerConfig `json:"-"`
	ServersConfig    []string               `json:"static"`
	ServersConfigOrg string                 `json:"staticOrg"`
}

Y
Your Name 已提交
45
//BalanceServerConfig 负载服务配置
E
eoLinker API Management 已提交
46 47 48 49 50 51 52 53
type BalanceServerConfig struct {
	Server string `json:"server"`
	Weight int    `json:"weight"`
	Status string `json:"status"`
}
type _OldVersionBalanceInfo struct {
	BalanceConfig []*BalanceServerConfig `json:"loadBalancingServer"`
}
Y
Your Name 已提交
54

E
eoLinker API Management 已提交
55
//
Y
Your Name 已提交
56
//func (info *BalanceInfo) Write() *BalanceInfoEntity {
E
eoLinker API Management 已提交
57 58 59 60 61 62 63 64 65 66 67 68
//
//	ent := new(BalanceInfoEntity)
//	ent.Name = info.Name
//	ent.Desc = info.Name
//	ent.UpdateTime = info.UpdateTime
//	ent.CreateTime = info.CreateTime
//	ent.DefaultConfig, _ = jsoniter.MarshalToString(info.Default)
//	ent.ClusterConfig, _ = jsoniter.MarshalToString(info.Cluster)
//	return ent
//
//}

Y
Your Name 已提交
69
//Decode decode
E
eoLinker API Management 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
func (ent *BalanceInfoEntity) Decode() (*BalanceInfo, error) {
	info := new(BalanceInfo)
	info.Name = ent.Name
	info.Desc = ent.Desc
	info.CreateTime = ent.CreateTime
	info.UpdateTime = ent.UpdateTime

	if ent.DefaultConfig != "" {
		info.Default = new(BalanceConfig)
		if err := jsoniter.UnmarshalFromString(ent.DefaultConfig, info.Default); err != nil {
			return nil, err
		}
		info.Default.Decode()

	} else {
		config, err := TryOld(ent.OldVersionConfig)
		if err != nil {
			return nil, err
		}

		info.Default = &BalanceConfig{
Y
Your Name 已提交
91
			DiscoveryID:   0,
E
eoLinker API Management 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
			ServiceName:   "",
			Servers:       config,
			ServersConfig: FormatServers(config),
		}
		info.Default.ServersConfigOrg = strings.Join(info.Default.ServersConfig, ";")
	}

	if ent.ClusterConfig != "" {
		info.Cluster = make(map[string]*BalanceConfig)
		if err := jsoniter.UnmarshalFromString(ent.ClusterConfig, &info.Cluster); err != nil {
			return nil, err
		}
		for key := range info.Cluster {
			info.Cluster[key].Decode()
		}
	}
	return info, nil

}

Y
Your Name 已提交
112
//TryOld 解析旧配置
E
eoLinker API Management 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
func TryOld(oldversionConfig string) ([]*BalanceServerConfig, error) {
	if oldversionConfig == "" {
		return nil, nil
	}

	old := new(_OldVersionBalanceInfo)
	err := jsoniter.UnmarshalFromString(oldversionConfig, old)

	if err != nil {
		return nil, err
	}
	return old.BalanceConfig, nil
	////Default := new(BalanceConfig)
	//Default.Servers = old.BalanceConfig
	//Default.Format()
	//Default.ServersConfigOrg = strings.Join(Default.ServersConfig,";")
	//return Default, err
}

Y
Your Name 已提交
132
//GetConfig 获取配置
E
eoLinker API Management 已提交
133 134 135 136 137 138 139 140
func (info *BalanceInfo) GetConfig(clusterName string) *BalanceConfig {
	c, has := info.Cluster[clusterName]
	if !has || len(c.Servers) < 1 {
		return info.Default
	}
	// todo: 服务发现需要另外处理,暂时不接入
	return c
}
Y
Your Name 已提交
141

Y
Your Name 已提交
142
//FormatServers 格式化服务
E
eoLinker API Management 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
func FormatServers(servers []*BalanceServerConfig) []string {
	if len(servers) == 0 {
		return nil
	}

	serversConfig := make([]string, 0, len(servers))
	buf := bytes.NewBuffer(make([]byte, 0, len(servers)*32))
	for _, node := range servers {
		if strings.TrimSpace(node.Server) == "" {
			continue
		}
		buf.WriteString(node.Server)
		buf.WriteString(" ")
		buf.WriteString(strconv.Itoa(node.Weight))
		if node.Status != "" {
			buf.WriteString(" ")
			buf.WriteString(node.Status)
		}
		serversConfig = append(serversConfig, buf.String())
		buf.Reset()
	}
	return serversConfig
}
func fields(str string) []string {

	words := strings.FieldsFunc(strings.Join(strings.Split(str, ";"), " ; "), func(r rune) bool {
		if unicode.IsSpace(r) {
			return true
		}

		return false
	})
	return words
}
Y
Your Name 已提交
177

Y
Your Name 已提交
178
//Decode decode配置
E
eoLinker API Management 已提交
179 180 181 182 183 184
func (c *BalanceConfig) Decode() error {

	words := fields(c.ServersConfigOrg)

	s := make([]*BalanceServerConfig, 0, 5)

Y
Your Name 已提交
185
	var node *BalanceServerConfig
E
eoLinker API Management 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	index := 0
	for _, word := range words {
		if word == ";" {
			index = 0
			node = nil
			continue
		}
		l := len(word)
		value := word
		if word[l-1] == ';' {
			value = word[:l-1]
		}
		switch index {
		case 0:
			{
				node = new(BalanceServerConfig)
				node.Server = value
				s = append(s, node)
			}
		case 1:
			{
				weight, err := strconv.Atoi(value)
				if err != nil {
					return err
				}
				node.Weight = weight

			}
		case 2:
			{
				node.Status = value
			}
		}
		if node.Weight == 0 {
			node.Weight = 1
		}
		if word[l-1] == ';' {
			index = 0
			node = nil
		} else {
			index++
		}
	}

	if len(s) > 0 {
		c.Servers = s
	}
	return nil

}