-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
91 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package chatrpcstart | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" | ||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw" | ||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/network" | ||
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" | ||
openKeeper "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry/zookeeper" | ||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs" | ||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils" | ||
"github.com/OpenIMSDK/chat/pkg/common/config" | ||
chatMw "github.com/OpenIMSDK/chat/pkg/common/mw" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"net" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, options ...grpc.ServerOption) error { | ||
fmt.Println("start", rpcRegisterName, "server, port: ", rpcPort, "prometheusPort:", prometheusPort, ", OpenIM version: ", config.Version) | ||
listener, err := net.Listen("tcp", net.JoinHostPort(network.GetListenIP(config.Config.Rpc.ListenIP), strconv.Itoa(rpcPort))) | ||
if err != nil { | ||
return err | ||
} | ||
defer listener.Close() | ||
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), openKeeper.WithLogger(log.NewZkLogger())) | ||
if err != nil { | ||
return utils.Wrap1(err) | ||
} | ||
defer zkClient.CloseZK() | ||
zkClient.AddOption(chatMw.AddUserType(), mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
registerIP, err := network.GetRpcRegisterIP(config.Config.Rpc.RegisterIP) | ||
if err != nil { | ||
return err | ||
} | ||
srv := grpc.NewServer(append(options, mw.GrpcServer())...) | ||
defer srv.GracefulStop() | ||
err = rpcFn(zkClient, srv) | ||
if err != nil { | ||
return errs.Wrap(err) | ||
} | ||
err = zkClient.Register(rpcRegisterName, registerIP, rpcPort, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return utils.Wrap1(err) | ||
} | ||
return utils.Wrap1(srv.Serve(listener)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package mw | ||
|
||
import ( | ||
"context" | ||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs" | ||
"github.com/OpenIMSDK/chat/pkg/common/constant" | ||
"google.golang.org/grpc" | ||
"strconv" | ||
) | ||
|
||
func AddUserType() grpc.DialOption { | ||
return grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
if arr, _ := ctx.Value(constant.RpcOpUserType).([]string); len(arr) > 0 { | ||
userType, err := strconv.Atoi(arr[0]) | ||
if err != nil { | ||
return errs.ErrInternalServer.Wrap("user type is not int") | ||
} | ||
headers, _ := ctx.Value(constant.RpcCustomHeader).([]string) | ||
ctx = context.WithValue(ctx, constant.RpcCustomHeader, append(headers, constant.RpcOpUserType)) | ||
ctx = context.WithValue(ctx, constant.RpcOpUserType, userType) | ||
} | ||
return invoker(ctx, method, req, reply, cc, opts...) | ||
}) | ||
} |