mrpc

package module
v0.0.0-...-9fbb035 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: MIT Imports: 11 Imported by: 1

README

mRPC

mRPC = mDNS + RPC.

Features

  1. mDNS的特性,即局域网服务发现。
  2. Go的RPC,即无需像gRPC那样生成代码,可以直接使用。

Example

RPC protocol and server implementation:

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
}

Server RPC service:

rpc := mrpc.NewRPC()
err := rpc.RegisterService("rpcserver-instance-1", new(server.Arith)) // &Arith{} 不行?
if err != nil {
    log.Fatalf("rpc.RegisterService error: %v", err)
}
select {}

Client RPC:

rpc := mrpc.NewRPC()

// Synchronous call
args := &server.Args{
    A: 7,
    B: 8,
}
var reply int
err := rpc.Call("rpcserver-instance-1", "Arith.Multiply", args, &reply)
if err != nil {
    log.Fatal("arith error:", err)
}
log.Printf("Arith: %d*%d=%d", args.A, args.B, reply)

Documentation

GoDoc

Documentation

Overview

mRPC = mDNS + RPC.

Index

Constants

View Source
const (
	ServiceActionOnline = iota
	ServiceActionOffline
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncCallResult

type AsyncCallResult struct {
	// TODO RequestID
	ServiceMethod string      // The name of the service and method to call.
	Args          interface{} // The argument to the function (*struct).
	Reply         interface{} // The reply from the function (*struct).
	Error         error       // After completion, the error status.
}

type RPC

type RPC struct {

	// client
	AsyncCallResultChan chan *AsyncCallResult // 通知异步调用结果
	ServiceEventChan    chan *ServiceEvent    // 通知service上线或下线
	// contains filtered or unexported fields
}

RPC RPC结构

func NewRPC

func NewRPC() *RPC

NewRPC 创建RPC对象。服务端和客户端都使用这个对象做RPC。

func (*RPC) AsyncCall

func (r *RPC) AsyncCall(instanceName string, serviceMethod string, args interface{}) error

AsyncCall 异步调用。 通过RPC的AsyncCallResultChan接收响应。

func (*RPC) Call

func (r *RPC) Call(service string, method string, args interface{}, reply interface{}) error

Call 同步调用。serviceMethod和rcvr名字相同。

func (*RPC) Close

func (r *RPC) Close() error

Close 关闭RPC。

func (*RPC) RegisterService

func (r *RPC) RegisterService(serviceName string, rcvrs ...interface{}) error

RegisterService 注册服务实例和receiver 其中rcvr实现了handler; 具体用什么ip、port,随机选取,对用户透明。需要rpc中维护连接池。

func (*RPC) UnRegisterService

func (r *RPC) UnRegisterService(service string)

type ServiceEvent

type ServiceEvent struct {
	ServiceName   string
	ServiceAction int
}

Jump to

Keyboard shortcuts

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