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/direct/client.go

46 lines
908 B
Go

4 years ago
package main
import (
"context"
"flag"
"fmt"
"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"
4 years ago
)
const timeFormat = "15:04:05"
func main() {
flag.Parse()
client := rpcx.MustNewClient(rpcx.RpcClientConf{
Etcd: discov.EtcdConf{
Hosts: []string{"localhost:2379"},
Key: "rpcx",
},
})
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
conn := client.Conn()
4 years ago
greet := unary.NewGreeterClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
resp, err := greet.Greet(ctx, &unary.Request{
Name: "kevin",
})
if err != nil {
fmt.Printf("%s X %s\n", time.Now().Format(timeFormat), err.Error())
} else {
fmt.Printf("%s => %s\n", time.Now().Format(timeFormat), resp.Greet)
}
cancel()
}
}
}