From 79a6b7939798d09a240519e22bfdb77cb6d4172e Mon Sep 17 00:00:00 2001 From: boyce <6549168@qq.com> Date: Mon, 23 Sep 2019 15:56:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5Readme=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 265 +++++++++++++++++------- Test2/Helloworld/SubNet1_Service.go | 34 --- Test2/Helloworld/SubNet1_Service1.go | 40 ---- Test2/Helloworld/SubNet1_Service2.go | 31 --- Test2/Helloworld/SubNet2_Service1.go | 31 --- Test2/Helloworld/config/cluster.json | 26 +-- Test2/Helloworld/config/nodeconfig.json | 56 ++--- Test2/Helloworld/main.go | 10 - 8 files changed, 210 insertions(+), 283 deletions(-) delete mode 100644 Test2/Helloworld/SubNet1_Service.go delete mode 100644 Test2/Helloworld/SubNet1_Service1.go delete mode 100644 Test2/Helloworld/SubNet1_Service2.go delete mode 100644 Test2/Helloworld/SubNet2_Service1.go diff --git a/README.md b/README.md index 4ab25f3..632c840 100644 --- a/README.md +++ b/README.md @@ -180,107 +180,221 @@ origin第一个服务: * config/cluster.json内容如下 ``` { -"NodeList":[ -{ - "NodeID":1, - "NodeName":"N_Node1", - "ServerAddr":"127.0.0.1:8080", - - "ServiceList":["CTestService1","CTestService2"], - "ClusterNode":[] -} -] + "SubNet": [{ + "Remark": "Manual,Full,Auto", + "SubNetMode": "Full", + "SubNetName": "SubNet1", + "PublicServiceList": ["logiclog"], + "NodeList": [{ + "NodeID": 1, + "NodeName": "N_Node1", + "ServiceList": [ + "TestService1", + "TestService2" + ], + "ClusterNode":[] + }, + { + "NodeID": 2, + "NodeName": "N_Node2", + "ServiceList": [ + "TestService3" + ], + "ClusterNode":[] + } + ] + } + ] } ``` +* config/nodeconfig.json内容如下 +``` +{ + "Public": { + "LogLevel": 1, + "HttpPort": 9400, + "WSPort": 9500, + "Environment": "Test", + "IsListenLog": 1, + "IsSendErrorMail": 0 + }, + + "NodeList": [{ + "NodeID": 1, + "NodeAddr": "127.0.0.1:8081" + }, + { + "NodeID": 2, + "NodeAddr": "127.0.0.1:8082" + } + ] +} +``` + + * 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() { - //新建一个origin node对象 + node := originnode.NewOriginNode() if node == nil { return } - //安装CTestService1与CTestService2服务 - node.SetupService(&CTestService1{}, &CTestService2{}) node.Init() node.Start() } + ``` + +* TestService1.go运行代码 +```go +package main + +import ( + "fmt" + + "github.com/duanhf2012/origin/cluster" + "github.com/duanhf2012/origin/originnode" + "github.com/duanhf2012/origin/service" +) + +type TestService1 struct { + service.BaseService +} + +func init() { + originnode.InitService(&TestService1{}) +} + +//OnInit ... +func (ws *TestService1) OnInit() error { + + return nil +} + +//OnRun ... +func (ws *TestService1) OnRun() bool { + var i InputData + var j int + i.A1 = 3 + i.A2 = 4 + err := cluster.Call("TestService2.RPC_Add", &i, &j) + if err == nil { + fmt.Printf(" TestService2.RPC_Add is %d\n", j) + } + + err = cluster.Call("TestService3.RPC_Multi", &i, &j) + if err == nil { + fmt.Printf(" TestService2.RPC_Multi is %d\n", j) + } + + return false +} + +``` + + + +* TestService2.go运行代码 +```go +package main + +import ( + "github.com/duanhf2012/origin/originnode" + "github.com/duanhf2012/origin/service" +) + +type InputData struct { + A1 int + A2 int +} + +type TestService2 struct { + service.BaseService +} + +func init() { + originnode.InitService(&TestService2{}) +} + +//OnInit ... +func (ws *TestService2) OnInit() error { + + return nil +} + +//OnRun ... +func (ws *TestService2) OnRun() bool { + return false +} + +//服务要对外的接口规划如下: +//RPC_MethodName(arg *DataType1, ret *DataType2) error +//如果不符合规范,在加载服务时,该函数将不会被映射,其他服务将不允能调用。 +func (slf *TestService2) RPC_Add(arg *InputData, ret *int) error { + + *ret = arg.A1 + arg.A2 + + return nil +} +``` + +* TestService3.go运行代码 +```go +package main + +import ( + "github.com/duanhf2012/origin/originnode" + "github.com/duanhf2012/origin/service" +) + +type TestService3 struct { + service.BaseService +} + +func init() { + originnode.InitService(&TestService3{}) +} + +//OnInit ... +func (ws *TestService3) OnInit() error { + return nil +} + +//OnRun ... +func (ws *TestService3) OnRun() bool { + return false +} + +func (slf *TestService3) RPC_Multi(arg *InputData, ret *int) error { + *ret = arg.A1 * arg.A2 + return nil +} +``` + + + 通过以下命令运行: ``` -main.exe NodeId=1 +OriginServer.exe NodeId=2 +OriginServer.exe NodeId=1 ``` -输出结果: +其中NodeId=1的进程输出结果: ``` -CTestService2.OnInit -CTestService1.OnInit -CTestService2.OnRun -CTestService1.OnRun -CTestService1.OnEndRun -CTestService2.OnRun -CTestService2.OnRun -CTestService2.OnRun -CTestService2.OnRun +TestService2.RPC_Add is 7 +TestService2.RPC_Multi is 12 ``` -通过日志可以确认,在Node启动时分别驱动Service的OnInit,OnRun,OnEndRun,上面的日志中CTestService2.OnRun会被循环调用, -因为在OnRun的返回是true,否则只会进入一次。如果你不需要OnRun可以不定义OnRun函数。我们已经成功的调用了两个服务了。 +通过日志可以确认,在Node启动时分别驱动Service的OnInit,OnRun,OnEndRun,上面的日志中TestService1.OnRun会被调用, +因为在OnRun的返回是false,所以OnRun只会进入一次。当返回true时,会重复循环进入OnRun。如果你不需要OnRun可以不定义OnRun函数。 +示例中,我们分别调用了本进程的TestService2服务中的RPC_Add的RPC接口,以及NodeId为2进程中服务为TestService3的接口RPC_Multi。 origin服务间通信: --------------- @@ -341,6 +455,7 @@ func main() { if node == nil { return } + //也可以通过以下方式安装服务CTestService1与CTestService2 node.SetupService(&CTestService1{}, &CTestService2{}) node.Init() node.Start() diff --git a/Test2/Helloworld/SubNet1_Service.go b/Test2/Helloworld/SubNet1_Service.go deleted file mode 100644 index 11f6a74..0000000 --- a/Test2/Helloworld/SubNet1_Service.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/duanhf2012/origin/cluster" - "github.com/duanhf2012/origin/originnode" - "github.com/duanhf2012/origin/service" -) - -type SubNet1_Service struct { - service.BaseService -} - -func init() { - originnode.InitService(&SubNet1_Service{}) -} - -//OnInit ... -func (ws *SubNet1_Service) OnInit() error { - - return nil -} - -//OnRun ... -func (ws *SubNet1_Service) OnRun() bool { - var in InputData - var ret int - in.A1 = 10 - in.A2 = 20 - err := cluster.Call("SubNet2_Service1.RPC_Multi", &in, &ret) - fmt.Printf("%+v", err) - return false -} diff --git a/Test2/Helloworld/SubNet1_Service1.go b/Test2/Helloworld/SubNet1_Service1.go deleted file mode 100644 index 88f7173..0000000 --- a/Test2/Helloworld/SubNet1_Service1.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "github.com/duanhf2012/origin/originnode" - "github.com/duanhf2012/origin/service" -) - -type InputData struct { - A1 int - A2 int -} - -type SubNet1_Service1 struct { - service.BaseService -} - -func init() { - originnode.InitService(&SubNet1_Service1{}) -} - -//OnInit ... -func (ws *SubNet1_Service1) OnInit() error { - - return nil -} - -//OnRun ... -func (ws *SubNet1_Service1) OnRun() bool { - return false -} - -//服务要对外的接口规划如下: -//RPC_MethodName(arg *DataType1, ret *DataType2) error -//如果不符合规范,在加载服务时,该函数将不会被映射,其他服务将不允能调用。 -func (slf *SubNet1_Service1) RPC_Add(arg *InputData, ret *int) error { - - *ret = arg.A1 + arg.A2 - - return nil -} diff --git a/Test2/Helloworld/SubNet1_Service2.go b/Test2/Helloworld/SubNet1_Service2.go deleted file mode 100644 index 83e87e1..0000000 --- a/Test2/Helloworld/SubNet1_Service2.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "github.com/duanhf2012/origin/originnode" - "github.com/duanhf2012/origin/service" -) - -type SubNet1_Service2 struct { - service.BaseService -} - -func init() { - originnode.InitService(&SubNet1_Service2{}) -} - -//OnInit ... -func (ws *SubNet1_Service2) OnInit() error { - return nil -} - -//OnRun ... -func (ws *SubNet1_Service2) OnRun() bool { - return false -} - -func (slf *SubNet1_Service2) RPC_Sub(arg *InputData, ret *int) error { - - *ret = arg.A1 - arg.A2 - - return nil -} diff --git a/Test2/Helloworld/SubNet2_Service1.go b/Test2/Helloworld/SubNet2_Service1.go deleted file mode 100644 index 78e7d54..0000000 --- a/Test2/Helloworld/SubNet2_Service1.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "github.com/duanhf2012/origin/originnode" - "github.com/duanhf2012/origin/service" -) - -type SubNet2_Service1 struct { - service.BaseService -} - -func init() { - originnode.InitService(&SubNet2_Service1{}) -} - -//OnInit ... -func (ws *SubNet2_Service1) OnInit() error { - return nil -} - -//OnRun ... -func (ws *SubNet2_Service1) OnRun() bool { - return false -} - -func (slf *SubNet2_Service1) RPC_Multi(arg *InputData, ret *int) error { - - *ret = arg.A1 * arg.A2 - - return nil -} diff --git a/Test2/Helloworld/config/cluster.json b/Test2/Helloworld/config/cluster.json index 9ccedf6..b515de2 100644 --- a/Test2/Helloworld/config/cluster.json +++ b/Test2/Helloworld/config/cluster.json @@ -8,38 +8,20 @@ "NodeID": 1, "NodeName": "N_Node1", "ServiceList": [ - "HttpServerService", - "SubNet1_Service", - "SubNet1_Service1" + "TestService1", + "TestService2" ], - "ClusterNode":["SubNet2.N_Node1","N_Node2"] + "ClusterNode":[] }, { "NodeID": 2, "NodeName": "N_Node2", "ServiceList": [ - "SubNet1_Service2" + "TestService3" ], "ClusterNode":[] } ] - }, - - - { - "Remark": "Manual,Full,Auto", - "SubNetMode": "Full", - "SubNetName": "SubNet2", - "PublicServiceList": ["logiclog"], - "NodeList": [{ - "NodeID": 3, - "NodeName": "N_Node1", - "ServiceList": [ - "SubNet2_Service1" - ], - "ClusterNode":["URQ.N_Node1"] - } - ] } ] } diff --git a/Test2/Helloworld/config/nodeconfig.json b/Test2/Helloworld/config/nodeconfig.json index b8cae4b..a09df0a 100644 --- a/Test2/Helloworld/config/nodeconfig.json +++ b/Test2/Helloworld/config/nodeconfig.json @@ -1,44 +1,20 @@ { -"Public":{ - "LogLevel":1, - "HttpPort":9400, - "WSPort":9500, - - "CAFile":[ - { - "CertFile":"", - "KeyFile":"" + "Public": { + "LogLevel": 1, + "HttpPort": 9400, + "WSPort": 9500, + "Environment": "Test", + "IsListenLog": 1, + "IsSendErrorMail": 0 }, - { - "CertFile":"", - "KeyFile":"" - } - ], - - "Environment":"FS_Dev_LFY", - "IsListenLog":1, - "IsSendErrorMail":0 -}, - -"NodeList":[ -{ - "NodeID":1, - "NodeAddr": "127.0.0.1:8081", - "LogLevel":1, - "HttpPort":7001, - "WSPort":7000, - "CertFile":"", - "KeyFile":"" -}, -{ - "NodeID":2, - "NodeAddr": "127.0.0.1:8082" -} -, -{ - "NodeID":3, - "NodeAddr": "127.0.0.1:8083" -} -] + "NodeList": [{ + "NodeID": 1, + "NodeAddr": "127.0.0.1:8081" + }, + { + "NodeID": 2, + "NodeAddr": "127.0.0.1:8082" + } + ] } \ No newline at end of file diff --git a/Test2/Helloworld/main.go b/Test2/Helloworld/main.go index 90842ef..8873e98 100644 --- a/Test2/Helloworld/main.go +++ b/Test2/Helloworld/main.go @@ -1,9 +1,7 @@ package main import ( - "github.com/duanhf2012/origin/cluster" "github.com/duanhf2012/origin/originnode" - "github.com/duanhf2012/origin/sysservice" ) func main() { @@ -13,14 +11,6 @@ func main() { return } - nodeCfg, _ := cluster.ReadNodeConfig("./config/nodeconfig.json", cluster.GetNodeId()) - httpserver := sysservice.NewHttpServerService(nodeCfg.HttpPort) // http服务 - for _, ca := range nodeCfg.CAFile { - httpserver.SetHttps(ca.CertFile, ca.KeyFile) - } - httpserver.SetPrintRequestTime(true) - - node.SetupService(httpserver) node.Init() node.Start() }