redis.go 3.0 KB
Newer Older
Y
Your Name 已提交
1
package consolemysql
E
eoLinker API Management 已提交
2 3

import (
黄孟柱 已提交
4 5 6 7
	database2 "github.com/eolinker/goku-api-gateway/common/database"
	"github.com/eolinker/goku-api-gateway/common/general"
	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/server/entity/console-entity"
E
eoLinker API Management 已提交
8 9 10 11 12 13 14
	"strings"
)

func init() {
	general.RegeditLater(CreateTable)
}

Y
Your Name 已提交
15 16
//GetServers 获取redis服务列表
func GetServers(clusterID int) ([]*entity.RedisNode, error) {
E
eoLinker API Management 已提交
17 18

	db := database2.GetConnection()
Y
Your Name 已提交
19
	rows, err := db.Query("SELECT `server`,`password`,`clusterID` from `goku_redis_server` WHERE `clusterID`=?;", clusterID)
E
eoLinker API Management 已提交
20 21 22 23 24 25 26 27 28
	if err != nil {
		return nil, err
	}

	defer rows.Close()

	servers := make([]*entity.RedisNode, 0)
	for rows.Next() {
		node := new(entity.RedisNode)
Y
Your Name 已提交
29
		err := rows.Scan(&node.Server, &node.Password, &node.ClusterID)
E
eoLinker API Management 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

		if err != nil {
			return nil, err
		}
		servers = append(servers, node)
	}
	return servers, nil
}

// GetRedisCount 获取redis数量
func GetRedisCount() (int, int) {
	sql := "SELECT status,COUNT(*) FROM goku_redis_server GROUP BY status;"
	rows, err := database2.GetConnection().Query(sql)
	if err != nil {
		return 0, 0
	}
	var normalCount, errorCount int
	defer rows.Close()
	for rows.Next() {
		var status, count int
		err := rows.Scan(&status, &count)
		if err != nil {
			return 0, 0
		}
		if status == 0 {
			normalCount = count
		} else {
			errorCount = count
		}
	}
	return normalCount, errorCount
}

Y
Your Name 已提交
63
//CreateTable 创建表
E
eoLinker API Management 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
func CreateTable() error {
	sqlDatas := []string{

		`CREATE TABLE  IF NOT EXISTS '''goku_redis_info''' (
  '''id''' int(11) unsigned NOT NULL AUTO_INCREMENT,
  '''server''' varchar(20) NOT NULL COMMENT 'ip:port',
  '''info''' text NOT NULL COMMENT 'info,json',
  '''datetime''' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY ('''id'''),
  KEY '''server''' ('''server''')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
		`CREATE TABLE IF NOT EXISTS '''goku_redis_memory''' (
  '''id''' int(11) unsigned NOT NULL AUTO_INCREMENT,
  '''server''' varchar(20) DEFAULT NULL COMMENT 'ip:port',
  '''used''' int(11) DEFAULT NULL,
  '''peak''' int(11) DEFAULT NULL,
  '''datetime''' timestamp NULL DEFAULT NULL,
  PRIMARY KEY ('''id'''),
  KEY '''server''' ('''server''')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
		`CREATE TABLE  IF NOT EXISTS '''goku_redis_server''' (
  '''id''' INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  '''server''' VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'ip:port',
  '''password''' VARCHAR(20) DEFAULT NULL,
  '''clusterID''' INT(11) DEFAULT '1',
  '''status''' INT(11) DEFAULT NULL,
  PRIMARY KEY ('''id'''),
  UNIQUE KEY '''server''' ('''server''','''clusterID'''),
  KEY '''clusterID''' ('''clusterID'''),
  KEY '''status''' ('''status''')
) ENGINE=INNODB AUTO_INCREMENT=224 DEFAULT CHARSET=utf8;`}
	for _, sql := range sqlDatas {
		sqlData := strings.ReplaceAll(sql, "'''", "`")
		db := database2.GetConnection()
		_, err := db.Exec(sqlData)
		if err != nil {
			log.Info(err.Error())
			return err
		}
	}

	return nil
}