go get github.com/google/protobuf
sudo apt-get install autoconf automake libtool
./autogen.sh
./configure
make
make install
go get -ugithub.com/golang/protobuf/protoc-gen-go
go get -u google.golang.org/grpc //--需要
syntax = "proto3";
package rminfo;
message RmCmdRequest{
string rm_cmd = 1;
}
message RmCmdReply {
string rm_cmd_repl = 1;
}
service RmRoute {
rpc FetchInfo(RmCmdRequest) returns (RmCmdReply) {}
}
protoc --go_out=plugins=grpc:. rminfo.proto
编译后是:rminfo.pb.go
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()
c:=rminfo.NewRmRouteClient(conn)
ctx, cancle := context.WithTimeout(context.Background(),time.Second)
defer cancle()
r, err := c.FetchInfo(ctx,&rminfo.RmCmdRequest{RmCmd:request})
if err != nil {
log.Error("could not get fetchInfo",err)
}
//log.Printf("reply is ",r.RmCmdRepl)
return r.RmCmdRepl, err
}
func FetchNetInfo(cmd int, input string ) (string,error){
//rcReply:= CmdReply{}
rcRequest:=CmdRequest{}
rcRequest.Cmd=cmd
rcRequest.Input=input
strRequst, err := json.Marshal(rcRequest)
if err != nil {
log.Error("fail to json.Marshal")
return "", err
}
strRepl, err := FetchRemoteNetInfo(string(strRequst))
fmt.Println("strRepl ", strRepl )
if err != nil {
log.Error("fail to FetchRemoteNetInfo")
return "", err
}
fmt.Println("in FetchRmNetInfo ---------------------- 1.1", strRepl)
start := strings.Index(strRepl, "output")
if start == -1 {
return "", errors.New("in Reple, no output ")
}
strHeartBeatRepl := strRepl[start+8:len(strRepl)-1]
return strHeartBeatRepl, nil
}
func JudgeTranByRuleServ(json_str string)(string , error){
strReply , err := FetchNetInfo(2,json_str)
if err != nil {
return "", err
}else{
return strReply, nil
}
}
func LoadContract(bUpdate bool )(string, error){
strReply, err := FetchNetInfo(1,"")
if err != nil {
return "", err
}else {
return strReply, nil
}
}
func RmHeartBeat()(string, error ){
strReply, err := FetchNetInfo(0,"")
if err != nil {
return "", err
}else {
return strReply, nil
}
}
package main
import (}
npm install grpc
var PROTO_PATH = __dirname + '/../rminfo/rminfo.proto';
var grpc = require('grpc');
console.log(PROTO_PATH)
//var hello_proto = grpc.load(PROTO_PATH).helloworld;
var rminfo_proto = grpc.load(PROTO_PATH).rminfo;
function FetchInfo(call, callback) {
var reqstr = call.request["rm_cmd"]
console.log(reqstr);
var reqo = JSON.parse(reqstr)
console.log(reqo);
var cmd = reqo["cmd"]
console.log(cmd)
if (cmd == 0 ){
callback(null, {rm_cmd_repl:'{"cmd":"0","output":{"timestamp":"12345","updateflag":"0"}}'});
}
}
/**
* Starts an RPC server that receives requests for the Greeter service at the
* sample server port
*/
function main() {
var server = new grpc.Server();
server.addService(rminfo_proto.RmRoute.service, {FetchInfo: FetchInfo});
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
server.start();
}
main();