Grpc环境搭建

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

至此,基本环境已经搭建完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值