一. 运行代码
client.go
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
pb "hello-grpc/proto/grpc_hello_service"
"log"
)
func main() {
conn ,err := grpc.Dial("localhost:1234",grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := pb.NewHelloServiceClient(conn)
reply,err := client.Hello(context.Background(), &pb.String{Value:"hello"})
if err != nil {
log.Fatal(err)
}
fmt.Println(reply.GetValue())
}
server.go:
package main
import (
"google.golang.org/grpc"
"log"
"net"
pb "hello-grpc/proto/grpc_hello_service"
"hello-grpc/service"
)
func main() {
grpcServer := grpc.NewServer()
pb.RegisterHelloServiceServer(grpcServer, &service.HelloServiceImpl{})
lis, err := net.Listen("tcp","localhost:1234")
if err != nil {
log.Fatal(err)
}
grpcServer.Serve(lis)
}
二. 运行结果
三. 参考文档
https://chai2010.cn/advanced-go-programming-book/ch4-rpc/ch4-01-rpc-intro.html
原文地址:https://cloud.tencent.com/developer/article/2137433
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。