github.com/mailgun/holster/v4@v4.20.0/discovery/README.md (about)

     1  ### GRPCSrvBuilder
     2  This build returns a GRPC resolver which will preform an DNS SRV record lookup on the provided domain name. Combined
     3  with round-robin load balancing, it will instruct the GRPC client to load balance each request to every node returned
     4  by the DNS SRV lookup.
     5  
     6  ```go
     7  
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  
    14  	"github.com/davecgh/go-spew/spew"
    15  	"github.com/mailgun/holster/v4/discovery"
    16  	"github.com/mailgun/ratelimits/v2"
    17  	"github.com/sirupsen/logrus"
    18  	"google.golang.org/grpc"
    19  	"google.golang.org/grpc/resolver"
    20  )
    21  
    22  func main() {
    23  	// Optional, will log the list of addresses dns-srv discovered when requesting the SRV records
    24  	discovery.GRPCSrvLogAddresses = false
    25  
    26  	// Optional, allows you to override the default logger. dns-srv will log an error when it is 
    27  	// unable to request the SRV records.
    28  	discovery.GRPCSrvDefaultLogger = logrus.New()
    29  	
    30  	// Required to register `dns-srv:///` as a schema GRPC can understand
    31  	resolver.Register(discovery.NewGRPCSRVBuilder())
    32  
    33  	c, err := grpc.Dial("dns-srv:///ratelimits-grpc.service.us-east4.prod.mailforce:8201",
    34  		// Enable round-robin load balancing
    35  		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
    36  		grpc.WithInsecure(),
    37  	)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	rl := ratelimits.NewV1Client(c)
    43  
    44  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    45  	defer cancel()
    46  
    47  	resp, err := rl.HealthCheck(ctx, &ratelimits.HealthCheckReq{})
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	spew.Dump(resp)
    52  
    53  	// It can take some time for dns-srv to fetch all and resolve all the SRV
    54  	// records for a service, especially if there are a lot of them.
    55  	time.Sleep(time.Second * 15)
    56  
    57  	// These requests should round-robin
    58  	resp, err = rl.HealthCheck(ctx, &ratelimits.HealthCheckReq{})
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	spew.Dump(resp)
    63  	resp, _ = rl.HealthCheck(ctx, &ratelimits.HealthCheckReq{})
    64  	spew.Dump(resp)
    65  	resp, _ = rl.HealthCheck(ctx, &ratelimits.HealthCheckReq{})
    66  	spew.Dump(resp)
    67  	resp, _ = rl.HealthCheck(ctx, &ratelimits.HealthCheckReq{})
    68  	spew.Dump(resp)
    69  	
    70  	c.Close()
    71  }
    72  ```
    73  
    74  ### SRV Resolver
    75  
    76  `discovery.GetSRVAddresses()` can be used directly as a utility for querying SRV DNS records. For example:
    77  
    78  ```go
    79  // Using "" as the dnsServer argument uses the Golang built-in resolver
    80  addresses, err = discovery.GetSRVAddresses("mytest.service.consul", "")
    81  ```
    82  
    83  If valid, will return a list of IP:Port records associated to the given DNS entry.
    84  
    85  Note: you can specify an explicit DNS server instead. Using `""` uses the DNS resolver of the machine.