前言
grpc默認(rèn)支持兩種負(fù)載均衡算法pick_first 和 round_robin
輪詢法round_robin不能滿足因服務(wù)器配置不同而承擔(dān)不同負(fù)載量,這篇文章將介紹如何實(shí)現(xiàn)自定義負(fù)載均衡策略--加權(quán)隨機(jī)。
加權(quán)隨機(jī)法可以根據(jù)服務(wù)器的處理能力而分配不同的權(quán)重,從而實(shí)現(xiàn)處理能力高的服務(wù)器可承擔(dān)更多的請(qǐng)求,處理能力低的服務(wù)器少承擔(dān)請(qǐng)求。
上一篇我們實(shí)現(xiàn)resolverBuilder接口,用來解析服務(wù)器地址,而負(fù)載均衡算法就是在這個(gè)接口基礎(chǔ)上篩選出一個(gè)地址,這個(gè)過程是在客戶端發(fā)送請(qǐng)求的時(shí)候進(jìn)行。
以下示例基于grpc v1.26.0版本,更高的版本不兼容etcd,不方便測(cè)試,而接口名或者參數(shù)也會(huì)不同,不過原理是相似的
自定義負(fù)載均衡策略
使用自定義的負(fù)載均衡策略主要實(shí)現(xiàn)V2PickerBuilder和V2Picker這兩個(gè)接口
type V2PickerBuilder interface {
Build(info PickerBuildInfo) balancer.V2Picker
}
Build方法:返回一個(gè)V2選擇器,將用于gRPC選擇子連接。
type V2Picker interface {
Pick(info PickInfo) (PickResult, error)
}
Pick方法:子連接選擇,具體的算法在這個(gè)方法內(nèi)實(shí)現(xiàn)
完整代碼:
package rpc
import (
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
"google.golang.org/grpc/grpclog"
"math/rand"
"sync"
)
const WEIGHT_LB_NAME = "weight"
func init() {
balancer.Register(
base.NewBalancerBuilderV2(WEIGHT_LB_NAME,&LocalBuilder{},base.Config{HealthCheck: false}))
}
type LocalBuilder struct {
}
func (*LocalBuilder) Build(info base.PickerBuildInfo) balancer.V2Picker {
if len(info.ReadySCs) == 0 {
return base.NewErrPickerV2(balancer.ErrNoSubConnAvailable)
}
var scs []balancer.SubConn
for subConn,addr := range info.ReadySCs {
weight := addr.Address.Attributes.Value("weight").(int)
if weight <= 0 {
weight = 1
}
for i := 0; i < weight; i++ {
scs = append(scs, subConn)
}
}
return &localPick{
subConn: scs,
}
}
type localPick struct {
subConn []balancer.SubConn
mu sync.Mutex
}
func (l *localPick)Pick(info balancer.PickInfo) (r balancer.PickResult, err error){
l.mu.Lock()
index := rand.Intn(len(l.subConn))
r.SubConn = l.subConn[index]
l.mu.Lock()
return r,nil
}
結(jié)合示例再看一下Build(info PickerBuildInfo) balancer.V2Picker這個(gè)方法的作用:
這個(gè)函數(shù)的返回值就是我們還要實(shí)現(xiàn)的第二個(gè)接口,所以這里主要看一下參數(shù)
type PickerBuildInfo struct {
// ReadySCs is a map from all ready SubConns to the Addresses used to
// create them.
ReadySCs map[balancer.SubConn]
}
type SubConnInfo struct {
Address resolver.Address // the address used to create this SubConn
}
ReadySCs:是所有可用的子連接
balancer.SubConn:是一個(gè)子連接的結(jié)構(gòu),這里我們不用關(guān)心這個(gè)值,在pick里面直接返回就可以了。
SubConnInfo:里面是一個(gè)Address的結(jié)構(gòu)
type Address struct {
Addr string
ServerName string
// Attributes contains arbitrary data about this address intended for
// consumption by the load balancing policy.
Attributes *attributes.Attributes
Type AddressType
Metadata interface{}
}
...
...
type Attributes struct {
m map[interface{}]interface{}
}
這個(gè)Address就是上一篇中resolverBuilder中設(shè)置的值,所以這兩個(gè)功能是有聯(lián)系的,Attributes的m是一個(gè)map,剛剛好保存我們需要的權(quán)重weight
結(jié)下來就是Pick方法
Pick(info balancer.PickInfo) (r balancer.PickResult, err error)
type PickInfo struct {
// FullMethodName is the method name that NewClientStream() is called
// with. The canonical format is /service/Method.
FullMethodName string //eg : /User/GetInfo
// Ctx is the RPC's context, and may contain relevant RPC-level information
// like the outgoing header metadata.
Ctx context.Context
}
type PickResult struct {
SubConn SubConn
Done func(DoneInfo)
}
FullMethodName:對(duì)應(yīng)XXXpb.go中生成的FullMethod: "/Memo/GetMemo",
Ctx: 如注釋所說,包含metadata
PickResult: 返回的子連接
Done:rpc完成之后調(diào)用Done
使用新建的策略
func main() {
grpc.UseCompressor(gzip.Name)
conn, err := grpc.Dial(
fmt.Sprintf("%s:///%s", "game", baseService),
//grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name)),
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, rpc.WEIGHT_LB_NAME)),
grpc.WithInsecure(),
)
...
}