From b4d5a6636ea75724739a6c7defb82b3b7502e7a4 Mon Sep 17 00:00:00 2001 From: boyce Date: Fri, 1 Mar 2019 18:39:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=95=99=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 34df791..93ce464 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,14 @@ orgin 是一个由 Go 语言(golang)编写的分布式开源游戏服务器 orgin 解决的问题: * orgin总体设计如go语言设计一样,总是尽可能的提供简洁和易用的模式,快速开发。 -* 能够根据业务需求快速并灵活的制定服务器架构,甚至做到上线后根据负载需求动态调整。 +* 能够根据业务需求快速并灵活的制定服务器架构。 * 利用多核优势,将不同的service配置到不同的node,并能高效的协同工作。 * 将整个引擎抽象三大对象,node,service,module。通过统一的组合模式管理游戏中各功能模块的关系。 * 有丰富并健壮的工具库。 Hello world! --------------- -下面我们来一步步的建立orgin服务器,先下载[orgin引擎](github.com/duanhf2012/origin),或者使用如下命令: +下面我们来一步步的建立orgin服务器,先下载[orgin引擎](),或者使用如下命令: ```go go get -v -u github.com/duanhf2012/origin ``` @@ -82,7 +82,7 @@ orgin所有的结点与服务通过配置进行关联,配置文件分为两大 orgin第一个服务: --------------- -我们准备新建两个服务,分别是CTestService1与CTestService2。 +我们准备的NodeId为1的结点下新建两个服务,分别是CTestService1与CTestService2。 * config/cluster.json内容如下 ``` { @@ -98,7 +98,94 @@ orgin第一个服务: ] } ``` +* main.go运行代码 + +```go +package main + +import ( + "fmt" + "time" + + "github.com/duanhf2012/origin/originnode" + "github.com/duanhf2012/origin/service" +) + +//定义一个服务,必需继承自service.BaseService +type CTestService1 struct { + service.BaseService +} + +//服务的初始化 +func (slf *CTestService1) OnInit() error { + fmt.Println("CTestService1.OnInit") + return nil +} + +//服务运行,返回值如果为true,将会重复的进入OnRun, +//直到返回false为止,此函数只会进入一次 +func (slf *CTestService1) OnRun() bool { + fmt.Println("CTestService1.OnRun") + return false +} + +//当OnRun退出时,调用OnEndRun,可以收尾OnRun的运行处理 +func (slf *CTestService1) OnEndRun() { + fmt.Println("CTestService1.OnEndRun") +} +type CTestService2 struct { + service.BaseService +} + +func (slf *CTestService2) OnInit() error { + fmt.Println("CTestService2.OnInit") + return nil +} + +func (slf *CTestService2) OnRun() bool { + fmt.Println("CTestService2.OnRun") + time.Sleep(time.Second * 5) + + //返回true,将重复进入 + return true +} + +func (slf *CTestService2) OnEndRun() { + fmt.Println("CTestService2.OnEndRun") +} + +func main() { + //新建一个orgin node对象 + node := originnode.NewOrginNode() + if node == nil { + return + } + + //安装CTestService1与CTestService2服务 + node.SetupService(&CTestService1{}, &CTestService2{}) + node.Init() + node.Start() +} +``` +通过以下命令运行: +``` +main.exe NodeId=1 +``` +输出结果: +``` +CTestService2.OnInit +CTestService1.OnInit +CTestService2.OnRun +CTestService1.OnRun +CTestService1.OnEndRun +CTestService2.OnRun +CTestService2.OnRun +CTestService2.OnRun +CTestService2.OnRun +``` +通过日志可以确认,在Node启动时分别驱动Service的OnInit,OnRun,OnEndRun,上面的日志中CTestService2.OnRun会被循环调用, +因为在OnRun的返回是true,否则只会进入一次。如果你不需要OnRun可以不定义OnRun函数。