client.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // This binary can only run on Google Cloud Platform (GCP).
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "time"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/credentials/alts"
  26. "google.golang.org/grpc/grpclog"
  27. testgrpc "google.golang.org/grpc/interop/grpc_testing"
  28. testpb "google.golang.org/grpc/interop/grpc_testing"
  29. )
  30. var (
  31. hsAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
  32. serverAddr = flag.String("server_address", ":8080", "The port on which the server is listening")
  33. logger = grpclog.Component("interop")
  34. )
  35. func main() {
  36. flag.Parse()
  37. opts := alts.DefaultClientOptions()
  38. if *hsAddr != "" {
  39. opts.HandshakerServiceAddress = *hsAddr
  40. }
  41. altsTC := alts.NewClientCreds(opts)
  42. // Block until the server is ready.
  43. conn, err := grpc.Dial(*serverAddr, grpc.WithTransportCredentials(altsTC), grpc.WithBlock())
  44. if err != nil {
  45. logger.Fatalf("gRPC Client: failed to dial the server at %v: %v", *serverAddr, err)
  46. }
  47. defer conn.Close()
  48. grpcClient := testgrpc.NewTestServiceClient(conn)
  49. // Call the EmptyCall API.
  50. ctx := context.Background()
  51. request := &testpb.Empty{}
  52. if _, err := grpcClient.EmptyCall(ctx, request); err != nil {
  53. logger.Fatalf("grpc Client: EmptyCall(_, %v) failed: %v", request, err)
  54. }
  55. logger.Info("grpc Client: empty call succeeded")
  56. // This sleep prevents the connection from being abruptly disconnected
  57. // when running this binary (along with grpc_server) on GCP dev cluster.
  58. time.Sleep(1 * time.Second)
  59. }