|
| 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 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + "strings" |
| 26 | + "time" |
| 27 | + |
| 28 | + "google.golang.org/grpc" |
| 29 | + hwpb "google.golang.org/grpc/examples/helloworld/helloworld" |
| 30 | + "google.golang.org/grpc/resolver" |
| 31 | +) |
| 32 | + |
| 33 | +const splitScheme = "split" |
| 34 | + |
| 35 | +var addrs = []string{"localhost:50051", "localhost:50052"} |
| 36 | + |
| 37 | +type splitResolverBuilder struct{} |
| 38 | + |
| 39 | +func (*splitResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) { |
| 40 | + r := &splitResolver{target: target, cc: cc} |
| 41 | + r.start() |
| 42 | + return r, nil |
| 43 | +} |
| 44 | +func (*splitResolverBuilder) Scheme() string { return splitScheme } |
| 45 | + |
| 46 | +type splitResolver struct { |
| 47 | + target resolver.Target |
| 48 | + cc resolver.ClientConn |
| 49 | +} |
| 50 | + |
| 51 | +func (r *splitResolver) start() { |
| 52 | + splitted := strings.Split(r.target.Endpoint, ",") |
| 53 | + addrs := make([]resolver.Address, len(splitted), len(splitted)) |
| 54 | + for i, s := range splitted { |
| 55 | + addrs[i] = resolver.Address{Addr: s} |
| 56 | + } |
| 57 | + r.cc.NewAddress(addrs) |
| 58 | +} |
| 59 | +func (*splitResolver) ResolveNow(o resolver.ResolveNowOption) {} |
| 60 | +func (*splitResolver) Close() {} |
| 61 | + |
| 62 | +func init() { resolver.Register(&splitResolverBuilder{}) } |
| 63 | + |
| 64 | +/////////////////////////////////////////////////////////////////////// |
| 65 | + |
| 66 | +// callSayHello calls SayHello on c with the given name, and prints the |
| 67 | +// response. |
| 68 | +func callSayHello(c hwpb.GreeterClient, name string) { |
| 69 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 70 | + defer cancel() |
| 71 | + r, err := c.SayHello(ctx, &hwpb.HelloRequest{Name: name}) |
| 72 | + if err != nil { |
| 73 | + log.Fatalf("could not greet: %v", err) |
| 74 | + } |
| 75 | + fmt.Println(r.Message) |
| 76 | +} |
| 77 | + |
| 78 | +func makeRPCs(cc *grpc.ClientConn, n int) { |
| 79 | + hwc := hwpb.NewGreeterClient(cc) |
| 80 | + for i := 0; i < n; i++ { |
| 81 | + callSayHello(hwc, "lb") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func main() { |
| 86 | + pickfirstConn, err := grpc.Dial( |
| 87 | + fmt.Sprintf("%s:///%s", splitScheme, strings.Join(addrs, ",")), |
| 88 | + // grpc.WithBalancerName("pick_first"), // "pick_first" is the default, so this DialOption is not necessary. |
| 89 | + grpc.WithInsecure(), |
| 90 | + ) |
| 91 | + if err != nil { |
| 92 | + log.Fatalf("did not connect: %v", err) |
| 93 | + } |
| 94 | + defer pickfirstConn.Close() |
| 95 | + |
| 96 | + fmt.Println("--- calling helloworld.Greeter/SayHello with pick_first ---") |
| 97 | + makeRPCs(pickfirstConn, 10) |
| 98 | + |
| 99 | + fmt.Println() |
| 100 | + |
| 101 | + // Make another ClientConn with round_robin policy. |
| 102 | + roundrobinConn, err := grpc.Dial( |
| 103 | + fmt.Sprintf("%s:///%s", splitScheme, strings.Join(addrs, ",")), |
| 104 | + grpc.WithBalancerName("round_robin"), // This sets the initial balancing policy. |
| 105 | + grpc.WithInsecure(), |
| 106 | + ) |
| 107 | + if err != nil { |
| 108 | + log.Fatalf("did not connect: %v", err) |
| 109 | + } |
| 110 | + defer roundrobinConn.Close() |
| 111 | + |
| 112 | + fmt.Println("--- calling helloworld.Greeter/SayHello with round_robin ---") |
| 113 | + makeRPCs(roundrobinConn, 10) |
| 114 | +} |
0 commit comments