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/pkg/startrpc/start.go

71 lines
2.6 KiB

2 years ago
package startrpc
import (
"fmt"
2 years ago
"net"
"strconv"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/network"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"github.com/OpenIMSDK/openKeeper"
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
2 years ago
"google.golang.org/grpc/credentials/insecure"
2 years ago
)
2 years ago
func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, options ...grpc.ServerOption) error {
2 years ago
fmt.Println("start", rpcRegisterName, "rpc server, port: ", rpcPort, "prometheusPort:", prometheusPort, ", OpenIM version: ", config.Version)
2 years ago
log.NewPrivateLog(constant.LogFileName)
2 years ago
listener, err := net.Listen("tcp", net.JoinHostPort(config.Config.ListenIP, strconv.Itoa(rpcPort)))
2 years ago
if err != nil {
2 years ago
return err
2 years ago
}
defer listener.Close()
2 years ago
fmt.Println(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, rpcRegisterName)
2 years ago
zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, 10, "", "")
if err != nil {
2 years ago
return utils.Wrap1(err)
2 years ago
}
2 years ago
defer zkClient.Close()
zkClient.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials()))
2 years ago
registerIP, err := network.GetRpcRegisterIP(config.Config.RpcRegisterIP)
if err != nil {
2 years ago
return err
2 years ago
}
2 years ago
options = append(options, mw.GrpcServer()) // ctx 中间件
2 years ago
if config.Config.Prometheus.Enable {
2 years ago
prome.NewGrpcRequestCounter()
prome.NewGrpcRequestFailedCounter()
prome.NewGrpcRequestSuccessCounter()
2 years ago
options = append(options, []grpc.ServerOption{
2 years ago
//grpc.UnaryInterceptor(prome.UnaryServerInterceptorPrometheus),
2 years ago
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
}
srv := grpc.NewServer(options...)
defer srv.GracefulStop()
2 years ago
err = rpcFn(zkClient, srv)
if err != nil {
return utils.Wrap1(err)
}
2 years ago
err = zkClient.Register(rpcRegisterName, registerIP, rpcPort, grpc.WithTransportCredentials(insecure.NewCredentials()))
2 years ago
if err != nil {
2 years ago
return utils.Wrap1(err)
2 years ago
}
2 years ago
go func() {
if config.Config.Prometheus.Enable && prometheusPort != 0 {
if err := prome.StartPrometheusSrv(prometheusPort); err != nil {
panic(err.Error())
}
2 years ago
}
2 years ago
}()
2 years ago
return utils.Wrap1(srv.Serve(listener))
2 years ago
}