begoniarpc

package module
v0.0.0-...-c7e9c76 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 8 Imported by: 0

README

Begonia-RPC

A golang rpc framework for efficient and concise.

User Guide

Prerequisites

Golang Version >= 1.11.2

Installation
$ go get github.com/MashiroC/begonia-rpc
Example

service center

package main

import (
	"github.com/MashiroC/begonia-sdk"
)

func main() {
	rpc := begoniarpc.Default()
	rpc.Run("0.0.0.0:8080")
}

How to provide a service?

package main

import (
	"github.com/MashiroC/begonia-rpc/entity"
	"github.com/MashiroC/begonia-rpc/sdk"
)

type MathService struct {
}

type HelloService struct {
}

func (s *MathService) Sum(a, b int) (res int) {
    return a + b
}


func (h *HelloService) Hello(name string) (res string) {
	return "Hello " + name
}

func main() {
	cli := begonia.Default(":8080")
	cli.Sign("Hello", &HelloService{})
	cli.Sign("Math",&MathService{})
	cli.KeepConnect()
}

And How to call remote function?

package main

import (
	"fmt"
	"github.com/MashiroC/begonia-rpc/entity"
	"github.com/MashiroC/begonia-rpc/sdk"
)

func main() {
    
    // get a begonia client
	cli := begonia.Default(":8080")
	
    // get a service
    helloService := cli.Service("Hello")
    
    // get a sync Function
    hello := helloService.FunSync("Hello")
    
    // call it!
    res, err := hello("MashiroC")
    
    fmt.Println(res, err)
    // Hello Mashiroc <nil>
    
   
    // get a async function
    helloAsync := helloService.FunAsync("Hello")

    // call it too!
    helloAsync(func(res interface{}, err error) {
        fmt.Println(res,err)
    	}, "MashiroC")
    
}
Run

this is rpc center, if you want to provide a service, please use sdk.

$ go build -o rpccenter ./cmd/server/main.go
$ ./rpccenter

or

$ go run ./cmd/server/main.go
Release History

0.1.0

Proof-of-concept code

Documentation

Overview

sdk-rpc 轻量级rpc框架 架构分为客户端 服务端 服务中心三部分 By MashiroC

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ServerCenter

type ServerCenter struct {
	// contains filtered or unexported fields
}

ServerCenter 服务中心的实体

func Default

func Default() *ServerCenter

Default 返回一个默认配置的服务中心

func (*ServerCenter) Run

func (s *ServerCenter) Run(addr string)

Run 开始监听

type WaitChan

type WaitChan struct {
	// contains filtered or unexported fields
}

WaitChan 根据uuid获得响应的回调 并发安全

func NewWaitChan

func NewWaitChan(len uint) *WaitChan

构造函数

func (*WaitChan) Get

func (w *WaitChan) Get(k string) (callback func(entity.Response), ok bool)

Get 取

func (*WaitChan) Remove

func (w *WaitChan) Remove(k string)

Remove 删

func (*WaitChan) Set

func (w *WaitChan) Set(k string, callback func(entity.Response))

Set 加

Directories

Path Synopsis
cmd
cmd
conn 封装过的连接 可以直接读出帧 帧分成三个部分 分别是len、opcode、payload len是payload的长度 有baseLen和extendLen两种 如果len小于255 占一个byte 如果length的长度大于等于255 先1byte的255 然后用2byte来表示长度 最大2byte opcode占1个byte 具体有哪些看下面的常量 不管payload是什么类型的数据 这里只处理读写数据
conn 封装过的连接 可以直接读出帧 帧分成三个部分 分别是len、opcode、payload len是payload的长度 有baseLen和extendLen两种 如果len小于255 占一个byte 如果length的长度大于等于255 先1byte的255 然后用2byte来表示长度 最大2byte opcode占1个byte 具体有哪些看下面的常量 不管payload是什么类型的数据 这里只处理读写数据
util
log

Jump to

Keyboard shortcuts

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