github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/client/selector/selector.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"math/rand"
     7  	"time"
     8  
     9  	example "github.com/micro/go-micro/examples/server/proto/example"
    10  	"github.com/micro/go-micro/v2/client"
    11  	"github.com/micro/go-micro/v2/client/selector"
    12  	"github.com/micro/go-micro/v2/config/cmd"
    13  	"github.com/micro/go-micro/v2/registry"
    14  )
    15  
    16  func init() {
    17  	rand.Seed(time.Now().Unix())
    18  }
    19  
    20  // Built in random hashed node selector
    21  type firstNodeSelector struct {
    22  	opts selector.Options
    23  }
    24  
    25  func (n *firstNodeSelector) Init(opts ...selector.Option) error {
    26  	for _, o := range opts {
    27  		o(&n.opts)
    28  	}
    29  	return nil
    30  }
    31  
    32  func (n *firstNodeSelector) Options() selector.Options {
    33  	return n.opts
    34  }
    35  
    36  func (n *firstNodeSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
    37  	services, err := n.opts.Registry.GetService(service)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	if len(services) == 0 {
    43  		return nil, selector.ErrNotFound
    44  	}
    45  
    46  	var sopts selector.SelectOptions
    47  	for _, opt := range opts {
    48  		opt(&sopts)
    49  	}
    50  
    51  	for _, filter := range sopts.Filters {
    52  		services = filter(services)
    53  	}
    54  
    55  	if len(services) == 0 {
    56  		return nil, selector.ErrNotFound
    57  	}
    58  
    59  	if len(services[0].Nodes) == 0 {
    60  		return nil, selector.ErrNotFound
    61  	}
    62  
    63  	return func() (*registry.Node, error) {
    64  		return services[0].Nodes[0], nil
    65  	}, nil
    66  }
    67  
    68  func (n *firstNodeSelector) Mark(service string, node *registry.Node, err error) {
    69  	return
    70  }
    71  
    72  func (n *firstNodeSelector) Reset(service string) {
    73  	return
    74  }
    75  
    76  func (n *firstNodeSelector) Close() error {
    77  	return nil
    78  }
    79  
    80  func (n *firstNodeSelector) String() string {
    81  	return "first"
    82  }
    83  
    84  // Return a new first node selector
    85  func FirstNodeSelector(opts ...selector.Option) selector.Selector {
    86  	var sopts selector.Options
    87  	for _, opt := range opts {
    88  		opt(&sopts)
    89  	}
    90  	if sopts.Registry == nil {
    91  		sopts.Registry = registry.DefaultRegistry
    92  	}
    93  	return &firstNodeSelector{sopts}
    94  }
    95  
    96  func call(i int) {
    97  	// Create new request to service go.micro.srv.example, method Example.Call
    98  	req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{
    99  		Name: "John",
   100  	})
   101  
   102  	rsp := &example.Response{}
   103  
   104  	// Call service
   105  	if err := client.Call(context.Background(), req, rsp); err != nil {
   106  		fmt.Println("call err: ", err, rsp)
   107  		return
   108  	}
   109  
   110  	fmt.Println("Call:", i, "rsp:", rsp.Msg)
   111  }
   112  
   113  func main() {
   114  	cmd.Init()
   115  
   116  	client.DefaultClient = client.NewClient(
   117  		client.Selector(FirstNodeSelector()),
   118  	)
   119  
   120  	fmt.Println("\n--- Call example ---")
   121  	for i := 0; i < 10; i++ {
   122  		call(i)
   123  	}
   124  }