55

关于麻烦的grpc环境的搭建问题

 5 years ago
source link: https://studygolang.com/articles/16773?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

个人觉得grpc的环境搭建还是比较麻烦的,不仅因为网络的原因,还因为各种系统的原因,就可能造成下载不成功,编译不通过。而且,换个工作环境,一切又要从头开始,家里的电脑要搭建一套,公司的电脑要搭建一套,正是让人烦躁。尤其当新加入一个项目时,想要快速搭建好环境,赶紧熟悉项目和代码时,搭建环境实在是令人头疼。因此我比较倾向于搭建一个docker镜像,以后走到哪都可以直接拉个镜像就开工,虽然可能看起来会比较牛刀小用,但是我觉得还是值得的。写这篇文章的目的在于记录这个镜像怎么build以及怎么在实际开发中使用。

第一步,安装protobuf

安装protobuf之前,要先安装git、unzip、build-essential、autoconf、libtool这些下载和编译工具,安装完成之后再在 https://github.com/google/protobuf.git 下载protobuf,进行编译。网上有相应的编译教程,这里就不再赘述。有些坑要说明一下,比如在我的Dockerfile里面,我是先设置了git config --global http.postBuffer 1048576000,以及clone的时候设置了--depth=1,如此是为了防止用Dockerfile build镜像的时候,因为git下载失败而失败。后面会附上我的Dockerfile。

第二步,下载grpc相关的包和proto-gen-go

这些包是用于将proto文件编译成go文件用的,同样相关的下载在网上的其他文章和我的Dockerfile也会提到,所以不再多讲。前两步是说一下这个镜像里面到底有什么东西,不关心有什么东西只想用镜像把proto文件编译成go文件的话,应该直接从第三步开始。

第三步,docker pull镜像或者Dockerfile生成镜像

直接pull镜像可以免去重新下载、编译proto和go相关包的时间。这里使用的是阿里云的镜像仓库。相关操作指南请搜索阿里云docker镜像操作指南或者登录阿里云镜像仓库会有相应的操作提示,我的镜像仓库地址为:registry.cn-hangzhou.aliyuncs.com/linhuanchao/grpc。

第四步,启动容器并挂载项目目录

这里我是使用docker-compose。我的docker-compose.yml十分简单,因为没有跑什么服务,仅仅只是把它当做一个编译工具来使用。

version: '2'
services:
  grpc:
    image: grpc/golang:v1
    container_name: grpc
    volumes:
      - /home/tomato/go/src/grpc-test:/go/src/grpc-test
    tty: "true"

第五步,使用容器编译proto

这里会遇到一个问题,就是我并不是跑到容器里面编写项目的代码,所以我宿主机的$GOPATH和容器里的$GOPATH并不相同,因而在宿主机编译go代码时,有些package可能会缺失,我的做法是先将容器里面$GOPATH的包复制一遍出来,容器只管编译proto文件,宿主机只管编译go文件,在宿主机拥有容器里面的所有package之后就不再需要里面容器里面的$GOPATH的package了。接下来是我使用的一个示例:

sudo docker exec -it grpc /bin/bash -c " mkdir /go/src/grpc-test/helloworld && cd /go/src/grpc-test/proto && protoc --go_out=plugins=grpc:../helloworld helloworld.proto "

我往docker容器里面运行了一个编译proto的命令。因为已经将项目挂载在了容器里面,所以,接下来我可以直接在宿主机里面编译go文件生成二进制文件了。

附,Dockerfile:

# Dockerfile for gRPC Go
FROM golang:1.10
# install protobuf from source
RUN apt-get update && \
    apt-get -y install git unzip build-essential autoconf libtool && \
    git config --global http.postBuffer 1048576000
RUN git clone https://github.com/google/protobuf.git --depth 1 && \
    cd protobuf && \
    ./autogen.sh && \
    ./configure && \
    make && \
    make install && \
    ldconfig && \
    make clean && \
    cd .. && \
    rm -r protobuf
# Get the source from GitHub
RUN git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
RUN git clone https://github.com/golang/net.git --depth 1 $GOPATH/src/golang.org/x/net
RUN git clone https://github.com/golang/text.git --depth 1 $GOPATH/src/golang.org/x/text
RUN go get -u github.com/golang/protobuf/proto
RUN git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
RUN git clone https://github.com/golang/sys.git $GOPATH/src/golang.org/x/sys
RUN cd $GOPATH/src/ && \
    go install google.golang.org/grpc
# Install protoc-gen-go
RUN go get github.com/golang/protobuf/protoc-gen-go

注:这里我写了很多个RUN,目的是防止下载失败,而重新build Dockerfile的时候要重新再来。如果是写多个RUN的话,重新build的时候,docker可以从缓存中取出前面的RUN好的步骤,从失败的步骤开始build。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK