main.go 686 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/tal-tech/go-zero/core/discov"
  7. "github.com/tal-tech/go-zero/example/rpc/remote/unary"
  8. "github.com/tal-tech/go-zero/rpcx"
  9. )
  10. func main() {
  11. cli := rpcx.MustNewClient(rpcx.RpcClientConf{
  12. Etcd: discov.EtcdConf{
  13. Hosts: []string{"localhost:2379"},
  14. Key: "rpcx",
  15. },
  16. })
  17. greet := unary.NewGreeterClient(cli.Conn())
  18. ticker := time.NewTicker(time.Second)
  19. defer ticker.Stop()
  20. for {
  21. select {
  22. case <-ticker.C:
  23. resp, err := greet.Greet(context.Background(), &unary.Request{
  24. Name: "kevin",
  25. })
  26. if err != nil {
  27. fmt.Println("X", err.Error())
  28. } else {
  29. fmt.Println("=>", resp.Greet)
  30. }
  31. }
  32. }
  33. }