Windows下Go的grpc的使用
环境的搭建
1、windows下搭建go
2、设置Go的相关环境比如GOPATH
3、生成protoc.exe:根据https://github.com/protocolbuffers/protobuf/releases下载,然后将bin下设置到环境变量中
4、根据文章设置:protoc-gen-go.exe的生成,百度一下。然后将exe放到go的bin下
grpc的demo使用
1、在目录中创建google官方示例helloword.proto
// 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.
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;
}
执行命令:protoc --go_out=plugins=grpc:. helloworld.proto
他会根据go_package 生成helloworld.pb文件
2、创建server.go
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "src/google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
port = ":50051"
)
// 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() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
3、创建client.go
package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "src/google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
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.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
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())
}
4、执行
4-1先执行server:go run example/server/server.go
4-2再执行client:go run example/client/client.go
查看是否有内容输出,如果在步骤1生成不了pb文件的话根据提示进行安装,如果在执行server.go或者client.go报错的话一般是包没有导入进行来,根据缺失的包执行go get -u
[1]:https://blog.csdn.net/u013536232/article/details/105395172
[2]: https://www.php.cn/be/go/437390.html