You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/example/rpc/client/lb/main.go

58 lines
1.0 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"log"
"time"
"github.com/tal-tech/go-zero/core/discov"
"github.com/tal-tech/go-zero/example/rpc/remote/unary"
"github.com/tal-tech/go-zero/rpcx"
)
var lb = flag.String("t", "direct", "the load balancer type")
func main() {
flag.Parse()
var cli rpcx.Client
switch *lb {
case "direct":
cli = rpcx.MustNewClient(rpcx.RpcClientConf{
Endpoints: []string{
"localhost:3456",
"localhost:3457",
},
})
case "discov":
cli = rpcx.MustNewClient(rpcx.RpcClientConf{
Etcd: discov.EtcdConf{
Hosts: []string{"localhost:2379"},
Key: "rpcx",
},
})
default:
log.Fatal("bad load balancing type")
}
greet := unary.NewGreeterClient(cli.Conn())
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
resp, err := greet.Greet(context.Background(), &unary.Request{
Name: "kevin",
})
if err != nil {
fmt.Println("X", err.Error())
} else {
fmt.Println("=>", resp.Greet)
}
}
}
}