Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
# Conflicts:
#	testdata/config/config.toml
  • Loading branch information
dobyte committed Oct 25, 2022
2 parents 8c88c84 + 164a11a commit ae893b3
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 95 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
strategy:
matrix:
go: [1.16, 1.17, 1.18]
name: build & test
runs-on: ubuntu-latest
services:
etcd:
image: gcr.io/etcd-development/etcd:v3.5.0
ports:
- 2379:2379
env:
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
ETCD_ADVERTISE_CLIENT_URLS: http://0.0.0.0:2379
consul:
image: consul:1.12.3
ports:
- 8500:8500
redis:
image: redis:6.0
ports:
- 6379:6379
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
179 changes: 97 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ due是一款基于Go语言开发的轻量级分布式游戏服务器框架。
* 日志:支持std、zap、logrus、aliyun、tencent等多种日志组件。
* 注册:支持consul、etcd、k8s、nacos、servicecomb、zookeeper等多种服务注册中心。
* 协议:支持json、protobuf等多种通信协议。
* 配置:支持json、yaml、toml、xml、ini等多种文件格式
* 配置:支持json、yaml、toml、xml等多种文件格式
* 通信:支持grpc、rpcx等多种高性能传输方案。
* 重启:支持服务器的平滑重启。

Expand All @@ -36,25 +36,26 @@ due是一款基于Go语言开发的轻量级分布式游戏服务器框架。
在due框架中,通信协议统一采用route+message的格式:

```
-------------------
| route | message |
-------------------
-------------------------
| seq | route | message |
-------------------------
```

tcp协议格式:

```
-------------------------
| len | route | message |
-------------------------
-------------------------------
| len | seq | route | message |
-------------------------------
```

说明:

1. route表示消息路由,固定为4字节,不同的路由对应不同的业务处理流程。
2. message表示消息体,采用json或protobuf编码。
3. 默认使用小端序编码。
4. 选择使用tcp协议时,为了解决粘包问题,还应在包前面加上包长度len,固定为4字节,默认使用小端序编码。
1. seq表示请求序列号,默认为2字节,常用于请求、响应对的确认。可通过配置文件修改
2. route表示消息路由,默认为2字节,不同的路由对应不同的业务处理流程。可通过配置文件修改
3. message表示消息体,采用json或protobuf编码。
4. 打包器,默认使用小端序编码。可通过配置文件修改
5. 选择使用tcp协议时,为了解决粘包问题,还应在包前面加上包长度len,固定为4字节,默认使用小端序编码。

### 5.心跳

Expand All @@ -63,11 +64,13 @@ tcp协议格式:
> 设计初衷:不想心跳检测侵入到业务路由层,哪怕是特殊的0号路由。
ws心跳包格式:

```go
[]byte
```

tcp心跳包格式:

```
-------
| len |
Expand All @@ -86,17 +89,21 @@ tcp心跳包格式:
下面我们就通过两段简单的代码来体验一下due的魅力,Let's go~~

0.启动组件

```shell
docker-compose up
```

> docker-compose.yaml文件已在docker目录中备好,可以直接取用
1.获取框架

```shell
go get github.com/dobyte/due@latest
go get github.com/dobyte/due/network/ws@latest
go get github.com/dobyte/due/registry/consul@latest
go get github.com/dobyte/due/registry/etcd@latest
go get github.com/dobyte/due/locate/redis@latest
go get github.com/dobyte/due/transport/grpc@latest
```

2.构建Gate服务器
Expand All @@ -107,120 +114,100 @@ package main
import (
"github.com/dobyte/due"
"github.com/dobyte/due/cluster/gate"
"github.com/dobyte/due/locate/redis"
"github.com/dobyte/due/config"
"github.com/dobyte/due/log"
"github.com/dobyte/due/mode"
"github.com/dobyte/due/network/ws"
"github.com/dobyte/due/registry/consul"
"github.com/dobyte/due/third/redis"
"github.com/dobyte/due/registry/etcd"
"github.com/dobyte/due/transport/grpc"
)

func main() {
// 创建容器
container := due.NewContainer()
// 设置模式
mode.SetMode(mode.DebugMode)

// 创建服务器
svr := ws.NewServer(
ws.WithServerListenAddr(":3553"),
ws.WithServerMaxConnNum(5000),
)

// 创建redis客户端
rds := redis.NewClient(
redis.WithAddrs("127.0.0.1:6379"),
redis.WithDB(0),
)

// 创建grpc服务器
gs := grpc.NewServer(
grpc.WithServerListenAddr(":8081"),
)

// 创建registry注册发现
registry := consul.NewRegistry(
consul.WithAddress("127.0.0.1:8500"),
)
// 监听配置
config.Watch()
defer config.Close()

// 创建网关
server := gate.NewGate(
gate.WithRedis(rds),
gate.WithServer(svr),
gate.WithGRPCServer(gs),
gate.WithRegistry(registry),
// 创建容器
container := due.NewContainer()
// 创建网关组件
component := gate.NewGate(
gate.WithServer(ws.NewServer()),
gate.WithLocator(redis.NewLocator()),
gate.WithRegistry(etcd.NewRegistry()),
gate.WithTransporter(grpc.NewTransporter()),
)

// 添加组件
container.Add(server)
// 启动服务器
// 添加网关组件
container.Add(component)
// 启动容器
container.Serve()
}
```

