|
| 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 | + |
| 19 | +// Binary client is an example client. |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + "log" |
| 26 | + "time" |
| 27 | + |
| 28 | + "google.golang.org/grpc" |
| 29 | + ecpb "google.golang.org/grpc/examples/features/proto/echo" |
| 30 | + "google.golang.org/grpc/resolver" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + exampleScheme = "example" |
| 35 | + exampleServiceName = "lb.example.grpc.io" |
| 36 | +) |
| 37 | + |
| 38 | +var addrs = []string{"localhost:50051", "localhost:50052"} |
| 39 | + |
| 40 | +func callUnaryEcho(c ecpb.EchoClient, message string) { |
| 41 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 42 | + defer cancel() |
| 43 | + r, err := c.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message}) |
| 44 | + if err != nil { |
| 45 | + log.Fatalf("could not greet: %v", err) |
| 46 | + } |
| 47 | + fmt.Println(r.Message) |
| 48 | +} |
| 49 | + |
| 50 | +func makeRPCs(cc *grpc.ClientConn, n int) { |
| 51 | + hwc := ecpb.NewEchoClient(cc) |
| 52 | + for i := 0; i < n; i++ { |
| 53 | + callUnaryEcho(hwc, "this is examples/load_balancing") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func main() { |
| 58 | + pickfirstConn, err := grpc.Dial( |
| 59 | + fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName), |
| 60 | + // grpc.WithBalancerName("pick_first"), // "pick_first" is the default, so this DialOption is not necessary. |
| 61 | + grpc.WithInsecure(), |
| 62 | + ) |
| 63 | + if err != nil { |
| 64 | + log.Fatalf("did not connect: %v", err) |
| 65 | + } |
| 66 | + defer pickfirstConn.Close() |
| 67 | + |
| 68 | + fmt.Println("--- calling helloworld.Greeter/SayHello with pick_first ---") |
| 69 | + makeRPCs(pickfirstConn, 10) |
| 70 | + |
| 71 | + fmt.Println() |
| 72 | + |
| 73 | + // Make another ClientConn with round_robin policy. |
| 74 | + roundrobinConn, err := grpc.Dial( |
| 75 | + fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName), |
| 76 | + grpc.WithBalancerName("round_robin"), // This sets the initial balancing policy. |
| 77 | + grpc.WithInsecure(), |
| 78 | + ) |
| 79 | + if err != nil { |
| 80 | + log.Fatalf("did not connect: %v", err) |
| 81 | + } |
| 82 | + defer roundrobinConn.Close() |
| 83 | + |
| 84 | + fmt.Println("--- calling helloworld.Greeter/SayHello with round_robin ---") |
| 85 | + makeRPCs(roundrobinConn, 10) |
| 86 | +} |
| 87 | + |
| 88 | +// Following is an example name resolver implementation. Read the name |
| 89 | +// resolution example to learn more about it. |
| 90 | + |
| 91 | +type exampleResolverBuilder struct{} |
| 92 | + |
| 93 | +func (*exampleResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) { |
| 94 | + r := &exampleResolver{ |
| 95 | + target: target, |
| 96 | + cc: cc, |
| 97 | + addrsStore: map[string][]string{ |
| 98 | + exampleServiceName: addrs, |
| 99 | + }, |
| 100 | + } |
| 101 | + r.start() |
| 102 | + return r, nil |
| 103 | +} |
| 104 | +func (*exampleResolverBuilder) Scheme() string { return exampleScheme } |
| 105 | + |
| 106 | +type exampleResolver struct { |
| 107 | + target resolver.Target |
| 108 | + cc resolver.ClientConn |
| 109 | + addrsStore map[string][]string |
| 110 | +} |
| 111 | + |
| 112 | +func (r *exampleResolver) start() { |
| 113 | + addrStrs := r.addrsStore[r.target.Endpoint] |
| 114 | + addrs := make([]resolver.Address, len(addrStrs), len(addrStrs)) |
| 115 | + for i, s := range addrStrs { |
| 116 | + addrs[i] = resolver.Address{Addr: s} |
| 117 | + } |
| 118 | + r.cc.NewAddress(addrs) |
| 119 | +} |
| 120 | +func (*exampleResolver) ResolveNow(o resolver.ResolveNowOption) {} |
| 121 | +func (*exampleResolver) Close() {} |
| 122 | + |
| 123 | +func init() { |
| 124 | + resolver.Register(&exampleResolverBuilder{}) |
| 125 | +} |
0 commit comments