grpc node 与golang通信

本文介绍了如何使用gRPC实现Node.js客户端与Golang服务端之间的通信。主要内容包括Golang环境的设置,如protobuf安装、protoc-gen-go插件安装以及grpc-go库的获取。详细阐述了.proto文件的编写,编译过程以及gRPC服务的实现。同时,还涵盖了Node.js环境中grpc库的安装和使用,展示了服务端与客户端的简单交互示例。
摘要由CSDN通过智能技术生成

一 、golang 设置

1     安装protobuf

go get github.com/google/protobuf

sudo apt-get install autoconf automake  libtool

./autogen.sh

./configure

make

make install

2     安装插件protoc-gen-go

go get -ugithub.com/golang/protobuf/protoc-gen-go

3     grpc-go

go get -u google.golang.org/grpc    //--需要翻墙


二、 protot文件

2.1 rminfo.proto

syntax = "proto3";
package rminfo;

message RmCmdRequest{
string rm_cmd = 1;
}
message RmCmdReply {
string rm_cmd_repl = 1;
}
service RmRoute {
rpc FetchInfo(RmCmdRequest) returns (RmCmdReply) {}

}

2.2 编译

#!/bin/sh

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

编译后是:rminfo.pb.go

三、grpcutils代码

package grpcutils
import (
"github.com/wanchain/go-wanchain/log"
"time"
"encoding/json"
"golang.org/x/net/context"
"google.golang.org/grpc"
"rminfo"
//"errors"
"fmt"
//"strings"
)
var (
Address = "localhost:50051"
)



type HeartBeatReply struct {
Timestamp string `json:"timestamp"`
UpdateFlag string `json:"updateflag"`
}
type CmdRequest struct{
Cmd int `json:"cmd"`
Input string `json:"input"`
}
type CmdReply struct{
Cmd string `json:"cmd"`
Output string`json:"output"`
}


type HeartBeatCmdReply struct {
Cmd string `json:"cmd"`
Hbr HeartBeatReply `json:"output"`
}
func FetchRemoteNetInfo(request string) (string, error){
conn, err := grpc.Dial(Address, grpc.WithInsecure())
        if  err != nil {
log.Error("did not connect: %v", err)
return "",err
}
defer conn.Close()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值