一 、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/shprotoc --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()