rpc

package module
v0.0.0-...-6674cbf Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

README

rpc

为了学习rpc框架原理

  • 实现自定义RPC通信协议,实现基于TCP长连接的服务器
  • 实现支持并发、异步请求的客户端
  • 实现基于反射的Go语言服务注册
  • 实现客户端、服务器的超时处理
  • 实现基于HTTP协议的握手操作,使用http.Hijacker实现
  • 实现客户端的负载均衡

TODO:

  • 实现protobuf编码方式
  • 实现服务注册与发现

Documentation

Index

Constants

View Source
const MagicNumber = 0x3bef5c

MagicNumber 魔数

Variables

View Source
var DefaultOption = &Option{
	MagicNumber:    MagicNumber,
	CodecType:      codec.GobType,
	ConnectTimeout: time.Second * 10,
}

DefaultOption 默认协商信息

View Source
var DefaultServer = NewServer()

DefaultServer 默认服务器实例

View Source
var ErrShutdown = errors.New("connection is shutdown")

Functions

func Accept

func Accept(lis net.Listener)

Accept 使用默认服务器实例去接受请求并处理

func HandleHTTP

func HandleHTTP()

func Register

func Register(rcvr any) error

Register 默认服务器注册服务

Types

type Call

type Call struct {
	Seq           int
	ServiceMethod string
	Args          any
	Reply         any
	Error         error
	Done          chan *Call // 结束调用时,通过这个通道通知调用方
}

Call 代表一次RPC调度

type Client

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

Client RPC客户端

func Dial

func Dial(network, address string, opt *Option) (c *Client, err error)

Dial 连接RPC服务器

func DialHTTP

func DialHTTP(network, address string, opt *Option) (*Client, error)

func NewClient

func NewClient(conn net.Conn, opt *Option) (*Client, error)

NewClient 创建客户端

func NewHTTPClient

func NewHTTPClient(conn net.Conn, opt *Option) (*Client, error)

NewHTTPClient 通过HTTP进行建立连接

func XDial

func XDial(rpcAddr string, opt *Option) (*Client, error)

XDial 连接RPC服务器

func (*Client) Call

func (c *Client) Call(ctx context.Context, serviceMethod string, args, reply any) error

Call 同步调用

func (*Client) Close

func (c *Client) Close() error

Close 关闭客户端

func (*Client) Go

func (c *Client) Go(serviceMethod string, args, reply any, done chan *Call) *Call

Go 这个是异步的调用

func (*Client) IsAvailable

func (c *Client) IsAvailable() bool

IsAvailable 客户端是否还能正常工作

type Option

type Option struct {
	MagicNumber    int
	CodecType      codec.Type
	ConnectTimeout time.Duration // 连接超时
	HandleTimeout  time.Duration // 处理超时
}

Option 协商信息,协商协议类型 | Option{MagicNumber: xxx, CodecType: xxx} | Header{ServiceMethod ...} | Body interface{} | | <------ 固定 JSON 编码 ------> | <------- 编码方式由 CodeType 决定 ------->|

type Server

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

Server RPC服务器

func NewServer

func NewServer() *Server

NewServer 创建新的服务器

func (*Server) Accept

func (s *Server) Accept(lis net.Listener)

Accept 接受请求并处理

func (*Server) HandleHTTP

func (s *Server) HandleHTTP()

func (*Server) Register

func (s *Server) Register(rcvr any) error

Register 注册服务

func (*Server) ServeConn

func (s *Server) ServeConn(conn io.ReadWriteCloser)

ServeConn 处理一个连接的请求

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP 通过HTTP去处理RPC请求 使用HTTP好处:一个TCP端口通过不同HTTP路径去支持不同服务 HTTP这里做的是协议的协商工作,去建立一个长连接

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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