Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng authored Jun 30, 2019
1 parent bde9c63 commit 58cab73
Showing 1 changed file with 2 additions and 53 deletions.
55 changes: 2 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 58cab73

Please sign in to comment.