1. GO安装/配置

1.1 安装

  1. 解压
  2. (官网二进制 https://golang.google.cn/dl/)
  3. # tar -C /usr/local/ -zxvf go1.17.linux-amd64.tar.gz
  4. (yum安装)
  5. # yum install golang.x86_64 (1.15.5-1.el7)
  6. 环境变量
  7. # vim /etc/profile
  8. export GOBIN=/usr/local/go/bin
  9. export GOPATH=/root
  10. export PATH=$PATH:/usr/local/go/bin
  11. # source /etc/profile
  12. # echo $GOPATH
  13. # echo $GOBIN
  14. 样例
  15. # cd /root
  16. # vim helloworld.go
  17. package main
  18. import ( "fmt" )
  19. func main()
  20. {
  21. fmt.Println( "Hello world!" )
  22. }
  23. # go run helloworld.go

1.2 开启go module

  1. 1.111.12版本
  2. # GO111MODULE=on
  3. # GOPROXY=https://goproxy.io
  4. 1.13版本之后 (需要注意的是这种方式并不会覆盖之前的配置,有点坑,你需要先把系统的环境变量里面的给删掉再设置)
  5. # go env -w GO111MODULE=on
  6. # go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct
  7. 配置文件位置
  8. # vim /root/.config/go/env
  9. GO111MODULE=on
  10. GOPROXY=https://goproxy.cn,https://goproxy.io,direct

1.3 go get使用

  1. 更新
  2. # go get -u

1.4 go mod基本操作

  1. 初始化一个moudle,模块名为你项目名
  2. # go mod init 模块名
  3. 删除错误或者不使用的modules
  4. # go mod tidy
  5. 自动下载到 $GOPATH/pkg/mod

2. grpc-gateway配置

2.1 grpc-c

  1. # git clone https://github.com/lixiangyun/grpc-c
  2. 根据README.txt编译、运行

2.2 grpc-gateway

  1. mod file
  2. # go mod init github.com/grpc-ecosystem/grpc-gateway
  3. grpc service
  4. # cd test-grpc-gateway
  5. # vim proto/foo/foo.proto
  6. option go_package = "github.com/grpc-ecosystem/grpc-gateway/proto/foo";
  7. grpc compile/stubs
  8. C/C++
  9. # cd grpc-c/examples/
  10. # ./build.sh
  11. Go
  12. # protoc -I . \
  13. --go_out . --go_opt paths=source_relative \
  14. --go-grpc_out . --go-grpc_opt paths=source_relative \
  15. proto/foo/foo.proto
  16. # ll proto/foo/foo_grpc.pb.go
  17. # ll proto/foo/foo.pb.go
  18. 生成反向代理(通过 protoc-gen-grpc-gateway
  19. # cd test-grpc-gateway
  20. 文件路径
  21. # mkdir -p proto/foo
  22. # vim proto/foo/config.yaml
  23. type: google.api.Service
  24. config_version: 3
  25. http:
  26. rules:
  27. - selector: foo.Greeter.SayHello
  28. post: /v1/example/sayhello
  29. body: "*"
  30. 生成反向代理
  31. # protoc -I . --grpc-gateway_out . \
  32. --grpc-gateway_opt logtostderr=true \
  33. --grpc-gateway_opt paths=source_relative \
  34. --grpc-gateway_opt grpc_api_configuration=proto/foo/config.yaml \
  35. proto/foo/foo.proto
  36. 非依赖情况增加选项:
  37. --grpc-gateway_opt standalone=true
  38. # ll proto/foo/foo.pb.gw.go
  39. 生成 swagger.json
  40. # protoc -I . --openapiv2_out . \
  41. --openapiv2_opt grpc_api_configuration=proto/foo/config.yaml \
  42. proto/foo/foo.proto
  43. # ll proto/foo/foo.swagger.json
  44. http反向代理
  45. package main
  46. import (
  47. "context"
  48. "flag"
  49. "net/http"
  50. "github.com/golang/glog"
  51. "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  52. "google.golang.org/grpc"
  53. gw "github.com/grpc-ecosystem/grpc-gateway/proto/foo" // Update
  54. )
  55. var (
  56. // command-line options:
  57. // gRPC server endpoint
  58. grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:3000", "gRPC server endpoint")
  59. )
  60. func run() error {
  61. ctx := context.Background()
  62. ctx, cancel := context.WithCancel(ctx)
  63. defer cancel()
  64. // Register gRPC server endpoint
  65. // Note: Make sure the gRPC server is running properly and accessible
  66. mux := runtime.NewServeMux()
  67. opts := []grpc.DialOption{grpc.WithInsecure()}
  68. err := gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
  69. if err != nil {
  70. return err
  71. }
  72. // Start HTTP server (and proxy calls to gRPC server endpoint)
  73. return http.ListenAndServe(":8090", mux)
  74. }
  75. func main() {
  76. flag.Parse()
  77. defer glog.Flush()
  78. if err := run(); err != nil {
  79. glog.Fatal(err)
  80. }
  81. }
  82. http测试
  83. gw启动
  84. # go run main.go
  85. # curl -X POST -k http://localhost:8090/v1/example/echo -d '{"name": " hello"}'

3. 参考文档