amqp-rpc
amqp-rpc copied to clipboard
An AMQP driver for net/rpc.
amqp-rpc
Example
Server
package server
import (
"log"
"net/rpc"
"github.com/streadway/amqp"
"github.com/vibhavp/amqp-rpc"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
serverCodec, err := amqprpc.NewServerCodec(conn, "rpc_queue", amqprpc.GobCodec{})
if err != nil {
log.Fatal(err)
}
rpc.Register(new(Arith))
rpc.ServeCodec(serverCodec)
}
Client
package client
import (
"github.com/streadway/amqp"
"github.com/vibhavp/amqp-rpc"
)
func main() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
clientCodec, err := amqprpc.NewClientCodec(conn, "rpc_queue", amqprpc.GobCodec{})
if err != nil {
log.Fatal(err)
}
reply := new(int)
client := rpc.NewClientWithCodec(clientCodec)
err := client.Call("Arith.Multiply", server.Args{1, 2}, reply)
if err != nil {
log.Fatal("arith error: ", err)
}
log.Printf("1 * 2 = %d", *reply)
}