FAQ
Hi chai2010:

     I have tried you protobuf rpc, it works fine for golang server and
golang client.

     But I found I cann't call python rpc server, or use python client to
request golang server. Is this a bug or you haven't consider cross-language
compatibility?

     btw, I use https://code.google.com/p/protobuf-socket-rpc/ to make
python protobuf rpc work.

On Wednesday, May 8, 2013 8:46:15 PM UTC+8, chai2010 wrote:

已经实现了一个简单的C++版本, 代码已经更新.

Go和C++实现的服务端和客户端的例子:

https://code.google.com/p/protorpc/source/browse/protobuf/src/google/protobuf/rpc/example/

2013/5/4 chai2010 <chais...@gmail.com <javascript:>>
protorpc的代码更新到以下的网址:

https://bitbucket.org/chai2010/protorpc
https://code.google.com/p/protorpc/

目前还在完善C++部分的RPC实现(C++部分代码没有上传).
完成后会统一更新上去.


2013/4/26 chai2010 <chais...@gmail.com <javascript:>>
Hi, I have write a Protobuf-RPC in Go
<https://bitbucket.org/chai2010/gopath/src/8d54643a1bbc/src/net/rpc/protorpc?at=default>
.

1. Download (can't use `go get`)

hg clone https://bitbucket.org/chai2010/gopath

2. Insall `net/rpc/protoprc` and `protoc-gen-go`

- Add gopath dir to $GOPATH
- Add gopath/bin dir to $PATH

go install encoding/protobuf/protoc-gen-go
go test net/rpc/protorpc/service.pb
go test net/rpc/protorpc

Note:
- You need install protoc app on !Windows OS
- I create a `encoding/protobuf/protoc-gen-go/generator/service.go
<https://bitbucket.org/chai2010/gopath/src/8d54643a1bbc/src/encoding/protobuf/protoc-gen-go/generator/service.go?at=default>
`
to gen rpc stub file for `protoc-gen-go`

3. How to use (from `net/rpc/protorpc/doc.go
<https://bitbucket.org/chai2010/gopath/src/8d54643a1bbc/src/net/rpc/protorpc/doc.go?at=default>
`)?

Here is a simple proto file("arith.pb/arith.proto"):

package arith;

// At least one of xx_generic_services is true
option cc_generic_services = true;
option java_generic_services = true;
option py_generic_services = true;

message ArithRequest {
optional int32 a = 1;
optional int32 b = 2;
}

message ArithResponse {
optional int32 val = 1;
optional int32 quo = 2;
optional int32 rem = 3;
}

service ArithService {
rpc multiply (ArithRequest) returns (ArithResponse);
rpc divide (ArithRequest) returns (ArithResponse);
}

Then use "protoc-gen-go" to generate "arith.pb.go
<https://bitbucket.org/chai2010/gopath/src/8d54643a1bbc386477da25beb25efa5f02acbe28/src/net/rpc/protorpc/service.pb/arith.pb.go?at=default>"
file(include rpc stub):

$ go install encoding/protobuf/protoc-gen-go
$ cd arith.pb && protoc --go_out=. arith.proto

The server calls (for TCP service):

package server

import (
"encoding/protobuf/proto"
"errors"

"./arith.pb"
)

type Arith int

func (t *Arith) Multiply(args *arith.ArithRequest, reply
*arith.ArithResponse) error {
reply.Val = proto.Int32(args.GetA() * args.GetB())
return nil
}

func (t *Arith) Divide(args *arith.ArithRequest, reply
*arith.ArithResponse) error {
if args.GetB() == 0 {
return errors.New("divide by zero")
}
reply.Quo = proto.Int32(args.GetA() / args.GetB())
reply.Rem = proto.Int32(args.GetA() % args.GetB())
return nil
}

func main() {
go arith.ListenAndServeArithService("tcp", ":1984", new(Arith))
}

At this point, clients can see a service "Arith" with methods
"Arith.Multiply" and
"Arith.Divide". To invoke one, a client first dials the server:

client, stub, err := arith.DialArithService("tcp", "127.0.0.1:1984")
if err != nil {
log.Fatal(`arith.DialArithService("tcp", "127.0.0.1:1984"):`, err)
}
defer client.Close()

Then it can make a remote call with stub:

var args ArithRequest
var reply ArithResponse

args.A = proto.Int32(7)
args.B = proto.Int32(8)
if err = stub.Multiply(&args, &reply); err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d", args.GetA(), args.GetB(), reply.GetVal())

Is very simple to use "Protobuf-RPC" with "protoc-gen-go" tool. Try it
out.

--
chaishushan
http://my.oschina.net/chai2010
http://chai2010.bitbucket.org/
http://golang-china.org

--
chaishushan
http://my.oschina.net/chai2010
http://chai2010.bitbucket.org/
http://golang-china.org

--
chaishushan
http://my.oschina.net/chai2010
http://chai2010.bitbucket.org/
http://golang-china.org
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 11, '14 at 6:53a
activeDec 11, '14 at 6:53a
posts1
users1
websitegolang.org

1 user in discussion

Kenneth Tse: 1 post

People

Translate

site design / logo © 2021 Grokbase