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

68 lines
2.4 KiB

2 years ago
package startrpc
import (
2 years ago
"OpenIM/internal/common/network"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/middleware"
promePkg "OpenIM/pkg/common/prome"
2 years ago
"flag"
"fmt"
"github.com/OpenIMSDK/openKeeper"
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
"net"
)
2 years ago
func start(rpcPorts []int, rpcRegisterName string, prometheusPorts []int, rpcFn func(client *openKeeper.ZkClient, server *grpc.Server) error, options []grpc.ServerOption) error {
flagRpcPort := flag.Int("port", rpcPorts[0], "get RpcGroupPort from cmd,default 16000 as port")
flagPrometheusPort := flag.Int("prometheus_port", prometheusPorts[0], "groupPrometheusPort default listen port")
2 years ago
flag.Parse()
2 years ago
fmt.Println("start group rpc server, port: ", *flagRpcPort, ", OpenIM version: ", constant.CurrentVersion)
2 years ago
log.NewPrivateLog(constant.LogFileName)
2 years ago
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.Config.ListenIP, *flagRpcPort))
2 years ago
if err != nil {
2 years ago
return err
2 years ago
}
defer listener.Close()
zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, 10, "", "")
if err != nil {
2 years ago
return err
2 years ago
}
2 years ago
defer zkClient.Close()
2 years ago
registerIP, err := network.GetRpcRegisterIP(config.Config.RpcRegisterIP)
if err != nil {
2 years ago
return err
2 years ago
}
options = append(options, grpc.UnaryInterceptor(middleware.RpcServerInterceptor)) // ctx 中间件
if config.Config.Prometheus.Enable {
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
options = append(options, []grpc.ServerOption{
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
}
srv := grpc.NewServer(options...)
defer srv.GracefulStop()
2 years ago
err = zkClient.Register(rpcRegisterName, registerIP, *flagRpcPort)
2 years ago
if err != nil {
2 years ago
return err
2 years ago
}
if config.Config.Prometheus.Enable {
2 years ago
err := promePkg.StartPromeSrv(*flagPrometheusPort)
2 years ago
if err != nil {
2 years ago
return err
2 years ago
}
}
2 years ago
return rpcFn(zkClient, srv)
2 years ago
}
2 years ago
func Start(rpcPorts []int, rpcRegisterName string, prometheusPorts []int, rpcFn func(client *openKeeper.ZkClient, server *grpc.Server) error, options ...grpc.ServerOption) {
err := start(rpcPorts, rpcRegisterName, prometheusPorts, rpcFn, options)
2 years ago
fmt.Println("end", err)
2 years ago
}