前言
grpc默認支持兩種負載均衡算法pick_first 和 round_robin
輪詢法round_robin不能滿足因服務器配置不同而承擔不同負載量拐迁,這篇文章將介紹如何實現(xiàn)自定義負載均衡策略--加權隨機。
加權隨機法可以根據(jù)服務器的處理能力而分配不同的權重绿语,從而實現(xiàn)處理能力高的服務器可承擔更多的請求叹括,處理能力低的服務器少承擔請求隅熙。
上一篇我們實現(xiàn)resolverBuilder接口哲戚,用來解析服務器地址,而負載均衡算法就是在這個接口基礎上篩選出一個地址荤堪,這個過程是在客戶端發(fā)送請求的時候進行。
以下示例基于grpc v1.26.0版本,更高的版本不兼容etcd逞力,不方便測試曙寡,而接口名或者參數(shù)也會不同,不過原理是相似的
自定義負載均衡策略
使用自定義的負載均衡策略主要實現(xiàn)V2PickerBuilder
和V2Picker
這兩個接口
type V2PickerBuilder interface {
Build(info PickerBuildInfo) balancer.V2Picker
}
Build
方法:返回一個V2選擇器寇荧,將用于gRPC選擇子連接。
type V2Picker interface {
Pick(info PickInfo) (PickResult, error)
}
Pick
方法:子連接選擇执隧,具體的算法在這個方法內實現(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
}
結合示例再看一下Build(info PickerBuildInfo) balancer.V2Picker
這個方法的作用:
這個函數(shù)的返回值就是我們還要實現(xiàn)的第二個接口揩抡,所以這里主要看一下參數(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
:是一個子連接的結構,這里我們不用關心這個值镀琉,在pick里面直接返回就可以了峦嗤。
SubConnInfo
:里面是一個Address的結構
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{}
}
這個Address就是上一篇中resolverBuilder中設置的值,所以這兩個功能是有聯(lián)系的屋摔,Attributes的m是一個map烁设,剛剛好保存我們需要的權重weight
結下來就是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
:對應XXXpb.go中生成的FullMethod: "/Memo/GetMemo",
Ctx
: 如注釋所說,包含metadata
PickResult
: 返回的子連接
Done
:rpc完成之后調用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(),
)
...
}