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
    评论
您好!关于GolanggRPC环境,您可以按照以下步骤进行设置: 1. 安装Golang:首先,您需要在您的计算机上安装Golang。您可以从官方网站(https://golang.org/dl/)下载适合您操作系统的安装程序,并按照提示进行安装。 2. 安装gRPC:安装Golang之后,您可以使用以下命令通过Go模块管理器安装gRPC: ``` go get google.golang.org/grpc ``` 3. 安装protobuf:gRPC使用Protocol Buffers(简称为protobuf)作为其序列化和通信协议。您可以使用以下命令安装protobuf编译器: ``` go get google.golang.org/protobuf/cmd/protoc-gen-go ``` 4. 定义和生成gRPC服务:使用protobuf语言定义您的gRPC服务。首先,创建一个`.proto`文件,然后使用protobuf编译器生成Golang代码。示例: ```protobuf syntax = "proto3"; package helloworld; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } ``` 使用以下命令生成Golang代码: ``` protoc --go_out=. --go-grpc_out=. path/to/your_service.proto ``` 5. 实现gRPC服务:在生成的Golang代码中,您将找到自动生成的服务接口和实现。您可以在实现中添加您的自定义逻辑。 6. 构建和运行:使用Golang的构建工具(如`go build`)构建您的应用程序,并运行它。 这样,您就可以开始使用GolanggRPC环境了!希望对您有所帮助。如果您有任何其他问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值