47 lines
916 B
Go
47 lines
916 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/micro/plugins/v5/client/http"
|
|
"github.com/micro/plugins/v5/registry/consul"
|
|
"go-micro.dev/v5/client"
|
|
"go-micro.dev/v5/registry"
|
|
"go-micro.dev/v5/selector"
|
|
"myschools.me/suguo/go-micro-demo/model"
|
|
)
|
|
|
|
func callAPI2(s selector.Selector) {
|
|
my := http.NewClient(
|
|
client.Selector(s),
|
|
client.ContentType("application/json"),
|
|
)
|
|
|
|
req := my.NewRequest("hello", `/v1/say`, &model.HelloRequest{
|
|
Times: 5,
|
|
})
|
|
var resp model.HelloResponse
|
|
if err := my.Call(context.Background(), req, &resp); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, r := range resp.Data {
|
|
fmt.Printf("id=%d,name=%s\r\n", r.GetId(), r.GetName())
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
consulReg := consul.NewRegistry(
|
|
registry.Addrs("127.0.0.1:8500"),
|
|
)
|
|
|
|
mySelector := selector.NewSelector(
|
|
selector.Registry(consulReg),
|
|
selector.SetStrategy(selector.RoundRobin),
|
|
)
|
|
|
|
callAPI2(mySelector)
|
|
|
|
}
|