2.构建Node服务器
3.构建Node服务器

```go
package main

import (
"github.com/dobyte/due"
"github.com/dobyte/due/cluster/node"
"github.com/dobyte/due/registry/consul"
"github.com/dobyte/due/third/redis"
"github.com/dobyte/due/locate/redis"
"github.com/dobyte/due/config"
"github.com/dobyte/due/log"
"github.com/dobyte/due/mode"
"github.com/dobyte/due/registry/etcd"
"github.com/dobyte/due/transport/grpc"
)

func main() {
// 创建容器
container := due.NewContainer()

// 创建redis客户端
rds := redis.NewClient(
redis.WithAddrs("127.0.0.1:6379"),
redis.WithDB(0),
)
// 设置模式
mode.SetMode(mode.DebugMode)

// 创建grpc服务器
gs := grpc.NewServer(
grpc.WithServerListenAddr(":8082"),
)

// 创建registry注册发现
registry := consul.NewRegistry(
consul.WithAddress("127.0.0.1:8500"),
)
// 监听配置
config.Watch()
defer config.Close()

// 创建节点服务器
server := node.NewNode(
node.WithRedis(rds),
node.WithGRPCServer(gs),
node.WithRegistry(registry),
// 创建容器
container := due.NewContainer()
// 创建网关组件
component := node.NewNode(
node.WithLocator(redis.NewLocator()),
node.WithRegistry(etcd.NewRegistry()),
node.WithTransporter(grpc.NewTransporter()),
)

// 注册路由
server.Proxy().AddRoute(1, false, greetHandler)
component.Proxy().AddRouteHandler(1, false, greetHandler)
// 添加组件
container.Add(server)
container.Add(component)
// 启动服务器
container.Serve()
}

func greetHandler(req node.Request) {
_ = req.Response([]byte("hello world~~"))
func greetHandler(r node.Request) {
_ = r.Response([]byte("hello world~~"))
}
```

3.构建客户端
4.构建客户端

```go
package main

import (
"github.com/dobyte/due/config"
"github.com/dobyte/due/log"
"github.com/dobyte/due/mode"
"github.com/dobyte/due/network"
"github.com/dobyte/due/network/ws"
"github.com/dobyte/due/packet"
)

var (
handlers map[int32]handlerFunc
)
var handlers map[int32]handlerFunc

type handlerFunc func(conn network.Conn, buffer []byte)

Expand All @@ -231,14 +218,24 @@ func init() {
}

func main() {
client := ws.NewClient(ws.WithClientDialUrl("ws://127.0.0.1:3553"))
// 设置模式
mode.SetMode(mode.DebugMode)

// 监听配置
config.Watch()
defer config.Close()

// 创建客户端
client := ws.NewClient()
// 监听连接
client.OnConnect(func(conn network.Conn) {
log.Infof("connection is opened")
})
// 监听断开连接
client.OnDisconnect(func(conn network.Conn) {
log.Infof("connection is closed")
})
// 监听收到消息
client.OnReceive(func(conn network.Conn, msg []byte, msgType int) {
message, err := packet.Unpack(msg)
if err != nil {
Expand Down Expand Up @@ -272,6 +269,7 @@ func greetHandler(conn network.Conn, buffer []byte) {

func push(conn network.Conn, route int32, buffer []byte) error {
msg, err := packet.Pack(&packet.Message{
Seq: 1,
Route: route,
Buffer: buffer,
})
Expand All @@ -283,6 +281,23 @@ func push(conn network.Conn, route int32, buffer []byte) error {
}
```

### 6.详细示例
### 7.支持组件
1. 日志组件
* zap: github.com/dobyte/due/log/zap
* logrus: github.com/dobyte/due/log/logrus
* aliyun: github.com/dobyte/due/log/aliyun
* tencent: github.com/dobyte/due/log/zap
2. 网络组件
* ws: github.com/dobyte/due/network/ws
* tcp: github.com/dobyte/due/network/tcp
3. 注册发现
* etcd: github.com/dobyte/due/registry/etcd
* consul: github.com/dobyte/due/registry/consul
4. 传输组件
* grpc: github.com/dobyte/due/transporter/grpc
5. 定位组件
* redis: github.com/dobyte/due/locate/redis

### 8.详细示例

更多详细示例请点击[due-example](https://github.com/dobyte/due-example)
5 changes: 5 additions & 0 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type Component interface {
Init()
// Start 启动组件
Start()
// Restart 重启组件
Restart()
// Destroy 销毁组件
Destroy()
}
Expand All @@ -23,5 +25,8 @@ func (b *Base) Init() {}
// Start 启动组件
func (b *Base) Start() {}

// Restart 重启组件
func (b *Base) Restart() {}

// Destroy 销毁组件
func (b *Base) Destroy() {}
Loading

0 comments on commit ae893b3

Please sign in to comment.