fake_grpclb.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 file is for testing only. Runs a fake grpclb balancer server.
  19. // The name of the service to load balance for and the addresses
  20. // of that service are provided by command line flags.
  21. package main
  22. import (
  23. "flag"
  24. "strings"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/credentials"
  27. "google.golang.org/grpc/credentials/alts"
  28. "google.golang.org/grpc/grpclog"
  29. "google.golang.org/grpc/internal/testutils/fakegrpclb"
  30. "google.golang.org/grpc/testdata"
  31. )
  32. var (
  33. port = flag.Int("port", 10000, "Port to listen on.")
  34. backendAddrs = flag.String("backend_addrs", "", "Comma separated list of backend IP/port addresses.")
  35. useALTS = flag.Bool("use_alts", false, "Listen on ALTS credentials.")
  36. useTLS = flag.Bool("use_tls", false, "Listen on TLS credentials, using a test certificate.")
  37. shortStream = flag.Bool("short_stream", false, "End the balancer stream immediately after sending the first server list.")
  38. serviceName = flag.String("service_name", "UNSET", "Name of the service being load balanced for.")
  39. logger = grpclog.Component("interop")
  40. )
  41. func main() {
  42. flag.Parse()
  43. var opts []grpc.ServerOption
  44. if *useTLS {
  45. certFile := testdata.Path("server1.pem")
  46. keyFile := testdata.Path("server1.key")
  47. creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
  48. if err != nil {
  49. logger.Fatalf("Failed to generate credentials: %v", err)
  50. }
  51. opts = append(opts, grpc.Creds(creds))
  52. } else if *useALTS {
  53. altsOpts := alts.DefaultServerOptions()
  54. altsTC := alts.NewServerCreds(altsOpts)
  55. opts = append(opts, grpc.Creds(altsTC))
  56. }
  57. rawBackendAddrs := strings.Split(*backendAddrs, ",")
  58. server, err := fakegrpclb.NewServer(fakegrpclb.ServerParams{
  59. ListenPort: *port,
  60. ServerOptions: opts,
  61. LoadBalancedServiceName: *serviceName,
  62. LoadBalancedServicePort: 443, // TODO: make this configurable?
  63. BackendAddresses: rawBackendAddrs,
  64. ShortStream: *shortStream,
  65. })
  66. if err != nil {
  67. logger.Fatalf("Failed to create balancer server: %v", err)
  68. }
  69. // Serve() starts serving and blocks until Stop() is called. We don't need to
  70. // call Stop() here since we want the server to run until we are killed.
  71. if err := server.Serve(); err != nil {
  72. logger.Fatalf("Failed to start balancer server: %v", err)
  73. }
  74. }