插件开发

rpcx提供了插件式的开发,你可以在某个或者某些插入点上加入你自己的业务逻辑来扩展RPC框架,事实上注册中心就是一个插件。

服务器插入点

服务端提供了以下的插入点:

  1. func (p *ServerPluginContainer) DoPostConnAccept(conn net.Conn) bool
  2. func (p *ServerPluginContainer) DoPostReadRequestBody(body interface{}) error
  3. func (p *ServerPluginContainer) DoPostReadRequestHeader(r *rpc.Request) error
  4. func (p *ServerPluginContainer) DoPostWriteResponse(resp *rpc.Response, body interface{}) error
  5. func (p *ServerPluginContainer) DoPreReadRequestBody(body interface{}) error
  6. func (p *ServerPluginContainer) DoPreReadRequestHeader(r *rpc.Request) error
  7. func (p *ServerPluginContainer) DoPreWriteResponse(resp *rpc.Response, body interface{}) error
  8. func (p *ServerPluginContainer) DoRegister(name string, rcvr interface{}, metadata ...string) error

你可以将实现插入点方法的插件加入到服务器的容器中,或者移除容器。

  1. func (p *ServerPluginContainer) Add(plugin IPlugin) error
  2. func (p *ServerPluginContainer) GetAll() []IPlugin
  3. func (p *ServerPluginContainer) GetByName(pluginName string) IPlugin
  4. func (p *ServerPluginContainer) GetDescription(plugin IPlugin) string
  5. func (p *ServerPluginContainer) GetName(plugin IPlugin) string
  6. func (p *ServerPluginContainer) Remove(pluginName string) error

事实上,服务器端的插件点定义了几个接口,你只需要实现一个或者几个接口即可。

  1. //IRegisterPlugin represents register plugin. IRegisterPlugin interface { Register(name string, rcvr interface{}, metadata ...string) error }
  2. //IPostConnAcceptPlugin represents connection accept plugin. // if returns false, it means subsequent IPostConnAcceptPlugins should not contiune to handle this conn // and this conn has been closed. IPostConnAcceptPlugin interface { HandleConnAccept(net.Conn) bool }
  3. //IServerCodecPlugin represents . IServerCodecPlugin interface { IPreReadRequestHeaderPlugin IPostReadRequestHeaderPlugin IPreReadRequestBodyPlugin IPostReadRequestBodyPlugin IPreWriteResponsePlugin IPostWriteResponsePlugin }
  4. //IPreReadRequestHeaderPlugin represents . IPreReadRequestHeaderPlugin interface { PreReadRequestHeader(r *rpc.Request) error }
  5. //IPostReadRequestHeaderPlugin represents . IPostReadRequestHeaderPlugin interface { PostReadRequestHeader(r *rpc.Request) error }
  6. //IPreReadRequestBodyPlugin represents . IPreReadRequestBodyPlugin interface { PreReadRequestBody(body interface{}) error }
  7. //IPostReadRequestBodyPlugin represents . IPostReadRequestBodyPlugin interface { PostReadRequestBody(body interface{}) error }
  8. //IPreWriteResponsePlugin represents . IPreWriteResponsePlugin interface { PreWriteResponse(*rpc.Response, interface{}) error }
  9. //IPostWriteResponsePlugin represents . IPostWriteResponsePlugin interface { PostWriteResponse(*rpc.Response, interface{}) error }

客户端插入点

客户端也提供了一些插入点:

  1. func (p *ClientPluginContainer) DoPostReadResponseBody(body interface{}) error
  2. func (p *ClientPluginContainer) DoPostReadResponseHeader(r *rpc.Response) error
  3. func (p *ClientPluginContainer) DoPostWriteRequest(r *rpc.Request, body interface{}) error
  4. func (p *ClientPluginContainer) DoPreReadResponseBody(body interface{}) error
  5. func (p *ClientPluginContainer) DoPreReadResponseHeader(r *rpc.Response) error
  6. func (p *ClientPluginContainer) DoPreWriteRequest(r *rpc.Request, body interface{}) error

你可以将插件加入的插件容器中:

  1. func (p *ClientPluginContainer) Add(plugin IPlugin) error
  2. func (p *ClientPluginContainer) GetAll() []IPlugin
  3. func (p *ClientPluginContainer) GetByName(pluginName string) IPlugin
  4. func (p *ClientPluginContainer) GetDescription(plugin IPlugin) string
  5. func (p *ClientPluginContainer) GetName(plugin IPlugin) string
  6. func (p *ClientPluginContainer) Remove(pluginName string) error

它也定义了一些插入点的接口,你只需实现这些接口即可。

  1. //IPreReadResponseHeaderPlugin represents . IPreReadResponseHeaderPlugin interface { PreReadResponseHeader(*rpc.Response) error }
  2. //IPostReadResponseHeaderPlugin represents . IPostReadResponseHeaderPlugin interface { PostReadResponseHeader(*rpc.Response) error }
  3. //IPreReadResponseBodyPlugin represents . IPreReadResponseBodyPlugin interface { PreReadResponseBody(interface{}) error }
  4. //IPostReadResponseBodyPlugin represents . IPostReadResponseBodyPlugin interface { PostReadResponseBody(interface{}) error }
  5. //IPreWriteRequestPlugin represents . IPreWriteRequestPlugin interface { PreWriteRequest(*rpc.Request, interface{}) error }
  6. //IPostWriteRequestPlugin represents . IPostWriteRequestPlugin interface { PostWriteRequest(*rpc.Request, interface{}) error }