Home >Backend Development >Golang >Detailed explanation of Windows10+golang+gRPC environment construction

Detailed explanation of Windows10+golang+gRPC environment construction

藏色散人
藏色散人forward
2021-03-15 17:08:173908browse

The following tutorial column of golang will introduce you to the Windows10 golang gRPC environment construction. I hope it will be helpful to friends in need!

Detailed explanation of Windows10+golang+gRPC environment construction

1. Install protoc

Download address: https: //github.com/protocolbuffers/protobuf/release
(Note: https://github.com/protocolbuffers/protobuf is its source code library, you can learn from it. If the source code library download is too slow, you can search it on Code Cloud. Many synchronized libraries are from domestic sources, and the download speed is relatively fast. Of course, you can also create a synchronized library yourself on the code cloud)

The latest version is 3.12.2
Mine is windows10 64-bit operating system, so choose the version: protoc-3.12.2-win64.zip
You can download it directly with the browser
If the Internet speed is not enough, you can also use Thunder to download: https://github.com/protocolbuffers/ protobuf/releases/download/v3.12.2/protoc-3.12.2-win64.zip
After decompression, copy protoc.exe to the $GOPATH/bin directory
If there are multiple GOPATHs, place them in the public In the GOPATH of the third-party library, multiple projects can use it

2. Install gRPC

gRPC source code: https://github.com/grpc/grpc-go.git
The installation method given by the official website is: go get -u google.golang.org/grpc
But the following error often occurs in China:

$ go get -u google.golang.org/grpc
package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

Because google.golang.org is difficult to access in China , so the download will fail.
The official website also provides multiple solutions:
https://github.com/grpc/grpc-go
We use the second method to directly clone the source code to the local
Enter $ In the GOPATH/src directory, execute the command:

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc

The download speed is sometimes fast, sometimes slow. When it is very slow, you can cancel and re-trigger. Try a few times and it will occasionally be faster.
After the download is completed, install gRPC:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ go install google.golang.org/grpc/
google.golang.org\grpc\credentials\credentials.go:31:2: cannot find package "github.com/golang/protobuf/proto" in any of:
        D:\Go\src\github.com\golang\protobuf\proto (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\proto (from $GOPATH)
google.golang.org\grpc\internal\binarylog\method_logger.go:28:2: cannot find package "github.com/golang/protobuf/ptypes" in any of:
        D:\Go\src\github.com\golang\protobuf\ptypes (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes (from $GOPATH)
google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:9:2: cannot find package "github.com/golang/protobuf/ptypes/duration" in any of:
        D:\Go\src\github.com\golang\protobuf\ptypes\duration (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\duration (from $GOPATH)
google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:10:2: cannot find package "github.com/golang/protobuf/ptypes/timestamp" in any of:
        D:\Go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOROOT)
        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOPATH)
google.golang.org\grpc\internal\transport\controlbuf.go:28:2: cannot find package "golang.org/x/net/http2" in any of:
        D:\Go\src\golang.org\x\net\http2 (from $GOROOT)
        C:\Users\ASUS\go\src\golang.org\x\net\http2 (from $GOPATH)
google.golang.org\grpc\internal\transport\controlbuf.go:29:2: cannot find package "golang.org/x/net/http2/hpack" in any of:
        D:\Go\src\golang.org\x\net\http2\hpack (from $GOROOT)
        C:\Users\ASUS\go\src\golang.org\x\net\http2\hpack (from $GOPATH)
google.golang.org\grpc\server.go:36:2: cannot find package "golang.org/x/net/trace" in any of:
        D:\Go\src\golang.org\x\net\trace (from $GOROOT)
        C:\Users\ASUS\go\src\golang.org\x\net\trace (from $GOPATH)
google.golang.org\grpc\status\status.go:34:2: cannot find package "google.golang.org/genproto/googleapis/rpc/status" in any of:
        D:\Go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOROOT)
        C:\Users\ASUS\go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOPATH)

You can find that there will be many errors. According to the prompts, you can find that it is due to the lack of packages. I will not analyze the error information bit by bit here, but directly give the required Dependency packages and download methods (execute the command in the $GOPATH/src directory):
1) text package

git clone https://github.com/golang/text.git ./golang.org/x/text

2) net package

git clone https://github.com/golang/net.git ./golang.org/x/net

3) genproto package

git clone https://github.com/google/go-genproto.git ./google.golang.org/genproto

4) Two protobuf packages
:

git clone https://github.com/protocolbuffers/protobuf-go.git ./google.golang.org/protobuf
git clone https://github.com/golang/protobuf.git ./github.com/golang/protobuf

must be downloaded. Some of the codes of github.com/golang/protobuf depend on google.golang.org/protobuf

After downloading all the above dependent libraries, reinstall gRPC:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ go install google.golang.org/grpc/

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$

It can be seen that there are no errors and no output. . .

Verify whether gRPC is OK
Open two bash windows and execute the following instructions respectively:

go run google.golang.org/grpc/examples/helloworld/greeter_server/main.go
go run google.golang.org/grpc/examples/helloworld/greeter_client/main.go

Detailed explanation of Windows10+golang+gRPC environment construction

As shown in the figure, you can see that the server has received the client The message sent by the client

3. Compile rpc

For the written proto file, it needs to be compiled to become a .go file, for example:
$GOPATHgoogle.golang.orggrpcexampleshelloworldhelloworld directory There are the following files:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ ll
total 16
-rw-r--r-- 1 ASUS 197121 4938 6月   1 21:46 helloworld.pb.go
-rw-r--r-- 1 ASUS 197121 1208 6月   1 21:46 helloworld.proto
-rw-r--r-- 1 ASUS 197121 2823 6月   1 21:46 helloworld_grpc.pb.go

Let’s back up the .go file first

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ mv helloworld.pb.go helloworld.pb.go.bak

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ mv helloworld_grpc.pb.go helloworld_grpc.pb.go.bak

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ ll
total 16
-rw-r--r-- 1 ASUS 197121 4938 6月   1 21:46 helloworld.pb.go.bak
-rw-r--r-- 1 ASUS 197121 1208 6月   1 21:46 helloworld.proto
-rw-r--r-- 1 ASUS 197121 2823 6月   1 21:46 helloworld_grpc.pb.go.bak

and then execute:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ protoc --go_out=plugins=grpc:. helloworld.proto
'protoc-gen-go' ????????????????????????е????
?????????????
--go_out: protoc-gen-go: Plugin failed with status code 1.

Found an error and need to install protoc-gen-go
Execute the following command to install:

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ go install github.com/golang/protobuf/protoc-gen-go/

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src
$ cd ../bin

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/bin
$ ll
total 11852
-rwxr-xr-x 1 ASUS 197121 3702272 5月  27 07:06 protoc.exe*
-rwxr-xr-x 1 ASUS 197121 8431104 6月   1 23:13 protoc-gen-go.exe*

After the installation is completed, the protoc-gen-go.exe file will be generated in the $GOPATH/bin directory
Then execute the compiled proto file:
A helloworld will be generated .pb.go file

Summary

There are several libraries that need to be understood:

1, https://github.com/protocolbuffers/protobuf
This is Google's open source protobuf source code library. This library contains the source code for implementing protobuf in various commonly used languages.

2, https://github.com/golang/protobuf
This library is from golang protobuf open source library, check the README.md of this library and you can find that this library has been replaced by google.golang.org/protobuf. Click this link and you can find that the source code git repository corresponding to this library is:
https://github.com/protocolbuffers/protobuf-go

Question:

Currently looking at the open source community, github’s protobuf will gradually be replaced by the google.golang.org library, but The current plug-in still has to use github's protoc-gen-go,
If you use google.golang.org/protobuf/cmd/protoc-gen-go (that is, go install google.golang.org/protobuf/cmd/protoc- gen-go, this will also generate protoc-gen-go.exe in the $GOPATH/bin directory), which will cause the following error

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ protoc --go_out=plugins=grpc:. helloworld.proto
--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)
$ protoc --go-grpc_out=. helloworld.proto
'protoc-gen-go-grpc' ????????????????????????е????
?????????????
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.

There will be a separate protoc-gen-go-grpc later to generate grpc interface, but this is still in the code review stage and is to be released. . .

The above is the detailed content of Detailed explanation of Windows10+golang+gRPC environment construction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete