diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/gochat.iml b/.idea/gochat.iml new file mode 100644 index 00000000..5e764c4f --- /dev/null +++ b/.idea/gochat.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..20e5a752 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/rpc/rpc.go b/api/rpc/rpc.go index a8dfa405..131bc7ea 100644 --- a/api/rpc/rpc.go +++ b/api/rpc/rpc.go @@ -24,12 +24,21 @@ var RpcLogicObj *RpcLogic func InitLogicRpcClient() { once.Do(func() { - d := client.NewEtcdV3Discovery( - config.Conf.Common.CommonEtcd.BasePath, - config.Conf.Common.CommonEtcd.ServerPathLogic, - []string{config.Conf.Common.CommonEtcd.Host}, - nil, - ) + var d client.ServiceDiscovery + if config.Conf.Common.Registy == "etcd" { + d = client.NewEtcdV3Discovery( + config.Conf.Common.CommonEtcd.BasePath, + config.Conf.Common.CommonEtcd.ServerPathLogic, + []string{config.Conf.Common.CommonEtcd.Host}, + nil, + ) + } + if config.Conf.Common.Registy == "zookeeper" { + d = client.NewZookeeperDiscovery(config.Conf.Common.CommonZookeeper.BasePath, + config.Conf.Common.CommonZookeeper.ServerPathLogic, + []string{config.Conf.Common.CommonZookeeper.Host}, + nil) + } LogicRpcClient = client.NewXClient(config.Conf.Common.CommonEtcd.ServerPathLogic, client.Failtry, client.RandomSelect, d, client.DefaultOption) RpcLogicObj = new(RpcLogic) }) diff --git a/config/config.go b/config/config.go index 261ca3d2..d1b6741b 100644 --- a/config/config.go +++ b/config/config.go @@ -131,6 +131,12 @@ type CommonEtcd struct { ServerPathLogic string `mapstructure:"serverPathLogic"` ServerPathConnect string `mapstructure:"serverPathConnect"` } +type CommonZookeeper struct { + Host string `mapstructure:"host"` + BasePath string `mapstructure:"basePath"` + ServerPathLogic string `mapstructure:"serverPathLogic"` + ServerPathConnect string `mapstructure:"serverPathConnect"` +} type CommonRedis struct { RedisAddress string `mapstructure:"redisAddress"` @@ -139,8 +145,10 @@ type CommonRedis struct { } type Common struct { - CommonEtcd CommonEtcd `mapstructure:"common-etcd"` - CommonRedis CommonRedis `mapstructure:"common-redis"` + Registy string `mapstructure:"registy"` + CommonEtcd CommonEtcd `mapstructure:"common-etcd"` + CommonZookeeper CommonZookeeper `mapstructure:"common-zookeeper"` + CommonRedis CommonRedis `mapstructure:"common-redis"` } type ConnectBase struct { diff --git a/config/dev/common.toml b/config/dev/common.toml index 75fedb41..3fd1ff2c 100644 --- a/config/dev/common.toml +++ b/config/dev/common.toml @@ -1,9 +1,17 @@ +registy = "zookeeper" + [common-etcd] host = "127.0.0.1:2379" basePath = "/gochat_srv" serverPathLogic = "LogicRpc" serverPathConnect = "ConnectRpc" +[common-zookeeper] +host = "127.0.0.1:2181" +basePath = "/gochat_srv" +serverPathLogic = "LogicRpc" +serverPathConnect = "ConnectRpc" + [common-redis] redisAddress = "127.0.0.1:6379" redisPassword = "" diff --git a/config/prod/common.toml b/config/prod/common.toml index 75fedb41..3fd1ff2c 100644 --- a/config/prod/common.toml +++ b/config/prod/common.toml @@ -1,9 +1,17 @@ +registy = "zookeeper" + [common-etcd] host = "127.0.0.1:2379" basePath = "/gochat_srv" serverPathLogic = "LogicRpc" serverPathConnect = "ConnectRpc" +[common-zookeeper] +host = "127.0.0.1:2181" +basePath = "/gochat_srv" +serverPathLogic = "LogicRpc" +serverPathConnect = "ConnectRpc" + [common-redis] redisAddress = "127.0.0.1:6379" redisPassword = "" diff --git a/connect/rpc.go b/connect/rpc.go index 797626f7..f0d0b01e 100644 --- a/connect/rpc.go +++ b/connect/rpc.go @@ -30,12 +30,21 @@ type RpcConnect struct { func (c *Connect) InitLogicRpcClient() (err error) { once.Do(func() { - d := client.NewEtcdV3Discovery( - config.Conf.Common.CommonEtcd.BasePath, - config.Conf.Common.CommonEtcd.ServerPathLogic, - []string{config.Conf.Common.CommonEtcd.Host}, - nil, - ) + var d client.ServiceDiscovery + if config.Conf.Common.Registy == "etcd" { + d = client.NewEtcdV3Discovery( + config.Conf.Common.CommonEtcd.BasePath, + config.Conf.Common.CommonEtcd.ServerPathLogic, + []string{config.Conf.Common.CommonEtcd.Host}, + nil, + ) + } + if config.Conf.Common.Registy == "zookeeper" { + d = client.NewZookeeperDiscovery(config.Conf.Common.CommonZookeeper.BasePath, + config.Conf.Common.CommonZookeeper.ServerPathLogic, + []string{config.Conf.Common.CommonZookeeper.Host}, + nil) + } logicRpcClient = client.NewXClient(config.Conf.Common.CommonEtcd.ServerPathLogic, client.Failtry, client.RandomSelect, d, client.DefaultOption) }) if logicRpcClient == nil { @@ -168,16 +177,34 @@ func (c *Connect) createConnectTcpRpcServer(network string, addr string) { } func addRegistryPlugin(s *server.Server, network string, addr string) { - r := &serverplugin.EtcdV3RegisterPlugin{ - ServiceAddress: network + "@" + addr, - EtcdServers: []string{config.Conf.Common.CommonEtcd.Host}, - BasePath: config.Conf.Common.CommonEtcd.BasePath, - Metrics: metrics.NewRegistry(), - UpdateInterval: time.Minute, + + if config.Conf.Common.Registy == "etcd" { + r := &serverplugin.EtcdV3RegisterPlugin{ + ServiceAddress: network + "@" + addr, + EtcdServers: []string{config.Conf.Common.CommonEtcd.Host}, + BasePath: config.Conf.Common.CommonEtcd.BasePath, + Metrics: metrics.NewRegistry(), + UpdateInterval: time.Minute, + } + err := r.Start() + if err != nil { + logrus.Fatal(err) + } + s.Plugins.Add(r) } - err := r.Start() - if err != nil { - logrus.Fatal(err) + if config.Conf.Common.Registy == "zookeeper" { + r := &serverplugin.ZooKeeperRegisterPlugin{ + ServiceAddress: network + "@" + addr, + ZooKeeperServers: []string{config.Conf.Common.CommonZookeeper.Host}, + BasePath: config.Conf.Common.CommonZookeeper.BasePath, + Metrics: metrics.NewRegistry(), + UpdateInterval: time.Minute, + } + err := r.Start() + if err != nil { + logrus.Fatal(err) + } + s.Plugins.Add(r) } - s.Plugins.Add(r) + } diff --git a/db/gochat.sqlite3 b/db/gochat.sqlite3 index e581a0cf..14c44767 100644 Binary files a/db/gochat.sqlite3 and b/db/gochat.sqlite3 differ diff --git a/go.mod b/go.mod index adc963ab..8b6c2eda 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/bwmarrin/snowflake v0.3.0 github.com/gin-gonic/gin v1.4.0 github.com/go-redis/redis v6.15.2+incompatible + github.com/google/uuid v1.1.1 // indirect github.com/gorilla/websocket v1.4.0 github.com/jinzhu/gorm v1.9.10 github.com/pkg/errors v0.8.1 diff --git a/logic/publish.go b/logic/publish.go index 39e319dd..5954313e 100644 --- a/logic/publish.go +++ b/logic/publish.go @@ -69,18 +69,35 @@ func (logic *Logic) createRpcServer(network string, addr string) { } func (logic *Logic) addRegistryPlugin(s *server.Server, network string, addr string) { - r := &serverplugin.EtcdV3RegisterPlugin{ - ServiceAddress: network + "@" + addr, - EtcdServers: []string{config.Conf.Common.CommonEtcd.Host}, - BasePath: config.Conf.Common.CommonEtcd.BasePath, - Metrics: metrics.NewRegistry(), - UpdateInterval: time.Minute, - } - err := r.Start() - if err != nil { - logrus.Fatal(err) + if config.Conf.Common.Registy == "etcd" { + r := &serverplugin.EtcdV3RegisterPlugin{ + ServiceAddress: network + "@" + addr, + EtcdServers: []string{config.Conf.Common.CommonEtcd.Host}, + BasePath: config.Conf.Common.CommonEtcd.BasePath, + Metrics: metrics.NewRegistry(), + UpdateInterval: time.Minute, + } + err := r.Start() + if err != nil { + logrus.Fatal(err) + } + s.Plugins.Add(r) + } + if config.Conf.Common.Registy == "zookeeper" { + r := &serverplugin.ZooKeeperRegisterPlugin{ + ServiceAddress: network + "@" + addr, + ZooKeeperServers: []string{config.Conf.Common.CommonZookeeper.Host}, + BasePath: config.Conf.Common.CommonZookeeper.BasePath, + Metrics: metrics.NewRegistry(), + UpdateInterval: time.Minute, + } + err := r.Start() + if err != nil { + logrus.Fatal(err) + } + s.Plugins.Add(r) } - s.Plugins.Add(r) + } func (logic *Logic) RedisPublishChannel(serverId string, toUserId int, msg []byte) (err error) { diff --git a/main.go b/main.go index 99c72877..d4f6f74c 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ import ( "gochat/logic" "gochat/site" "gochat/task" + + //"gochat/task" "os" "os/signal" "syscall" diff --git a/task/rpc.go b/task/rpc.go index 6a3c78b4..a329a5ab 100644 --- a/task/rpc.go +++ b/task/rpc.go @@ -19,8 +19,31 @@ import ( var RpcConnectClientList map[string]client.XClient func (task *Task) InitConnectRpcClient() (err error) { - etcdConfig := config.Conf.Common.CommonEtcd - d := client.NewEtcdV3Discovery(etcdConfig.BasePath, etcdConfig.ServerPathConnect, []string{etcdConfig.Host}, nil) + //etcdConfig := config.Conf.Common.CommonEtcd + //zookkperConfig := config.Conf.Common.CommonZookeeper + ////d := client.NewEtcdV3Discovery(etcdConfig.BasePath, etcdConfig.ServerPathConnect, []string{etcdConfig.Host}, nil) + //d := client.NewZookeeperDiscovery(zookkperConfig.BasePath, zookkperConfig.ServerPathConnect, []string{zookkperConfig.Host}, nil) + + + var d client.ServiceDiscovery + var ServerPathConnect string + if config.Conf.Common.Registy == "etcd" { + ServerPathConnect=config.Conf.Common.CommonEtcd.ServerPathConnect + d = client.NewEtcdV3Discovery( + config.Conf.Common.CommonEtcd.BasePath, + config.Conf.Common.CommonEtcd.ServerPathConnect, + []string{config.Conf.Common.CommonEtcd.Host}, + nil, + ) + } + if config.Conf.Common.Registy == "zookeeper" { + ServerPathConnect=config.Conf.Common.CommonZookeeper.ServerPathConnect + d = client.NewZookeeperDiscovery(config.Conf.Common.CommonZookeeper.BasePath, + config.Conf.Common.CommonZookeeper.ServerPathConnect, + []string{config.Conf.Common.CommonZookeeper.Host}, + nil) + } + if len(d.GetServices()) <= 0 { logrus.Panicf("no etcd server find!") } @@ -35,7 +58,7 @@ func (task *Task) InitConnectRpcClient() (err error) { } d := client.NewPeer2PeerDiscovery(connectConf.Key, "") //under serverId - RpcConnectClientList[serverId] = client.NewXClient(etcdConfig.ServerPathConnect, client.Failtry, client.RandomSelect, d, client.DefaultOption) + RpcConnectClientList[serverId] = client.NewXClient(ServerPathConnect, client.Failtry, client.RandomSelect, d, client.DefaultOption) logrus.Infof("InitConnectRpcClient addr %s, v %+v", connectConf.Key, RpcConnectClientList[serverId]) } return