https://grpc.io/
https://grpc.io/docs/guides/
https://grpc.io/docs/tutorials/basic/go/
1.下载
go get -u -v google.golang.org/grpc/...
因为在go的环境变量中配置了go mod,所以要注意,且在此处下载必须是带【/…】,否则容易报莫名的异常!
2.测试
1.服务端启动
1.找到下载的grpc所在的目录【使用go mod的环境变量一般在$GOPATH/pkg下面】
cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_server && ls
2.启动server服务
cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_server && go run main.go
2.客户端启动
另开一个终端
1.找到下载的grpc所在的目录【使用go mod的环境变量一般在$GOPATH/pkg下面】
cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_client && ls
2.启动client服务
cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_client && go run main.go
环境搭建成功!!!
3.客户端代码浅析
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package main implements a client for Greeter service.
package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld" // 引用编译好的protobuf文件
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// 建立与服务器的连接
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
// 延迟关闭连接
defer conn.Close()
// 调用protobuf的函数创建客户端连接句柄
c := pb.NewGreeterClient(conn)
// 与服务器联系并打印其响应。
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
// context的超时设置
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// 调用protobuf的SayHello函数
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())
}
4.服务端代码浅析
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
//go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto
// Package main implements a server for Greeter service.
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld" // 引用编译好的protobuf文件
)
const (
port = ":50051"
)
// 服务器用于实现helloworld.GreeterServer。
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello实现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() {
// 创建监听
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// new服务对象
s := grpc.NewServer()
// 注册服务
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}