You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
pro "gomicr/grpc"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 客户端 1,连接服务 2实例化grpc客户端 3发起请求/调用服务
|
|
|
|
|
func main() {
|
|
|
|
|
//1,连接服务
|
|
|
|
|
conn, err := grpc.Dial("127.0.0.1:8083", grpc.WithInsecure())
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("连接异常:", err)
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
//2实例化grpc客户端
|
|
|
|
|
client := pro.NewSearchServiceClient(conn)
|
|
|
|
|
//3发起请求/调用服务
|
|
|
|
|
req := new(pro.OrderRequest)
|
|
|
|
|
req.ActivityType = 1
|
|
|
|
|
resp, err1 := client.SearchOrder(context.Background(), req)
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
fmt.Println("响应异常err1:", err1)
|
|
|
|
|
} else {
|
|
|
|
|
data, err2 := json.Marshal(resp)
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
fmt.Println("响应异常err2:", err2)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("响应成功:", string(data))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|