Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
withchao committed Jul 6, 2023
1 parent aa8044d commit 352dbc9
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/api/admin_api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
panic(err)
}
zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName,
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 {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpc/admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/startrpc"
"github.com/OpenIMSDK/chat/pkg/common/chatrpcstart"

"github.com/OpenIMSDK/chat/internal/rpc/admin"
"github.com/OpenIMSDK/chat/pkg/common/config"
Expand All @@ -29,7 +29,7 @@ func main() {
if err := log.InitFromConfig("chat.log", "admin-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount); err != nil {
panic(err)
}
err := startrpc.Start(config.Config.RpcPort.OpenImAdminPort[0], config.Config.RpcRegisterName.OpenImAdminName, 0, admin.Start)
err := chatrpcstart.Start(config.Config.RpcPort.OpenImAdminPort[0], config.Config.RpcRegisterName.OpenImAdminName, 0, admin.Start)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpc/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/startrpc"
"github.com/OpenIMSDK/chat/pkg/common/chatrpcstart"

"github.com/OpenIMSDK/chat/internal/rpc/chat"
"github.com/OpenIMSDK/chat/pkg/common/config"
Expand All @@ -29,7 +29,7 @@ func main() {
if err := log.InitFromConfig("chat.log", "chat-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount); err != nil {
panic(err)
}
err := startrpc.Start(config.Config.RpcPort.OpenImChatPort[0], config.Config.RpcRegisterName.OpenImChatName, 0, chat.Start)
err := chatrpcstart.Start(config.Config.RpcPort.OpenImChatPort[0], config.Config.RpcRegisterName.OpenImChatName, 0, chat.Start)
if err != nil {
panic(err)
}
Expand Down
6 changes: 5 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ zookeeper:
schema: openim
zkAddr:
- 127.0.0.1:2181
userName: ""
username: ""
password: ""

chatApi:
Expand All @@ -28,6 +28,10 @@ adminApi:
openImAdminApiPort: [ 10009 ]
listenIP:

rpc:
registerIP: #作为rpc启动时,注册到zookeeper的IP,api/gateway能访问到此ip和对应的rpcPort中的端口
listenIP: #默认为0.0.0.0

rpcPort:
openImAdminPort: [ 30200 ]
openImChatPort: [ 30300 ]
Expand Down
1 change: 1 addition & 0 deletions internal/rpc/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func Start(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
if err := discov.CreateRpcRootNodes([]string{config.Config.RpcRegisterName.OpenImAdminName, config.Config.RpcRegisterName.OpenImChatName}); err != nil {
panic(err)
}

admin.RegisterAdminServer(server, &adminServer{
Database: database.NewAdminDatabase(db),
Chat: chat.NewChatClient(discov),
Expand Down
51 changes: 51 additions & 0 deletions pkg/common/chatrpcstart/start.go
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))
}
6 changes: 5 additions & 1 deletion pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Config struct {
Zookeeper struct {
Schema string `yaml:"schema"`
ZkAddr []string `yaml:"zkAddr"`
UserName string `yaml:"userName"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"zookeeper"`
ChatApi struct {
Expand All @@ -34,6 +34,10 @@ var Config struct {
GinPort []int `yaml:"openImAdminApiPort"`
ListenIP string `yaml:"listenIP"`
} `yaml:"adminApi"`
Rpc struct {
RegisterIP string `yaml:"registerIP"`
ListenIP string `yaml:"listenIP"`
} `yaml:"rpc"`
RpcPort struct {
OpenImAdminPort []int `yaml:"openImAdminPort"`
OpenImChatPort []int `yaml:"openImChatPort"`
Expand Down
24 changes: 24 additions & 0 deletions pkg/common/mw/user.go
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...)
})
}

0 comments on commit 352dbc9

Please sign in to comment.