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

72 lines
2.6 KiB

2 years ago
package startrpc
import (
"fmt"
2 years ago
"net"
"strconv"
2 years ago
"time"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"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"
1 year ago
openKeeper "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry/zookeeper"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
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, "server, port: ", rpcPort, "prometheusPort:", prometheusPort, ", OpenIM version: ", config.Version)
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
zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName,
config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10))
2 years ago
if err != nil {
2 years ago
return utils.Wrap1(err)
2 years ago
}
2 years ago
defer zkClient.CloseZK()
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
// ctx 中间件
2 years ago
if config.Config.Prometheus.Enable {
2 years ago
prome.NewGrpcRequestCounter()
prome.NewGrpcRequestFailedCounter()
prome.NewGrpcRequestSuccessCounter()
2 years ago
unaryInterceptor := mw.InterceptChain(grpcPrometheus.UnaryServerInterceptor, mw.RpcServerInterceptor)
2 years ago
options = append(options, []grpc.ServerOption{
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
2 years ago
grpc.UnaryInterceptor(unaryInterceptor),
2 years ago
}...)
2 years ago
} else {
options = append(options, mw.GrpcServer())
2 years ago
}
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
}