main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // Binary client is an example client.
  19. package main
  20. import (
  21. "context"
  22. "fmt"
  23. "log"
  24. "time"
  25. "google.golang.org/grpc"
  26. ecpb "google.golang.org/grpc/examples/features/proto/echo"
  27. "google.golang.org/grpc/resolver"
  28. )
  29. const (
  30. exampleScheme = "example"
  31. exampleServiceName = "resolver.example.grpc.io"
  32. backendAddr = "localhost:50051"
  33. )
  34. func callUnaryEcho(c ecpb.EchoClient, message string) {
  35. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  36. defer cancel()
  37. r, err := c.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message})
  38. if err != nil {
  39. log.Fatalf("could not greet: %v", err)
  40. }
  41. fmt.Println(r.Message)
  42. }
  43. func makeRPCs(cc *grpc.ClientConn, n int) {
  44. hwc := ecpb.NewEchoClient(cc)
  45. for i := 0; i < n; i++ {
  46. callUnaryEcho(hwc, "this is examples/name_resolving")
  47. }
  48. }
  49. func main() {
  50. passthroughConn, err := grpc.Dial(
  51. fmt.Sprintf("passthrough:///%s", backendAddr), // Dial to "passthrough:///localhost:50051"
  52. grpc.WithInsecure(),
  53. )
  54. if err != nil {
  55. log.Fatalf("did not connect: %v", err)
  56. }
  57. defer passthroughConn.Close()
  58. fmt.Printf("--- calling helloworld.Greeter/SayHello to \"passthrough:///%s\"\n", backendAddr)
  59. makeRPCs(passthroughConn, 10)
  60. fmt.Println()
  61. exampleConn, err := grpc.Dial(
  62. fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName), // Dial to "example:///resolver.example.grpc.io"
  63. grpc.WithInsecure(),
  64. )
  65. if err != nil {
  66. log.Fatalf("did not connect: %v", err)
  67. }
  68. defer exampleConn.Close()
  69. fmt.Printf("--- calling helloworld.Greeter/SayHello to \"%s:///%s\"\n", exampleScheme, exampleServiceName)
  70. makeRPCs(exampleConn, 10)
  71. }
  72. // Following is an example name resolver. It includes a
  73. // ResolverBuilder(https://godoc.org/google.golang.org/grpc/resolver#Builder)
  74. // and a Resolver(https://godoc.org/google.golang.org/grpc/resolver#Resolver).
  75. //
  76. // A ResolverBuilder is registered for a scheme (in this example, "example" is
  77. // the scheme). When a ClientConn is created for this scheme, the
  78. // ResolverBuilder will be picked to build a Resolver. Note that a new Resolver
  79. // is built for each ClientConn. The Resolver will watch the updates for the
  80. // target, and send updates to the ClientConn.
  81. // exampleResolverBuilder is a
  82. // ResolverBuilder(https://godoc.org/google.golang.org/grpc/resolver#Builder).
  83. type exampleResolverBuilder struct{}
  84. func (*exampleResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) {
  85. r := &exampleResolver{
  86. target: target,
  87. cc: cc,
  88. addrsStore: map[string][]string{
  89. exampleServiceName: {backendAddr},
  90. },
  91. }
  92. r.start()
  93. return r, nil
  94. }
  95. func (*exampleResolverBuilder) Scheme() string { return exampleScheme }
  96. // exampleResolver is a
  97. // Resolver(https://godoc.org/google.golang.org/grpc/resolver#Resolver).
  98. type exampleResolver struct {
  99. target resolver.Target
  100. cc resolver.ClientConn
  101. addrsStore map[string][]string
  102. }
  103. func (r *exampleResolver) start() {
  104. addrStrs := r.addrsStore[r.target.Endpoint]
  105. addrs := make([]resolver.Address, len(addrStrs))
  106. for i, s := range addrStrs {
  107. addrs[i] = resolver.Address{Addr: s}
  108. }
  109. r.cc.UpdateState(resolver.State{Addresses: addrs})
  110. }
  111. func (*exampleResolver) ResolveNow(o resolver.ResolveNowOption) {}
  112. func (*exampleResolver) Close() {}
  113. func init() {
  114. // Register the example ResolverBuilder. This is usually done in a package's
  115. // init() function.
  116. resolver.Register(&exampleResolverBuilder{})
  117. }