-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.go
67 lines (57 loc) · 1.4 KB
/
socket.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
package socket
import (
"fmt"
"github.com/hulutech-web/goravel-socket/define"
"github.com/hulutech-web/goravel-socket/pkg/etcd"
"github.com/hulutech-web/goravel-socket/servers"
"github.com/hulutech-web/goravel-socket/setting"
"github.com/hulutech-web/goravel-socket/tools/log"
"github.com/hulutech-web/goravel-socket/tools/util"
"net"
)
type Socket struct{}
func init() {
setting.Setup()
log.Setup()
Boot()
}
func Boot() {
//初始化RPC服务
initRPCServer()
//将服务器地址、端口注册到etcd中
registerServer()
//启动一个定时器用来发送心跳
servers.PingTimer()
}
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)
}
}
}