2015-12-08 00:09:10 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/cmd"
|
|
|
|
"github.com/micro/go-micro/errors"
|
|
|
|
example "github.com/micro/go-micro/examples/server/proto/example"
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
2015-12-08 02:56:17 +03:00
|
|
|
// Built in random hashed node selector
|
|
|
|
type nodeSelector struct {
|
|
|
|
r registry.Registry
|
|
|
|
}
|
|
|
|
|
2015-12-08 03:11:51 +03:00
|
|
|
func (n *nodeSelector) Select(ctx context.Context, req client.Request) (*registry.Node, error) {
|
2015-12-08 02:56:17 +03:00
|
|
|
service, err := n.r.GetService(req.Service())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) == 0 {
|
2015-12-08 00:09:10 +03:00
|
|
|
return nil, errors.NotFound("go.micro.client", "Service not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
i := rand.Int()
|
2015-12-08 02:56:17 +03:00
|
|
|
j := i % len(service)
|
2015-12-08 00:09:10 +03:00
|
|
|
|
2015-12-08 02:56:17 +03:00
|
|
|
if len(service[j].Nodes) == 0 {
|
2015-12-08 00:09:10 +03:00
|
|
|
return nil, errors.NotFound("go.micro.client", "Service not found")
|
|
|
|
}
|
|
|
|
|
2015-12-08 02:56:17 +03:00
|
|
|
k := i % len(service[j].Nodes)
|
|
|
|
return service[j].Nodes[k], nil
|
2015-12-08 00:09:10 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 02:56:17 +03:00
|
|
|
func (n *nodeSelector) Response(node *registry.Node, err error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodeSelector) Reset() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a new random node selector
|
|
|
|
func RandomSelector(r registry.Registry) client.NodeSelector {
|
|
|
|
return &nodeSelector{r}
|
2015-12-08 00:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-12-08 02:56:17 +03:00
|
|
|
if err := client.Call(context.Background(), req, rsp); err != nil {
|
2015-12-08 00:09:10 +03:00
|
|
|
fmt.Println("call err: ", err, rsp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Call:", i, "rsp:", rsp.Msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Init()
|
|
|
|
|
|
|
|
client.DefaultClient = client.NewClient(
|
2015-12-08 02:56:17 +03:00
|
|
|
client.Select(RandomSelector),
|
2015-12-08 00:09:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fmt.Println("\n--- Call example ---\n")
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
call(i)
|
|
|
|
}
|
|
|
|
}
|