• go的grpc环境源码编译安装


    go的grpc环境安装

    参考grpc-go官方文档:https://grpc.io/docs/languages/go/quickstart/
    视频教程:https://www.bilibili.com/video/BV1zi4y1L7Sf

    环境

    golang的docker image

    $ docker run -it golang:alpine
    

    简单准备

    # go env -w GO111MODULE=on
    //重要:添加代理,参考:https://goproxy.cn/
    # go env -w GOPROXY=https://goproxy.cn,direct
    //必要的一些工具
    # apk add git make gcc g++
    

    prorobuf安装

    下载protobuf-all-3.8.0.tar.gz

    # wget https://github.com/protocolbuffers/protobuf/releases/download/v3.8.0/protobuf-all-3.8.0.tar.gz
    Connecting to github.com (52.74.223.119:443)
    Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (52.217.76.180:443)
    saving to 'protobuf-all-3.8.0.tar.gz'
    protobuf-all-3.8.0.t  13% |************                                                                            |  968k  0:05:10 ETA
    

    解压并安装

    # tar -xvf protobuf-all-3.8.0.tar.gz 
    # cd protobuf-3.8.0
    protobuf-3.8.0# mkdir -p /usr/local/protobuf
    protobuf-3.8.0# ./configure --prefix=/usr/local/protobuf
    protobuf-3.8.0# make
    protobuf-3.8.0# make install
    
    protobuf-3.8.0# echo "export PATH=$PATH:/usr/local/protobuf/bin">>/etc/profile
    protobuf-3.8.0# source /etc/profile
    protobuf-3.8.0# protoc --version
    libprotoc 3.8.0
    

    prorobuf安装完成。

    protoc-gen-go安装

    protoc-gen-go是proto文件生成go文件的插件

    方法1:

    //网络原因,安装比较困难,添加GOPROXY后解决。
    go get -u github.com/golang/protobuf/protoc-gen-go
    

    方法2:源码安装

    mkdir -p $GOPATH/src/github.com/golang && cd $GOPATH/src/github.com/golang
    //大小8M,可以直接clone
    golang# git clone https://github.com/golang/protobuf.git
    Cloning into 'protobuf'...
    remote: Enumerating objects: 7251, done.
    remote: Total 7251 (delta 0), reused 0 (delta 0), pack-reused 7251
    Receiving objects: 100% (7251/7251), 8.43 MiB | 17.00 KiB/s, done.
    Resolving deltas: 100% (4783/4783), done.
    

    编译方法

    golang# cd protobuf/protoc-gen-go
    protoc-gen-go# go build -o protoc-gen-go main.go
    go: downloading google.golang.org/protobuf v1.23.0
    protoc-gen-go# ls
    protoc-gen-go        <<< 编译出可执行文件
    
    protoc-gen-go# echo "export PATH=$PATH:$GOPATH/bin" >> /etc/profile
    protoc-gen-go# source /etc/profile
    //复制protoc-gen-go到bin目录
    protoc-gen-go# cp protoc-gen-go /usr/local/go/bin
    protoc-gen-go# ls /usr/local/go/bin/
    go             gofmt          protoc-gen-go
    
    

    验证protoc-gen-go插件

    验证protoc-gen-go插件就是看是否能将proto文件生成pb.go文件。

    helloworld# ls
    helloworld.proto       
    helloworld# protoc --go_out=plugins=grpc:. helloworld.proto
    helloworld# ls google.golang.org/grpc/examples/helloworld/helloworld/
    helloworld.pb.go
    

    证明protoc-gen-go插件成功!

    安装grpc-go

    # mkdir -p $GOPATH/src/google.golang.org
    # cd $GOPATH/src/google.golang.org
    //下载源码包
    google.golang.org# wget https://github.com/grpc/grpc-go/archive/master.tar.gz
    Connecting to github.com (13.229.188.59:443)
    Connecting to codeload.github.com (54.251.140.56:443)
    saving to 'master.tar.gz'
    master.tar.gz        100% |****************************************************************************************| 1138k  0:00:00 ETA
    'master.tar.gz' saved
    //解压源码包
    google.golang.org# tar -xvf master.tar.gz 
    google.golang.org# mv grpc-go-master grpc
    //安装grpc-go
    google.golang.org# cd grpc
    grpc# go install google.golang.org/grpc
    go: downloading github.com/golang/protobuf v1.4.2
    go: downloading google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
    go: downloading golang.org/x/net v0.0.0-20190311183353-d8887717615a
    go: downloading golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
    go: downloading golang.org/x/text v0.3.0
    

    grpc-go安装完成。

    验证

    找到安装grpc-go时已经下载好的示例代码。

    服务端启动

    $ docker ps | grep golang
    a450ff530be6        golang              "/bin/sh -c sh"     About an hour ago   Up About an hour                        elastic_ptolemy
    $ docker exec -it a450ff530be6 sh
    
    # cd /go/src/google.golang.org/grpc/examples/helloworld
    helloworld # ls
    greeter_client  greeter_server  helloworld
    
    helloworld# go run greeter_server/main.go
    2021/01/17 00:53:56 Received: world
    
    

    客户端启动

    $ docker ps | grep golang
    a450ff530be6        golang              "/bin/sh -c sh"     About an hour ago   Up About an hour                        elastic_ptolemy
    $ docker exec -it a450ff530be6 sh
    
    # cd /go/src/google.golang.org/grpc/examples/helloworld
    helloworld # ls
    greeter_client  greeter_server  helloworld
    
    helloworld # go run greeter_client/main.go
    2021/01/17 00:53:56 Greeting: Hello world
    

    grpc通信成功!

  • 相关阅读:
    实验4 IIC通讯与EEPROM接口
    实验3 串口通信
    实验2 中断和定时计数器实验
    实验1 单片机IO口应用及数码管显示
    央行大小额支付系统
    银行各交易渠道的清算方式
    ATM跨行取款的清算方式
    POS机刷卡跨行交易的清算方式
    商业银行在CNAPS体系中对各种交易的处理
    支付相关名词解释
  • 原文地址:https://www.cnblogs.com/abc36725612/p/14288333.html
Copyright © 2020-2023  润新知