add Weight/RoundRobin/LeastConnection selector for package gsel; improve package balancer

This commit is contained in:
John Guo
2022-01-27 15:15:55 +08:00
parent d322e67117
commit c845d1d93d
26 changed files with 398 additions and 189 deletions

View File

@ -5,6 +5,7 @@ go 1.15
require (
github.com/gogf/gf/contrib/registry/etcd/v2 v2.0.0-rc2
github.com/gogf/gf/contrib/resolver/v2 v2.0.0-rc2
github.com/gogf/gf/contrib/balancer/v2 v2.0.0-rc2
github.com/gogf/gf/v2 v2.0.0-rc2
github.com/golang/protobuf v1.5.2
google.golang.org/grpc v1.43.0
@ -12,6 +13,7 @@ require (
)
replace (
github.com/gogf/gf/contrib/balancer/v2 => ../contrib/balancer/
github.com/gogf/gf/contrib/registry/etcd/v2 => ../contrib/registry/etcd/
github.com/gogf/gf/contrib/resolver/v2 => ../contrib/resolver/
github.com/gogf/gf/v2 => ../

View File

@ -1,6 +1,9 @@
package main
import (
"time"
"github.com/gogf/gf/contrib/balancer/v2"
"github.com/gogf/gf/contrib/registry/etcd/v2"
"github.com/gogf/gf/contrib/resolver/v2"
pb "github.com/gogf/gf/example/rawgrpc/helloworld"
@ -21,17 +24,21 @@ func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(
service.KeyWithSchema(),
balancer.WithRandom(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
g.Log().Fatalf(ctx, "did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: `GoFrame`})
if err != nil {
g.Log().Fatalf(ctx, "could not greet: %+v", err)
client := pb.NewGreeterClient(conn)
for i := 0; i < 10; i++ {
res, err := client.SayHello(ctx, &pb.HelloRequest{Name: `GoFrame`})
if err != nil {
g.Log().Fatalf(ctx, "could not greet: %+v", err)
}
g.Log().Printf(ctx, "Greeting: %s", res.GetMessage())
time.Sleep(time.Second)
}
g.Log().Printf(ctx, "Greeting: %s", r.GetMessage())
}