| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package redis
- import (
- "context"
- "reflect"
- cfacade "github.com/cherry-game/cherry/facade"
- clog "github.com/cherry-game/cherry/logger"
- cprofile "github.com/cherry-game/cherry/profile"
- "github.com/go-redis/redis/v8"
- )
- type (
- Component struct {
- cfacade.Component
- clientList []IClient
- }
- )
- func New() *Component {
- return &Component{
- clientList: allList(),
- }
- }
- func NewClients(clientList ...IClient) *Component {
- return &Component{
- clientList: clientList,
- }
- }
- func (*Component) Name() string {
- return "redis_component"
- }
- func (p *Component) Init() {
- if len(p.clientList) < 1 {
- clog.Fatalf("[nodeID = %s] redis client list is empty.", p.App().NodeID())
- return
- }
- rdbIDList := p.App().Settings().Get("redis_id_list")
- if rdbIDList.LastError() != nil || rdbIDList.Size() < 1 {
- clog.Fatalf("[nodeID = %s] `redis_id_list` property not exists.", p.App().NodeID())
- return
- }
- // 从profile-xx.json 获取所有redis节点配置信息
- redisConfigList := cprofile.GetConfig("redis")
- if redisConfigList.LastError() != nil {
- clog.Fatal("`redis` property not exists.")
- return
- }
- // redis客户端实例 key:configID, value:*redis.Client
- clientMap := map[string]*client{}
- // 根据当前节点redis的配置构造 *redis.Client
- for _, configID := range rdbIDList.Keys() {
- if _, found := clientMap[configID]; found {
- continue
- }
- redisID := rdbIDList.Get(configID).ToString()
- if len(redisID) < 1 {
- clog.Fatalf("RedisID not found. [configID = %s]", configID)
- return
- }
- redisConfig := GetConfigWithRedisID(redisConfigList, redisID)
- if redisConfig == nil {
- clog.Fatalf("Redis config not found. [configID = %s, redisID = %s]", configID, redisID)
- return
- }
- // add
- clientMap[configID] = p.newClient(redisConfig)
- clog.Infof("New redis *client [configID = %s, redisID = %s]", configID, redisID)
- }
- // 根据自定义redis实例注入*client
- for _, instance := range p.clientList {
- client, found := clientMap[instance.ConfigID()]
- if !found {
- clog.Fatalf("Redis config not found. [configID = %s]", instance.ConfigID())
- return
- }
- instance.load(client)
- clog.Infof("Load [type = %v, configID = %s, redisID = %s]",
- reflect.TypeOf(instance).String(),
- instance.ConfigID(),
- instance.RedisID(),
- )
- }
- }
- func (*Component) OnStop() {
- }
- func (p *Component) OnAfterInit() {
- }
- func GetConfigWithRedisID(allList cfacade.ProfileJSON, redisID string) cfacade.ProfileJSON {
- for i := 0; i < allList.Size(); i++ {
- item := allList.GetConfig(i)
- if item.Get("redis_id").ToString() == redisID {
- return item
- }
- }
- return nil
- }
- func NewClientOption(item cfacade.ProfileJSON) (string, *redis.Options) {
- redisID := item.GetString("redis_id")
- address := item.GetString("address")
- password := item.GetString("password")
- db := item.GetInt("db")
- poolSize := item.GetInt("pool_size", 32)
- options := &redis.Options{
- Addr: address,
- Password: password,
- DB: db,
- PoolSize: poolSize,
- OnConnect: func(ctx context.Context, cn *redis.Conn) error {
- clog.Infof("[redisID = %s] connected! [address = %s]", redisID, address)
- return nil
- },
- }
- return redisID, options
- }
- func (p *Component) newClient(item cfacade.ProfileJSON) *client {
- redisID, options := NewClientOption(item)
- return newClient(redisID, options)
- }
|