110 lines
2.1 KiB
Go
Raw Normal View History

2015-12-09 00:18:06 +00:00
package main
import (
"fmt"
"math/rand"
"time"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd"
example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/registry"
2015-12-09 19:23:16 +00:00
"github.com/micro/go-micro/selector"
2015-12-09 00:18:06 +00:00
"golang.org/x/net/context"
)
func init() {
rand.Seed(time.Now().Unix())
}
// Built in random hashed node selector
type firstNodeSelector struct {
2015-12-09 19:23:16 +00:00
opts selector.Options
2015-12-09 00:18:06 +00:00
}
2015-12-09 19:23:16 +00:00
func (n *firstNodeSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
2015-12-09 00:18:06 +00:00
services, err := n.opts.Registry.GetService(service)
if err != nil {
return nil, err
}
if len(services) == 0 {
2015-12-09 19:23:16 +00:00
return nil, selector.ErrNotFound
2015-12-09 00:18:06 +00:00
}
2015-12-09 19:23:16 +00:00
var sopts selector.SelectOptions
2015-12-09 00:18:06 +00:00
for _, opt := range opts {
opt(&sopts)
}
for _, filter := range sopts.Filters {
services = filter(services)
}
if len(services) == 0 {
2015-12-09 19:23:16 +00:00
return nil, selector.ErrNotFound
2015-12-09 00:18:06 +00:00
}
if len(services[0].Nodes) == 0 {
2015-12-09 19:23:16 +00:00
return nil, selector.ErrNotFound
2015-12-09 00:18:06 +00:00
}
return func() (*registry.Node, error) {
return services[0].Nodes[0], nil
}, nil
}
func (n *firstNodeSelector) Mark(service string, node *registry.Node, err error) {
return
}
func (n *firstNodeSelector) Reset(service string) {
return
}
func (n *firstNodeSelector) Close() error {
return nil
}
// Return a new first node selector
2015-12-09 19:23:16 +00:00
func FirstNodeSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options
2015-12-09 00:18:06 +00:00
for _, opt := range opts {
opt(&sopts)
}
if sopts.Registry == nil {
sopts.Registry = registry.DefaultRegistry
}
return &firstNodeSelector{sopts}
}
func call(i int) {
// Create new request to service go.micro.srv.example, method Example.Call
req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{
Name: "John",
})
rsp := &example.Response{}
// Call service
if err := client.Call(context.Background(), req, rsp); err != nil {
fmt.Println("call err: ", err, rsp)
return
}
fmt.Println("Call:", i, "rsp:", rsp.Msg)
}
func main() {
cmd.Init()
client.DefaultClient = client.NewClient(
client.Selector(FirstNodeSelector()),
)
fmt.Println("\n--- Call example ---\n")
for i := 0; i < 10; i++ {
call(i)
}
}