You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/msg/rpcChat.go

67 lines
1.7 KiB

package msg
4 years ago
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/kafka"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbChat "Open_IM/pkg/proto/chat"
3 years ago
"Open_IM/pkg/utils"
"google.golang.org/grpc"
4 years ago
"net"
"strconv"
"strings"
)
type rpcChat struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
producer *kafka.Producer
}
func NewRpcChatServer(port int) *rpcChat {
log.NewPrivateLog("msg")
4 years ago
rc := rpcChat{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImOfflineMessageName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
rc.producer = kafka.NewKafkaProducer(config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.Ws2mschat.Topic)
return &rc
}
func (rpc *rpcChat) Run() {
log.Info("", "", "rpc get_token init...")
4 years ago
3 years ago
address := utils.ServerIP + ":" + strconv.Itoa(rpc.rpcPort)
4 years ago
listener, err := net.Listen("tcp", address)
if err != nil {
log.Error("", "", "listen network failed, err = %s, address = %s", err.Error(), address)
4 years ago
return
}
log.Info("", "", "listen network success, address = %s", address)
4 years ago
//grpc server
srv := grpc.NewServer()
defer srv.GracefulStop()
//service registers with etcd
pbChat.RegisterChatServer(srv, rpc)
3 years ago
err = getcdv3.RegisterEtcd(rpc.etcdSchema, strings.Join(rpc.etcdAddr, ","), utils.ServerIP, rpc.rpcPort, rpc.rpcRegisterName, 10)
4 years ago
if err != nil {
log.Error("", "", "register rpc get_token to etcd failed, err = %s", err.Error())
4 years ago
return
}
err = srv.Serve(listener)
if err != nil {
log.Info("", "", "rpc get_token fail, err = %s", err.Error())
4 years ago
return
}
log.Info("", "", "rpc get_token init success")
4 years ago
}