go使用gRPC框架
官方文档
https://grpc.io/docs/languages/go/quickstart/
官方example
https://github.com/grpc/grpc-go
1.定义proto文件
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
proto定义,参考官方文档
https://github.com/grpc/grpc-go/blob/master/examples/helloworld/helloworld/helloworld.proto
2.编译proto文件
使用protoc命令编译
需要先安装compile插件,参考:https://grpc.io/docs/languages/go/quickstart/
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
编译命令
protoc --go_out=./ --go_opt=paths=source_relative --go-grpc_out=./cmd/proto/helloworld --go-grpc_opt=paths=source_relative ./cmd/proto//helloworld/helloworld.proto
编译得到go代码

3.编写server和client代码
1.server代码
官方例子
https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_server/main.go
代码
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
pb "awesome-project/cmd/proto/helloworld"
"google.golang.org/grpc"
)
var (
port = flag.Int("port", 50051, "The server port")
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
2.client代码
官方例子
https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_client/main.go
代码
package main
import (
"context"
"flag"
"log"
"time"
pb "awesome-project/cmd/proto/helloworld"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
defaultName = "world"
)
var (
addr = flag.String("addr", "localhost:50051", "the address to connect to")
name = flag.String("name", defaultName, "Name to greet")
)
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}
3.运行server和client
成功看到hello world

本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5558387.html

浙公网安备 33010602011771号