Commit 02003dc0 authored by AlexStocks's avatar AlexStocks

Add: hash load balance func

parent c8c1e4f1
...@@ -10,10 +10,13 @@ ...@@ -10,10 +10,13 @@
package main package main
import ( import (
"context"
"math/rand"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal" "os/signal"
"sync/atomic"
"syscall" "syscall"
"time" "time"
) )
...@@ -22,6 +25,9 @@ import ( ...@@ -22,6 +25,9 @@ import (
"github.com/AlexStocks/getty-examples/micro/proto" "github.com/AlexStocks/getty-examples/micro/proto"
"github.com/AlexStocks/getty/micro" "github.com/AlexStocks/getty/micro"
"github.com/AlexStocks/getty/rpc" "github.com/AlexStocks/getty/rpc"
"github.com/AlexStocks/goext/database/filter"
"github.com/AlexStocks/goext/database/registry"
"github.com/AlexStocks/goext/log"
"github.com/AlexStocks/goext/net" "github.com/AlexStocks/goext/net"
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
jerrors "github.com/juju/errors" jerrors "github.com/juju/errors"
...@@ -33,8 +39,13 @@ const ( ...@@ -33,8 +39,13 @@ const (
var ( var (
client *micro.Client client *micro.Client
seq int64
) )
func init() {
rand.Seed(time.Now().UnixNano())
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// main // main
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
...@@ -64,9 +75,32 @@ func initProfiling() { ...@@ -64,9 +75,32 @@ func initProfiling() {
}() }()
} }
func LoadBalance(ctx context.Context, arr *gxfilter.ServiceArray) (*gxregistry.Service, error) {
var (
ok bool
seq int64
)
if ctx == nil {
seq = int64(rand.Int())
} else if seq, ok = ctx.Value("seq").(int64); !ok {
return nil, jerrors.Errorf("illegal seq %#v", ctx.Value("seq"))
}
arrLen := len(arr.Arr)
if arrLen == 0 {
return nil, jerrors.Errorf("@arr length is 0")
}
service := arr.Arr[int(seq)%arrLen]
gxlog.CInfo("seq %d, service %#v", seq, service)
return service, nil
// return arr.Arr[seq%arrLen], nil
// context.WithValue(context.Background(), key, value).Value(key)
}
func initClient() { func initClient() {
var err error var err error
client, err = micro.NewClient(&conf.ClientConfig, &conf.Registry) client, err = micro.NewClient(&conf.ClientConfig, &conf.Registry, micro.WithServiceHash(LoadBalance))
if err != nil { if err != nil {
panic(jerrors.ErrorStack(err)) panic(jerrors.ErrorStack(err))
} }
...@@ -109,27 +143,31 @@ func initSignal() { ...@@ -109,27 +143,31 @@ func initSignal() {
func testJSON() { func testJSON() {
ts := micro_examples.TestService{} ts := micro_examples.TestService{}
ctx := context.WithValue(context.Background(), "seq", atomic.AddInt64(&seq, 1))
testReq := micro_examples.TestReq{"aaa", "bbb", "ccc"} testReq := micro_examples.TestReq{"aaa", "bbb", "ccc"}
testRsp := micro_examples.TestRsp{} testRsp := micro_examples.TestRsp{}
err := client.Call(nil, rpc.CodecJson, ts.Service(), ts.Version(), "Test", &testReq, &testRsp) err := client.Call(ctx, rpc.CodecJson, ts.Service(), ts.Version(), "Test", &testReq,
&testRsp)
if err != nil { if err != nil {
log.Error("client.Call(Json, TestService::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(Json, TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestService::Test(Json, param:%#v) = res:%s", testReq, testRsp) log.Info("TestService::Test(Json, param:%#v) = res:%s", testReq, testRsp)
ctx = context.WithValue(context.Background(), "seq", atomic.AddInt64(&seq, 1))
addReq := micro_examples.AddReq{1, 10} addReq := micro_examples.AddReq{1, 10}
addRsp := micro_examples.AddRsp{} addRsp := micro_examples.AddRsp{}
err = client.Call(nil, rpc.CodecJson, ts.Service(), ts.Version(), "Add", &addReq, &addRsp) err = client.Call(ctx, rpc.CodecJson, ts.Service(), ts.Version(), "Add", &addReq, &addRsp)
if err != nil { if err != nil {
log.Error("client.Call(Json, TestService::Add) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(Json, TestService::Add) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestService::Add(Json, req:%#v) = res:%#v", addReq, addRsp) log.Info("TestService::Add(Json, req:%#v) = res:%#v", addReq, addRsp)
ctx = context.WithValue(context.Background(), "seq", atomic.AddInt64(&seq, 1))
errReq := micro_examples.ErrReq{1} errReq := micro_examples.ErrReq{1}
errRsp := micro_examples.ErrRsp{} errRsp := micro_examples.ErrRsp{}
err = client.Call(nil, rpc.CodecJson, ts.Service(), ts.Version(), "Err", &errReq, &errRsp) err = client.Call(ctx, rpc.CodecJson, ts.Service(), ts.Version(), "Err", &errReq, &errRsp)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(Json, TestService::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(Json, TestService::Err) = error:%s", jerrors.ErrorStack(err))
...@@ -140,27 +178,31 @@ func testJSON() { ...@@ -140,27 +178,31 @@ func testJSON() {
func testProtobuf() { func testProtobuf() {
ts := micro_examples.TestService{} ts := micro_examples.TestService{}
ctx := context.WithValue(context.Background(), "seq", atomic.AddInt64(&seq, 1))
testReq := micro_examples.TestReq{"aaa", "bbb", "ccc"} testReq := micro_examples.TestReq{"aaa", "bbb", "ccc"}
testRsp := micro_examples.TestRsp{} testRsp := micro_examples.TestRsp{}
err := client.Call(nil, rpc.CodecProtobuf, ts.Service(), ts.Version(), "Test", &testReq, &testRsp) err := client.Call(ctx, rpc.CodecProtobuf, ts.Service(), ts.Version(), "Test", &testReq,
&testRsp)
if err != nil { if err != nil {
log.Error("client.Call(protobuf, TestService::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(protobuf, TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestService::Test(protobuf, param:%#v) = res:%s", testReq, testRsp) log.Info("TestService::Test(protobuf, param:%#v) = res:%s", testReq, testRsp)
ctx = context.WithValue(context.Background(), "seq", atomic.AddInt64(&seq, 1))
addReq := micro_examples.AddReq{1, 10} addReq := micro_examples.AddReq{1, 10}
addRsp := micro_examples.AddRsp{} addRsp := micro_examples.AddRsp{}
err = client.Call(nil, rpc.CodecProtobuf, ts.Service(), ts.Version(), "Add", &addReq, &addRsp) err = client.Call(ctx, rpc.CodecProtobuf, ts.Service(), ts.Version(), "Add", &addReq, &addRsp)
if err != nil { if err != nil {
log.Error("client.Call(protobuf, TestService::Add) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(protobuf, TestService::Add) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestService::Add(protobuf, req:%#v) = res:%#v", addReq, addRsp) log.Info("TestService::Add(protobuf, req:%#v) = res:%#v", addReq, addRsp)
ctx = context.WithValue(context.Background(), "seq", atomic.AddInt64(&seq, 1))
errReq := micro_examples.ErrReq{1} errReq := micro_examples.ErrReq{1}
errRsp := micro_examples.ErrRsp{} errRsp := micro_examples.ErrRsp{}
err = client.Call(nil, rpc.CodecProtobuf, ts.Service(), ts.Version(), "Err", &errReq, &errRsp) err = client.Call(ctx, rpc.CodecProtobuf, ts.Service(), ts.Version(), "Err", &errReq, &errRsp)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(protobuf, TestService::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(protobuf, TestService::Err) = error:%s", jerrors.ErrorStack(err))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment