1. GO安装/配置
1.1 安装
解压(官网二进制 https://golang.google.cn/dl/)# tar -C /usr/local/ -zxvf go1.17.linux-amd64.tar.gz(yum安装)# yum install golang.x86_64 (1.15.5-1.el7)环境变量# vim /etc/profileexport GOBIN=/usr/local/go/binexport GOPATH=/rootexport PATH=$PATH:/usr/local/go/bin# source /etc/profile# echo $GOPATH# echo $GOBIN样例# cd /root# vim helloworld.gopackage main import ( "fmt" ) func main() { fmt.Println( "Hello world!" ) }# go run helloworld.go
1.2 开启go module
1.11和1.12版本# GO111MODULE=on# GOPROXY=https://goproxy.io1.13版本之后 (需要注意的是这种方式并不会覆盖之前的配置,有点坑,你需要先把系统的环境变量里面的给删掉再设置)# go env -w GO111MODULE=on# go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct配置文件位置# vim /root/.config/go/envGO111MODULE=onGOPROXY=https://goproxy.cn,https://goproxy.io,direct
1.3 go get使用
更新# go get -u
1.4 go mod基本操作
初始化一个moudle,模块名为你项目名# go mod init 模块名删除错误或者不使用的modules# go mod tidy自动下载到 $GOPATH/pkg/mod
2. grpc-gateway配置
2.1 grpc-c
# git clone https://github.com/lixiangyun/grpc-c根据README.txt编译、运行
2.2 grpc-gateway
零 mod file# go mod init github.com/grpc-ecosystem/grpc-gateway① grpc service# cd test-grpc-gateway# vim proto/foo/foo.protooption go_package = "github.com/grpc-ecosystem/grpc-gateway/proto/foo";② grpc compile/stubsC/C++# cd grpc-c/examples/# ./build.shGo# protoc -I . \ --go_out . --go_opt paths=source_relative \ --go-grpc_out . --go-grpc_opt paths=source_relative \ proto/foo/foo.proto# ll proto/foo/foo_grpc.pb.go# ll proto/foo/foo.pb.go③ 生成反向代理(通过 protoc-gen-grpc-gateway)# cd test-grpc-gateway文件路径# mkdir -p proto/foo# vim proto/foo/config.yamltype: google.api.Serviceconfig_version: 3http: rules: - selector: foo.Greeter.SayHello post: /v1/example/sayhello body: "*"生成反向代理# protoc -I . --grpc-gateway_out . \ --grpc-gateway_opt logtostderr=true \ --grpc-gateway_opt paths=source_relative \ --grpc-gateway_opt grpc_api_configuration=proto/foo/config.yaml \ proto/foo/foo.proto非依赖情况增加选项:--grpc-gateway_opt standalone=true# ll proto/foo/foo.pb.gw.go④ 生成 swagger.json# protoc -I . --openapiv2_out . \ --openapiv2_opt grpc_api_configuration=proto/foo/config.yaml \ proto/foo/foo.proto# ll proto/foo/foo.swagger.json⑤ http反向代理package mainimport ( "context" "flag" "net/http" "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "google.golang.org/grpc" gw "github.com/grpc-ecosystem/grpc-gateway/proto/foo" // Update)var ( // command-line options: // gRPC server endpoint grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:3000", "gRPC server endpoint"))func run() error { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() // Register gRPC server endpoint // Note: Make sure the gRPC server is running properly and accessible mux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithInsecure()} err := gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts) if err != nil { return err } // Start HTTP server (and proxy calls to gRPC server endpoint) return http.ListenAndServe(":8090", mux)}func main() { flag.Parse() defer glog.Flush() if err := run(); err != nil { glog.Fatal(err) }}⑥ http测试gw启动# go run main.go# curl -X POST -k http://localhost:8090/v1/example/echo -d '{"name": " hello"}'
3. 参考文档