Server

Example:102basic

You can implements services in Server side.

The type of services is not important. You can use customized type to keep states or use struct{} or int simplely.

You should start a TCP or UDP server to serve services.

And you can aslo set some plugins to add features to the server.

Service

As service providers, you should define some service first.Currently rpcx only support exported methods as functions of service.And the exported method must satisfy the below rules:

  • exported method of exported type
  • three arguments, the first is of context.Context, both of exported type or three arguments
  • the third argument is a pointer
  • one return value, of type error
    in rpcx 3.1 we want to add register raw functions that satisfy last three rules, but for rpcx 3.0, you must use method to implement services.

you can use RegisterName to register methods of rcvr and name of this service is name.If you use Register, the generated service name is type name of rcvr.You can add meta data of this service in registry, that can be used by clients or service manager, for example, weightgeolocationmetrics.

  1. func (s *Server) Register(rcvr interface{}, metadata string) error
  2. func (s *Server) RegisterName(name string, rcvr interface{}, metadata string) error

There is an example that implements a Mul method:

  1. import "context"
  2. type Args struct {
  3. A int
  4. B int
  5. }
  6. type Reply struct {
  7. C int
  8. }
  9. type Arith int
  10. func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
  11. reply.C = args.A * args.B
  12. return nil
  13. }

In this example, you can define Arith as struct{} and it doesn't affect the this service.

And you can define args as Args, this service aslo works.

Server

After you define services, you should want to explore to users. You should start a TCP server or UDP server to listen.

The server supports below approaches to start, serve and close:

  1. func NewServer(options ...OptionFn) *Server
  2. func (s *Server) Close() error
  3. func (s *Server) RegisterOnShutdown(f func())
  4. func (s *Server) Serve(network, address string) (err error)
  5. func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

First you use NewServer to create a server instance with options. Second you can invokde Serve or ServeHTTP to serve.

The server contains some fields (and unexported fields):

  1. type Server struct {
  2. Plugins PluginContainer
  3. // AuthFunc can be used to auth.
  4. AuthFunc func(ctx context.Context, req *protocol.Message, token string) error
  5. // contains filtered or unexported fields
  6. }

Plugins caontains all plugins in this server. We will introduce it in later chapters.

AuthFunc is a authentication function which can check clients are authorized. It is also introduced later.

rpcx provides three OptionFn to set options:

  1. func WithReadTimeout(readTimeout time.Duration) OptionFn
  2. func WithTLSConfig(cfg *tls.Config) OptionFn
  3. func WithWriteTimeout(writeTimeout time.Duration) OptionFn

It can set readTimeout, writeTimeout and tls.Config.

ServeHTTP uses a HTTP connection to serve services. It hijacks the connnection to communicate with clients.

Serve uses TCP or UDP ptotocol to communicate with clients.

rpcx supports the below network:

  • tcp: recommended network
  • http: hijack http connection
  • unix: unix domain sockets
  • reuseport: use SO_REUSEPORT socket option, only support Linux kernel 3.9+
  • quic: support quic protocol
  • kcp: sopport kcp protocol
    There is a server example:
  1. package main
  2. import (
  3. "flag"
  4. example "github.com/rpcx-ecosystem/rpcx-examples3"
  5. "github.com/smallnest/rpcx/server"
  6. )
  7. var (
  8. addr = flag.String("addr", "localhost:8972", "server address")
  9. )
  10. func main() {
  11. flag.Parse()
  12. s := server.NewServer()
  13. //s.RegisterName("Arith", new(example.Arith), "")
  14. s.Register(new(example.Arith), "")
  15. s.Serve("tcp", *addr)
  16. }

By smallnest updated 2018-03-27 11:12:10

原文:

http://doc.rpcx.site/part1/server.html