Giter Site home page Giter Site logo

amqp's Introduction

RPC implementation over RabbitMQ

Installation

Install the standard protocol buffer implementation from https://github.com/google/protobuf. Install the gogo golang protobuf implementation from https://github.com/gogo/protobuf.

Install mq-rpc binary:

go get github.com/gavrilaf/amqp/protoc-gen-mqrpc

Run example

Run RabbitMQ locally or from Docker image

docker run --hostname my-rabbit -p 5672:5672 --name rabbit22 rabbitmq:3

Run amqp/examples/rpc-test-srv

go run server.go messages.pb.go messages_mqrpc.gen.go

go run client.go messages.pb.go messages_mqrpc.gen.go

Using library

Write service definition:

service TestService {
  rpc Ping (Empty) returns (ServerStatus);
  rpc CreateUser(User) returns (ResourceID);
  rpc CreateAccount(Account) returns (ResourceID);
  rpc FindAccount(ResourceID) returns (Account);
}

message Empty {}

message ResourceID {
  string ID = 1;
}

message ServerStatus {
  int32 status = 1; 
}
......

Generate types & service:

protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --gogofast_out=. *.proto

protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --mqrpc_out=. *.proto

Impement server:

func main() {
  srv, err := rpc.CreateServer("amqp://localhost:5672", "rpc-rabbit-worker")
  RunServer(srv, &srvHandler{})
}

type srvHandler struct{}

func (p srvHandler) Ping(arg *Empty) (*ServerStatus, error) {
  fmt.Printf("Ping\n")
  return &ServerStatus{Status: 2}, nil
}

func (p srvHandler) CreateUser(user *User) (*ResourceID, error) {
  fmt.Printf("CreateUser: %v\n", user.String())
  return &ResourceID{ID: uuid.NewV4().String()}, nil
}

func (p srvHandler) CreateAccount(acc *Account) (*ResourceID, error) {
......

Connect to server and use service methods:

func main() {
  conn, err := rpc.Connect(rpc.ClientConfig{Url: "amqp://localhost:5672", 
                                            ServerQueue: "rpc-rabbit-worker", 
                                            Timeout: time.Second})
  
  client := NewTestServiceClient(conn)
  
  status, err := client.Ping(&Empty{})
	
  id1, err := client.CreateUser(&User{Username: "username", PasswordHash: "111", Device: nil})
......

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.