GRPC-go版本

GRPC-go版本

1.安装GO,protobuf

只适合有梯子的

GO的安装没必要说了

protobuf :https://github.com/protocolbuffers/protobuf/releases

选合适的版本,将解压后bin目录的protoc.exe放到GO的安装目录的bin下(省事)

image-20210720224246244

打开CMD,输入protoc --version,正常如下图

image-20210720224324923

环境变量有三个需要设置

  1. GOPATH:是GO的工程目录

  2. GOBIN:生成的exe目录

  3. PATH:电脑环境变量

image-20210720223914345

2.安装相关包

2021-0721

好像要在grpcTest目录下,go mod init 之后进行安装,也有可能是我电脑问题,

1.golang 的proto工具包

go get -u github.com/golang/protobuf/proto

2.golang 的proto编译支持

go get -u github.com/golang/protobuf/protoc-gen-go

3.安装grpc包

go get -u google.golang.org/grpc

4.安装grpc-gen插件

go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc

安装正常的话,在GOPATH的bin目录下会有两个exe

image-20210720224723292

3.检验

去下载示例代码,放到GOPATH下,找到里面的helloworld

 git clone -b v1.35.0 https://github.com/grpc/grpc-go

在GOPATH下新建grpcTest,项目结构如下

image-20210720225028833

helloworld.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 go_package="." 表示生成的proto.go在当前目录
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;
}

生成.go文件

image-20210720225743460

client.go(官方) server和client一样,我这就只列举client

/*
 *
 * 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"
	--》改成 	pb "grpcTest/proto"
)

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())
}

然后去运行sever和client

image-20210720225902184

ok了

还是建议大家多去看官方的介绍,不要怕英文,有点底子的都能看懂

Quick start | Go | gRPC

出现protoc-gen-go' is not recognized as***********就是你没配置好环境变量,把GOPATH下的bin加到PATH里去

参考链接

转载请标明,谢谢
原文地址:https://www.cnblogs.com/guapilsh/p/15037521.html