本机环境

简要概述

通过在本机安装 go、protoc、protoc-gen-go、protoc-gen-go-grpc、protoc-gen-grpc-gateway、protoc-gen-openapiv2 等编译依赖的组件。

目前推荐使用本地环境进行应用模版创建、编译。

基础环境

go 版本安装

版本必须大于等于1.18.x,版本检查:

go version

centos 7 安装方式:

yum install -y epel-release.noarch
yum install -y golang.x86_64

macOS 安装方式:

brew install go

设置全局变量

应用运行均依赖 “GOPATH” 与 “GO111MODULE”,所以必须确保正确设置了该变量。

# 开启 go mod
export GO111MODULE=on

# 根据实际情况是否需要设置 proxy
export GOPROXY="https://goproxy.cn"

# GOPATH 仅做示例,根据实际情况更改
export GOPATH=$HOME/go

# 添加 $GOPATH/bin 目录下二进制文件可被 shell 查询
export PATH=$PATH:$HOME/bin:$GOPATH/bin

请确保以上变量在系统全局生效,可以写入 “"$HOME/.bash_profile” 或 “$HOME/.zshrc” 等。

安装 grpc-kit-cli

https://github.com/grpc-kit/cli/releases 选择最新版本,根据目标系统架构下载。

目前支持以下架构:

  1. macOS intel x86_64
  2. macOS apple m
  3. linux x86_64
  4. linux arm64

安装 protoc 插件

手工安装 protoc

或者通过通过-make-下载依赖

使用 protoc 版本 v21.12,根据不同系统选择下载地址:

https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-x86_64.zip
https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-aarch_64.zip
https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-osx-aarch_64.zip
https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-osx-x86_64.zip

Linux x86_64 系统安装 protoc 选择,示例:

cd /tmp
curl -L -O 'https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-x86_64.zip'

unzip protoc-21.12-linux-x86_64.zip

# 相关二进制存放 GOPATH/bin 目录下
mv bin/protoc $GOPATH/bin/

# 对 protoc 公知类型移动至 /usr/local/include 目录下
mv include/google /usr/local/include/

# 清理移除垃圾数据
rmdir bin/ include/

特别的,对于 macOS apple 芯片选择 v21.12 的 aarch 架构版本,如:

protoc-3.19.3-linux-aarch_64.zip
curl -L -O 'https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-osx-aarch_64.zip'

手工安装 protoc-gen-X

或者通过通过-make-下载依赖

protoc-gen-go、protoc-gen-go-grpc、protoc-gen-grpc-gateway、protoc-gen-openapiv2 各版本安装,会自动拷贝二进制至 “$GOPATH/bin” 下面:

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.15.2
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v2.15.2

手工安装依赖的 proto 文件

或者通过通过-make-下载依赖

git clone -b v0.3.0 --depth 1 https://github.com/grpc-kit/api.git $GOPATH/src/github.com/grpc-kit/api
git clone --depth 1 https://github.com/googleapis/googleapis.git $GOPATH/src/github.com/googleapis/googleapis
git clone -b v2.15.2 --depth 1 https://github.com/grpc-ecosystem/grpc-gateway.git $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway

手工安装 grpc-kit-cli 二进制

可通过以下两种方式进行

  • 下载二进制安装
https://github.com/grpc-kit/cli/releases
  • 源码编译安装
git clone https://github.com/grpc-kit/cli.git

make build
cp ./build/grpc-kit-cli-* /usr/local/bin/grpc-kit-cli

生成应用的模版

生成代码模版

mkdir -p $GOPATH/src/github.com/opsaid
cd $GOPATH/src/github.com/opsaid

./grpc-kit-cli new -t service -o grpc-kit -p opsaid -s test1 --git-domain github.com

通过 make 下载依赖

make protoc
make protoc-gen-go
make protoc-gen-go-grpc
make protoc-gen-grpc-gateway
make protoc-gen-openapiv2

取保 $GOPATH/bin 目录在 $PATH 里面。

服务访问测试

运行微服务代码

make run

微服务接口文档

http://127.0.0.1:8080/openapi-spec/

微服务编译版本

# curl http://127.0.0.1:8080/version | python -m json.tool

{
    "appname": "test1.v1.opsaid",
    "build_date": "2023-01-13T09:10:45Z",
    "git_commit": "1234567890123456789012345678901234567890",
    "git_branch": "",
    "go_version": "go1.18.5",
    "compiler": "gc",
    "platform": "darwin/amd64",
    "cli_version": "0.2.3",
    "commit_unix_time": 0,
    "release_version": "0.1.0"
}

微服务性能数据

# curl http://127.0.0.1:8080/metrics
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.000114581
go_gc_duration_seconds{quantile="0.25"} 0.000873528
go_gc_duration_seconds{quantile="0.5"} 0.002296699
go_gc_duration_seconds{quantile="0.75"} 0.003722618
go_gc_duration_seconds{quantile="1"} 0.010592338
go_gc_duration_seconds_sum 0.033207328
go_gc_duration_seconds_count 12
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 19

...

微服务健康探测

探测流量仅到 gateway 不会调度到 grpc 服务。

# curl http://127.0.0.1:8080/ping
OK

探测流量同时到 gateway 与 grpc 服务。

# curl 'http://127.0.0.1:8080/healthz?service=test1.v1.opsaid'
{"status":"SERVING"}

示例 demo 接口

# curl -u user1:grpc-kit-cli http://127.0.0.1:8080/api/demo