diff --git a/README.md b/README.md index a32ef765..13a3c66c 100644 --- a/README.md +++ b/README.md @@ -34,60 +34,9 @@ While you had processed your logic, you can response or push message to the clie #### How to build distributed system with `Nano` -Nano has no built-in distributed system components, but you can easily implement it with `gRPC` and `smux` . Here we take grpc as an example. +Nano contains built-in distributed system solution, and make you creating a distributed game server easily. -- First of all, you need to define a remote component -```go -type RemoteComponent struct { - rpcClients []*grpc.ClientConn -} -``` - -- Second, fetch all grpc servers infomation from services like `etcd` or `consul` in `nano` lifetime hooks -```go -type ServerInfo struct { - Host string `json:"host"` - Port int `json:"port"` -} - -// lifetime callback -func (r *RemoteComponent) Init() { - // fetch server list from etcd - resp, err := http.Get("http://your_etcd_server/backend/server_list/area/10023") - if err != nil { - panic(err) - } - - servers := []ServerInfo{} - if err := json.NewDecoder(resp.Body).Decode(&servers); err != nil { - panic(err) - } - - for i := range servers { - server := servers[i] - client, err := grpc.Dial(fmt.Sprintf("%s:%d", server.Host, server.Post), options) - if err != nil { - panic(err) - } - r.rpcClients = append(r.rpcClients, client) - } -} - -func (r *RemoteComponent) client(s *session.Session) *grpc.ClientConn { - // load balance - return r.rpcClients[s.UID() % len(s.rpcClients)] -} - -// Your handler, accessed by: -// nanoClient.Request("RemoteComponent.DemoHandler", &pb.DemoMsg{/*...*/}) -func (r *RemoteComponent) DemoHandler(s *session.Session, msg *pb.DemoMsg) error { - client := r.client(s) - // do something with client - // .... - // ... - return nil -} -``` +See: [The distributed chat demo](https://github.com/lonng/nano/tree/master/examples/cluster) The Nano will remain simple, but you can perform any operations in the component and get the desired goals. You can startup a group of `Nano` application as agent to dispatch message to backend servers.