本文整理匯總了Golang中github.com/micro/go-micro/client.Call函數的典型用法代碼示例。如果您正苦於以下問題:Golang Call函數的具體用法?Golang Call怎麽用?Golang Call使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Call函數的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Hello
func (s *Say) Hello(ctx context.Context, req *api.Request, rsp *api.Response) error {
log.Info("Received Say.Hello API request")
name, ok := req.Get["name"]
if !ok || len(name.Values) == 0 {
return errors.BadRequest("go.micro.api.greeter", "Name cannot be blank")
}
request := client.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{
Name: strings.Join(name.Values, " "),
})
response := &hello.Response{}
if err := client.Call(ctx, request, response); err != nil {
return err
}
rsp.StatusCode = 200
b, _ := json.Marshal(map[string]string{
"message": response.Msg,
})
rsp.Body = string(b)
return nil
}
示例2: Query
// Query API handler
func (es *Elastic) Query(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var input map[string]interface{}
var query []byte
// Unmarshal unknown JSON
if err = json.Unmarshal([]byte(req.Body), &input); err != nil {
return errors.BadRequest("go.micro.api.elastic", err.Error())
}
query, err = json.Marshal(input["query"])
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Query",
&elastic.QueryRequest{
Index: fmt.Sprintf("%v", input["index"]),
Type: fmt.Sprintf("%v", input["type"]),
Query: string(query),
},
)
srvRsp := &elastic.SearchResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = srvRsp.Result
return nil
}
示例3: Update
// Update API handler
func (es *Elastic) Update(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var input map[string]interface{}
var data []byte
// Unmarshal unknown JSON
if err = json.Unmarshal([]byte(req.Body), &input); err != nil {
return errors.BadRequest("go.micro.api.elastic", err.Error())
}
// Marshal unknown JSON (data)
data, err = json.Marshal(input["data"])
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Update",
&elastic.UpdateRequest{
Index: fmt.Sprintf("%v", input["index"]),
Type: fmt.Sprintf("%v", input["type"]),
Id: fmt.Sprintf("%v", input["id"]),
Data: string(data),
},
)
srvRsp := &elastic.UpdateResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = `{}`
return nil
}
示例4: Pong
func (p *Ping) Pong(ctx context.Context, req *proto.Ping, rsp *proto.Pong) error {
rq := client.NewRequest("go.micro.srv.example", "Example.Call", &proto.Request{
Name: "John",
})
rp := &proto.Response{}
// Call service
if err := client.Call(ctx, rq, rp); err != nil {
return err
}
return nil
}
示例5: call
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)
}
示例6: call
func call(i int) {
// Create new request to service go.micro.srv.example, method Example.Call
req := client.NewRequest("go.micro.srv.example", "Ping.Pong", &example.Ping{
Stroke: 10,
})
rsp := &example.Pong{}
// Call service
if err := client.Call(context.TODO(), req, rsp); err != nil {
fmt.Println("call err: ", err, rsp)
return
}
fmt.Println("Call:", i, "rsp:", rsp.Stroke)
}
示例7: requestEntity
func requestEntity(typ string, num int64, radius, lat, lon float64) ([]*common.Entity, error) {
req := client.NewRequest("go.micro.srv.geo", "Location.Search", &loc.SearchRequest{
Center: &common.Point{
Latitude: lat,
Longitude: lon,
},
Type: typ,
Radius: radius,
NumEntities: num,
})
rsp := &loc.SearchResponse{}
err := client.Call(context.Background(), req, rsp)
if err != nil {
return nil, err
}
return rsp.Entities, nil
}
示例8: queryService
func queryService(c *cli.Context) {
if len(c.Args()) < 2 {
fmt.Println("require service and method")
return
}
service := c.Args()[0]
method := c.Args()[1]
var request map[string]interface{}
var response map[string]interface{}
json.Unmarshal([]byte(strings.Join(c.Args()[2:], " ")), &request)
req := client.NewJsonRequest(service, method, request)
err := client.Call(context.Background(), req, &response)
if err != nil {
fmt.Printf("error calling %s.%s: %v\n", service, method, err)
return
}
b, _ := json.MarshalIndent(response, "", "\t")
fmt.Println(string(b))
}
示例9: call
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",
})
// create context with metadata
ctx := c.WithMetadata(context.Background(), map[string]string{
"datacenter": "local",
})
rsp := &example.Response{}
// Call service
if err := client.Call(ctx, req, rsp); err != nil {
fmt.Println("call err: ", err, rsp)
return
}
fmt.Println("Call:", i, "rsp:", rsp.Msg)
}
示例10: restHandler
func restHandler(w http.ResponseWriter, r *http.Request) {
request, err := requestToProto(r)
if err != nil {
er := errors.InternalServerError("go.micro.api", err.Error())
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
w.Write([]byte(er.Error()))
return
}
service, method := pathToReceiver(r.URL.Path)
req := client.NewRequest(service, method, request)
rsp := &api.Response{}
if err := client.Call(context.Background(), req, rsp); err != nil {
w.Header().Set("Content-Type", "application/json")
ce := errors.Parse(err.Error())
switch ce.Code {
case 0:
w.WriteHeader(500)
default:
w.WriteHeader(int(ce.Code))
}
w.Write([]byte(ce.Error()))
return
}
for _, header := range rsp.GetHeader() {
for _, val := range header.Values {
w.Header().Add(header.Key, val)
}
}
if len(w.Header().Get("Content-Type")) == 0 {
w.Header().Set("Content-Type", "application/json")
}
w.WriteHeader(int(rsp.StatusCode))
w.Write([]byte(rsp.Body))
}
示例11: saveEntity
func saveEntity(id, typ string, lat, lon float64) {
entity := &common.Entity{
Id: id,
Type: typ,
Location: &common.Point{
Latitude: lat,
Longitude: lon,
Timestamp: time.Now().Unix(),
},
}
req := client.NewRequest("go.micro.srv.geo", "Location.Save", &loc.SaveRequest{
Entity: entity,
})
rsp := &loc.SaveResponse{}
if err := client.Call(context.Background(), req, rsp); err != nil {
fmt.Println(err)
return
}
}
示例12: Read
// Read API handler
func (es *Elastic) Read(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var readRequest *elastic.ReadRequest
if err = json.Unmarshal([]byte(req.Body), &readRequest); err != nil {
return errors.InternalServerError("go.micro.api.elastic", err.Error())
}
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Read",
readRequest,
)
srvRsp := &elastic.ReadResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = srvRsp.Result
return nil
}
示例13: main
func main() {
cmd.Init()
// Create new request to service go.micro.srv.greeter, method Say.Hello
req := client.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{
Name: "John",
})
// Set arbitrary headers in context
ctx := metadata.NewContext(context.Background(), map[string]string{
"X-User-Id": "john",
"X-From-Id": "script",
})
rsp := &hello.Response{}
// Call service
if err := client.Call(ctx, req, rsp); err != nil {
fmt.Println(err)
return
}
fmt.Println(rsp.Msg)
}
示例14: ServeHTTP
func (s *ApiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
defer r.Body.Close()
var service, method string
var request interface{}
// response content type
w.Header().Set("Content-Type", "application/json")
switch r.Header.Get("Content-Type") {
case "application/json":
b, err := ioutil.ReadAll(r.Body)
if err != nil {
e := errors.BadRequest("go.micro.api", err.Error())
w.WriteHeader(400)
w.Write([]byte(e.Error()))
return
}
var body map[string]interface{}
err = json.Unmarshal(b, &body)
if err != nil {
e := errors.BadRequest("go.micro.api", err.Error())
w.WriteHeader(400)
w.Write([]byte(e.Error()))
return
}
service = body["service"].(string)
method = body["method"].(string)
request = body["request"]
default:
r.ParseForm()
service = r.Form.Get("service")
method = r.Form.Get("method")
json.Unmarshal([]byte(r.Form.Get("request")), &request)
}
log.Infof("API Request: /rpc service: %s, method: %s", service, method)
var response map[string]interface{}
req := client.NewJsonRequest(service, method, request)
err := client.Call(context.Background(), req, &response)
if err != nil {
log.Errorf("Error calling %s.%s: %v", service, method, err)
ce := errors.Parse(err.Error())
switch ce.Code {
case 0:
w.WriteHeader(500)
default:
w.WriteHeader(int(ce.Code))
}
w.Write([]byte(ce.Error()))
return
}
b, _ := json.Marshal(response)
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Write(b)
}