golang的Grpc环境搭建
安装protobuf
使用brew进行安装。
brew info protobuf
brew install protobuf
检验protobuf安装结果
protoc --version
libprotoc 3.17.3
安装第三方包
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
测试
终端打开一个临时目录,新建helloworld.proto文件,将下面的内容 复制进去。
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
保存退出后,在当前目录下使用下面的命令看是否可以正常生成两个文件。
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
./helloworld.proto
huzd@huzddeMacBook-Pro test % protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
./helloworld.proto
huzd@huzddeMacBook-Pro test % ls
helloworld.pb.go helloworld.proto helloworld_grpc.pb.go
huzd@huzddeMacBook-Pro test %
出现上面所示结果说明功能正常。
c语言的Grpc环境搭建
Protocol Buffer原生没有对C的支持,只能使用protobuf-c这个第三方库
在上面可以发现在使用protoc生成go代码时使用的是protoc --go_out,但是因为原生不支持c,所以借助protobuf-c实现c代码的生成,即这里的指令变为protoc-c --c_out
protobuf-c安装
在mac环境下可以直接借助brew实现安装
brew search protoc-c
brew install protoc-c
结果
huzd@huzddeMacBook-Pro test % brew search protobuf-c
==> Formulae
protobuf-c protobuf ✔ protobuf@3.6
huzd@huzddeMacBook-Pro test % brew install protobuf-c
Updating Homebrew...
==> Downloading https://ghcr.io/v2/homebrew/core/protobuf-c/manifests/1.4.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/protobuf-c/blobs/sha256:a79fd80
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring protobuf-c--1.4.0.big_sur.bottle.tar.gz
🍺 /usr/local/Cellar/protobuf-c/1.4.0: 15 files, 388.6KB
huzd@huzddeMacBook-Pro test %
测试
依然使用上面新建的proto文件,
protoc-c --c_out=. helloworld.proto
此时发现已经生成对应的c文件和h头文件。
huzd@huzddeMacBook-Pro test % ls
helloworld.pb-c.c helloworld.pb.go helloworld_grpc.pb.go
helloworld.pb-c.h helloworld.proto
至此,基本环境已经搭建完成。