2

Welcome to my GitHub

Here is a classification and summary of all original (including supporting source code): 160bf38a17db5e https://github.com/zq2599/blog_demos

Links to gRPC learning series articles

  1. deploy and set up GO
  2. GO's gRPC development environment preparation
  3. first trial GO version gRPC development
  4. Actual combat four types of service methods
  5. gRPC-Gateway actual combat
  6. gRPC-Gateway integrated swagger

Overview of this article

  • This article is the second part of the "gRPC learning" series. In the previous article, GO is installed in the CentOS7 environment, and then the gRPC development environment must be prepared. In general, there are three steps:

在这里插入图片描述

Install protoc

  1. protoc is a compilation tool, the installation method is to download the binary file, just copy and paste the following command to execute:
mkdir -p $GOPATH/bin \
&& mkdir ~/temp-protoc-download \
&& wget https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/protoc-3.14.0-linux-x86_64.zip -O ~/temp-protoc-download/protoc.zip \
&& cd ~/temp-protoc-download \
&& unzip protoc.zip \
&& cp ./bin/protoc $GOPATH/bin/ \
&& cd ~/ \
&& rm -rf ~/temp-protoc-download
  1. Execute <font color="blue">protoc --version</font> to check whether the protoc installation is successful:
[golang@centos7 ~]$ protoc --version
libprotoc 3.14.0

Problems encountered in installing protoc-gen-go and grpc packages

  • It turns out that when using the <font color="blue">go get</font> command to install the protoc-gen-go and grpc packages, network errors are often prompted, so I wrote a shell script to combine protoc-gen-go with Download the source code of the grpc package from GitHub, compile and build it locally to achieve the same effect as the installation of <font color="blue">go get</font>;
  • Using the <font color="blue">git clone</font> command to download the source code is time-consuming (too many files), so the script I wrote is to download the corresponding source package (zip file), unzip it, and git clone The effect is the same but the time-consuming is reduced a lot;
  • Therefore, the next operation is a script to complete the installation of the protoc-gen-go and grpc packages;

    Install protoc-gen-go and grpc packages

  • Execute the following commands to complete the installation of the protoc-gen-go and grpc packages:
curl -o install-grpc.sh \
https://raw.githubusercontent.com/zq2599/blog_demos/master/files/install-grpc.sh \
&& chmod a+x ./install-grpc.sh \
&& ./install-grpc.sh
  1. The console outputs the following information without errors, indicating that the installation is successful:
...
install protoc-gen-go
go: downloading google.golang.org/protobuf v1.23.0
install grpc
clear resource
install finish
  1. Protoc-gen-go can be seen in the <font color="blue">$GOPATH/bin</font> directory:
[golang@centos7 ~]$ cd $GOPATH/bin
[golang@centos7 bin]$ ls
protoc  protoc-gen-go
    • At this point, the gRPC development environment has been prepared, and the next article can start the actual combat;

List of installation scripts

The installation process of the protoc-gen-go and grpc packages is completed in install-grpc.sh. The content of the script is as follows. It can be seen that they are all very simple operations: download the source code, unzip, and build

#!/bin/bash

mkdir ~/temp-grpc-install

echo "clear old files"
rm -rf $GOPATH/src/google.golang.org/grpc
rm -rf $GOPATH/src/golang.org/x
rm -rf $GOPATH/src/google.golang.org/protobuf
rm -rf $GOPATH/src/github.com/golang/protobuf
rm -rf $GOPATH/src/google.golang.org/genproto

echo "create directory"
mkdir -p $GOPATH/src/google.golang.org/
mkdir -p $GOPATH/src/golang.org/x
mkdir -p $GOPATH/src/github.com/golang/

echo "1. grpc"
cd ~/temp-grpc-install
wget https://github.com/grpc/grpc-go/archive/master.zip -O grpc-go.zip
unzip grpc-go.zip -d $GOPATH/src/google.golang.org/
cd $GOPATH/src/google.golang.org/ 
mv grpc-go-master grpc

echo "2. x/net"
cd ~/temp-grpc-install
wget https://github.com/golang/net/archive/master.zip -O net.zip
unzip net.zip -d $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/ 
mv net-master net

echo "3. x/text"
cd ~/temp-grpc-install
wget https://github.com/golang/text/archive/master.zip -O text.zip
unzip text.zip -d $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/ 
mv text-master text

echo "4. protobuf-go"
cd ~/temp-grpc-install
wget https://github.com/protocolbuffers/protobuf-go/archive/master.zip -O protobuf-go.zip
unzip protobuf-go.zip -d $GOPATH/src/google.golang.org/ 
cd $GOPATH/src/google.golang.org/
mv protobuf-go-master protobuf

echo "5. protobuf"
cd ~/temp-grpc-install
wget https://github.com/golang/protobuf/archive/master.zip -O protobuf.zip
unzip protobuf.zip -d $GOPATH/src/github.com/golang/
cd $GOPATH/src/github.com/golang/
mv protobuf-master protobuf

echo "6. go-genproto"
cd ~/temp-grpc-install
wget https://github.com/google/go-genproto/archive/master.zip -O go-genproto.zip
unzip go-genproto.zip -d $GOPATH/src/google.golang.org/
cd $GOPATH/src/google.golang.org/
mv go-genproto-master genproto

echo "7. x/sys"
cd ~/temp-grpc-install
wget https://github.com/golang/sys/archive/master.zip -O sys.zip
unzip sys.zip -d $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x
mv sys-master sys

echo "install protoc-gen-go"
cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go/
go build
go install

echo "install grpc"

cd $GOPATH/src/
go install google.golang.org/grpc

echo "clear resource"
cd ~/
rm -rf ~/temp-grpc-install

echo "install finish"

You are not alone, Xinchen and original are with you all the way

  1. Java series
  2. Spring series
  3. Docker series
  4. kubernetes series
  5. database + middleware series
  6. DevOps series

Welcome to pay attention to the public account: programmer Xin Chen

Search for "Programmer Xin Chen" on WeChat, I am Xin Chen, and I look forward to traveling the Java world with you...
https://github.com/zq2599/blog_demos

程序员欣宸
147 声望24 粉丝

热爱Java和Docker