-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathmain.go
75 lines (63 loc) · 1.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/woodylan/go-websocket/define"
"github.com/woodylan/go-websocket/pkg/etcd"
"github.com/woodylan/go-websocket/pkg/setting"
"github.com/woodylan/go-websocket/routers"
"github.com/woodylan/go-websocket/servers"
"github.com/woodylan/go-websocket/tools/log"
"github.com/woodylan/go-websocket/tools/util"
"net"
"net/http"
)
func init() {
setting.Setup()
log.Setup()
}
func main() {
//初始化RPC服务
initRPCServer()
//将服务器地址、端口注册到etcd中
registerServer()
//初始化路由
routers.Init()
//启动一个定时器用来发送心跳
servers.PingTimer()
fmt.Printf("服务器启动成功,端口号:%s\n", setting.CommonSetting.HttpPort)
if err := http.ListenAndServe(":"+setting.CommonSetting.HttpPort, nil); err != nil {
panic(err)
}
}
func initRPCServer() {
//如果是集群,则启用RPC进行通讯
if util.IsCluster() {
//初始化RPC服务
servers.InitGRpcServer()
fmt.Printf("启动RPC,端口号:%s\n", setting.CommonSetting.RPCPort)
}
}
//ETCD注册发现服务
func registerServer() {
if util.IsCluster() {
//注册租约
ser, err := etcd.NewServiceReg(setting.EtcdSetting.Endpoints, 5)
if err != nil {
panic(err)
}
hostPort := net.JoinHostPort(setting.GlobalSetting.LocalHost, setting.CommonSetting.RPCPort)
//添加key
err = ser.PutService(define.ETCD_SERVER_LIST+hostPort, hostPort)
if err != nil {
panic(err)
}
cli, err := etcd.NewClientDis(setting.EtcdSetting.Endpoints)
if err != nil {
panic(err)
}
_, err = cli.GetService(define.ETCD_SERVER_LIST)
if err != nil {
panic(err)
}
}
}