# proto-demo **Repository Path**: liuw5367/protodemo ## Basic Information - **Project Name**: proto-demo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-05-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Protocol Buffer ## proto文件生成swagger.json 1. 电脑安装protocol buffer https://github.com/protocolbuffers/protobuf/releases - 注意maven中依赖的protobuf版本要和 机器上装的protoc版本一致 2. 安装 go语言 配置go的环境变量 ``` export GOROOT=/usr/local/Cellar/go/1.12.4/libexec export GOPATH=/Users/xxxx/develop/GOPATH export GOBIN=$GOPATH/bin export PATH=$PATH:$GOBIN:$GOROOT/bin ``` 3. go 需要安装的库 https://github.com/grpc-ecosystem/grpc-gateway ```shell # 查看环境变量 go env # 代理 export GO111MODULE=on export GOPROXY=https://mirrors.aliyun.com/goproxy/ # 安装工具 需配置$GOPATH/bin环境变量 go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/grpc-ecosystem/grpc-gateway go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway # 查看生成swagger.json支持的参数 protoc-gen-swagger -h ``` 4. 生成swagger.json ```shell # 在proto目录下运行 protoc -I/usr/local/include -I. \ -I$GOPATH/src \ -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ --swagger_out=logtostderr=true,json_names_for_fields=true:. \ app/service.proto # windows protoc -I. \ -I$GOPATH/src \ -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ --swagger_out=logtostderr=true,json_names_for_fields=true:. \ app/service.proto ``` ## 使用swagger.json生成各语言的API代码 https://github.com/swagger-api/swagger-codegen ``` # 生成代码java端的api代码 java -jar swagger-codegen-cli.jar generate \ -i service.swagger.json \ -l java \ -c config.json \ -o ./api ``` 查看swagger-codegen-cli支持的java语言的所有参数 ``` java -jar swagger-codegen-cli.jar config-help -l java ``` Retrofit支持protobuf ``` compile 'com.squareup.retrofit2:converter-protobuf:version' ``` ``` go get -v -u github.com/gpmgo/gopm 1. gopm get -g github.com/google/go-genproto 2. 复制到 ${GOPATH}/src/google.golang.org/genproto 目录 cp go-genproto ${GOPATH}/src/google.golang.org/genproto 3. gopm get -g github.com/golang/protobuf/protoc-gen-go go install $GOPATH/src/github.com/golang/protobuf/protoc-gen-go 4. gopm get -g github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger go install $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger 5. gopm get -g github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go install $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway ```