rmqrpc

package module
v0.0.0-...-0c173dd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2017 License: BSD-3-Clause Imports: 10 Imported by: 0

README

rmqrpc

rmqrpc 实现了go语言基于rabbitmq为数据通道和protobuf为载体的远程调用。远程调用框架使用了go 自带的net/rpc. rmqrpc 仅仅添加了处理通信和载体相关的ClientCodec 和 ServerCodec以及protobuf rpc 的简单代码生成。

特别说明

本项目是在https://github.com/chai2010/protorpc 进行改动的, 特别感谢chai2010开源protorpc。本项目也视为是protorpc 的rabbitmq分支。

Install

1.Install rabbitmq

安装rabbitmq 最新版本RabbitMQ 3.6.6 release

参考官网 http://www.rabbitmq.com/download.htmlanz

安装rabbitmq go client package :go get github.com/streadway/amqp

2.Install rmqrpc package:

  1. go get github.com/liuyuanlin/rmqrpc
  2. go run hello.go

3.Install protoc-gen-go plugin:

  1. install protoc at first: http://github.com/google/protobuf/releases
  2. go get github.com/liuyuanlin/rmqrpc/protoc-gen-go
  3. go generate github.com/liuyuanlin/protorpc/examples/service.pb
  4. go test github.com/liuyuanlin/protorpc/examples/service.pb

Examples

First, create echo.proto:

syntax = "proto3";

package service;

message EchoRequest {
	string msg = 1;
}

message EchoResponse {
	string msg = 1;
}

service EchoService {
	rpc Echo (EchoRequest) returns (EchoResponse);
	rpc EchoTwice (EchoRequest) returns (EchoResponse);
}

Second, generate echo.pb.go from [echo.proto]

protoc --plugin=protoc-gen-go=../../protoc-gen-go/protoc-gen-go --go_out=plugins=rmqrpc:. echo.proto arith.proto

Now, we can use the stub code like this:

package main
import (
	"fmt"
	"log"

	service "github.com/liuyuanlin/rmqrpc/examples/service.pb"
)

type Echo int

func (t *Echo) Echo(args *service.EchoRequest, reply *service.EchoResponse) error {
	reply.Msg = args.Msg
	return nil
}

func (t *Echo) EchoTwice(args *service.EchoRequest, reply *service.EchoResponse) error {
	reply.Msg = args.Msg + args.Msg
	return nil
}

func init() {
	go service.StartEchoServiceServer("amqp://guest:guest@localhost:5672/", "", "rpc_queue", new(Echo))
}

func main() {
	echoClient := service.NewEchoServiceClient("amqp://guest:guest@localhost:5672/", "", "rpc_queue")
	if echoClient == nil {
		log.Fatalf("service.NewEchoServiceClient:fail")
	}
	defer echoClient.Close()

	args := &service.EchoRequest{Msg: "你好, 世界!"}
	reply, err := echoClient.EchoTwice(args)
	if err != nil {
		log.Fatalf("echoClient.EchoTwice: %v", err)
	}
	fmt.Println(reply.Msg)

	// Output:
	// 你好, 世界!你好, 世界!
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(uri string, exchangeName string, queueName string) (*rpc.Client, error)

Dial connects to a Protobuf-RPC server at the specified network address.

func NewClient

func NewClient(uri string, exchangeName string, queueName string) *rpc.Client

NewClient returns a new rpc.Client to handle requests to the set of services at the other end of the connection.

func NewClientCodec

func NewClientCodec(uri string, exchangeName string, queueName string) rpc.ClientCodec

NewClientCodec returns a new rpc.ClientCodec using Protobuf-RPC on conn.

func NewServerCodec

func NewServerCodec(uri string, exchangeName string, queueName string) rpc.ServerCodec

NewServerCodec returns a serverCodec that communicates with the ClientCodec on the other end of the given conn.

func ServeConn

func ServeConn(uri string, exchangeName string, queueName string)

ServeConn runs the Protobuf-RPC server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement.

Types

This section is empty.

Directories

Path Synopsis
examples
message.pb
Package message is a generated protocol buffer package.
Package message is a generated protocol buffer package.
proto3.pb
Package proto3_proto is a generated protocol buffer package.
Package proto3_proto is a generated protocol buffer package.
service.pb
Package service is a generated protocol buffer package.
Package service is a generated protocol buffer package.
protoc-gen-go is a plugin for the Google protocol buffer compiler to generate Go code.
protoc-gen-go is a plugin for the Google protocol buffer compiler to generate Go code.
rmqrpc
Package rmqrpc outputs protorpc stub code.
Package rmqrpc outputs protorpc stub code.
Package rmqrpc_wire is a generated protocol buffer package.
Package rmqrpc_wire is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL