grpc linux 环境搭建,linux下golang gRPC配置详解

1.安装gRPC运行环境

go get google.golang.org/grpc

这里的grpc通俗来说就说用在代码里的一个类库,后面的例子可以看到。比较坑的是这里可能需要FQ.....

2.安装protoc

这里需要安装proto buffer的编译器。首先在官网下载,如c++版本的protobuf-cpp-3.4.1.tar.gz,解压后进行编译:

./configure

make && make install

3.安装protoc-gen-go

go get -a github.com/golang/protobuf/protoc-gen-go

4.编写proto文件

基于protobuf的跨语言的特性,不难想到它自己实现了一套数据类型。这里有一个简单的例子

//testHello.proto

syntax = "proto3";

package protos;

// The service definition.

service Devops {

// 定义服务

rpc SayHello (HelloRequest) returns (Response) {}

}

// The request message containing the user's name.

message HelloRequest {

string name = 1;

}

// The response message

message HelloReply {

string message = 1;

}

message Response {

enum StatusCode {

UNDEFINED = 0;

SUCCESS = 200;

FAILURE = 500;

}

StatusCode status = 1;

HelloReply msg = 2;

}

然后在终端自动生成pb.go:

protoc --go_out=plugins=grpc:. testHello.proto

5.编写服务端

package main

const (

port = ":50051"

)

type myserver struct{}

//这里myserver实现了SayHello

func (s *myserver) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.Response, error) {

fmt.Print("receive " + in.Name)

return &pb.Response{

Status:pb.Response_SUCCESS,

Msg:&pb.HelloReply{Message:"receive " + in.Name},

}, nil

}

func main() {

//绑定端口

lis, err := net.Listen("tcp", port)

if err != nil{

log.Fatal("fail to listen")

}

s := grpc.NewServer()

pb.RegisterDevopsServer(s, &myserver{})

s.Serve(lis)

}

6.编写客户端

package main

const (

address = "localhost:50051"

)

func main() {

//grpc.WithInsecure()指定后才不会报错

conn, err := grpc.Dial(address, grpc.WithInsecure())

if err != nil{

log.Fatal("error....", err)

}

c := pb.NewDevopsClient(conn)

res, _ := c.SayHello(context.Background(), &pb.HelloRequest{"eeee"})

fmt.Print(res)

}

WithInsecure returns a DialOption which disables transport security for this ClientConn.

Note that transport security is required unless WithInsecure is set.

WithInsecure返回一个DialOption,它在传输过程中不保证安全。除非设置WithInsecure,否则grpc.Dial必须指定安全选项。

参考:

1.https://github.com/google/protobuf/releases

2.http://www.cnblogs.com/YaoDD/p/5504881.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值