Commit 47fafa0f2ffed8c54490a3ba88028c000cb24761

Authored by 陆恒
1 parent 6acf04ad
Exists in master

提交测试

src/HttpServer/conf/conf.go
@@ -15,6 +15,7 @@ type RedisConf struct { @@ -15,6 +15,7 @@ type RedisConf struct {
15 } 15 }
16 16
17 type TexasConf struct { 17 type TexasConf struct {
  18 + GameDB MysqlConf `xml:"GameDB"`
18 ServerHttpAddr ServerHttpAddrConf `xml:"ServerHttpAddr"` 19 ServerHttpAddr ServerHttpAddrConf `xml:"ServerHttpAddr"`
19 Redis RedisConf `xml:"Redis"` 20 Redis RedisConf `xml:"Redis"`
20 } 21 }
@@ -26,6 +27,18 @@ var ( @@ -26,6 +27,18 @@ var (
26 func GetRedisConf() RedisConf { 27 func GetRedisConf() RedisConf {
27 return config.Redis 28 return config.Redis
28 } 29 }
  30 +type MysqlConf struct {
  31 + Ip string `xml:",attr"`
  32 + Port int `xml:",attr"`
  33 + User string `xml:",attr"`
  34 + Pwd string `xml:",attr"`
  35 + Database string `xml:",attr"`
  36 + LoadInterval int `xml:",attr"`
  37 +}
  38 +
  39 +func GetGameDBConf() MysqlConf {
  40 + return config.GameDB
  41 +}
29 42
30 func LoadConf(filename string) error { 43 func LoadConf(filename string) error {
31 content, err := ioutil.ReadFile(filename) 44 content, err := ioutil.ReadFile(filename)
src/HttpServer/conf/world.xml
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <Config> 2 <Config>
  3 + <GameDB Ip="172.21.0.34" Port="3306" User="mini_game_db" Pwd="TXsql@VFhzcWxAV1Z" Database="cat_cafe" LoadInterval="5" />
3 <ServerHttpAddr Host="127.0.0.1:50057" /> 4 <ServerHttpAddr Host="127.0.0.1:50057" />
4 - <Redis Host="172.21.0.25:6379" Db="6" Password="crs-lzslccdc:redis@YXp8Jk#MV" /> 5 + <Redis Host="172.21.0.25:6379" Db="27" Password="crs-lzslccdc:redis@YXp8Jk#MV" />
5 </Config> 6 </Config>
6 \ No newline at end of file 7 \ No newline at end of file
src/HttpServer/main/main.go
@@ -2,6 +2,7 @@ package main @@ -2,6 +2,7 @@ package main
2 2
3 import ( 3 import (
4 "HttpServer/conf" 4 "HttpServer/conf"
  5 + "mysql"
5 6
6 "HttpServer/logic" 7 "HttpServer/logic"
7 "HttpServer/redishandler" 8 "HttpServer/redishandler"
@@ -49,9 +50,16 @@ func main() { @@ -49,9 +50,16 @@ func main() {
49 50
50 err = redishandler.Init() 51 err = redishandler.Init()
51 if err != nil { 52 if err != nil {
52 - logger.Info("err init redis err=%v", err) 53 + logger.Error("err init redis err=%v", err)
53 return 54 return
54 } 55 }
  56 +
  57 + err = mysql.InitMysql()
  58 + if err != nil {
  59 + logger.Error("err init mysql err=%v", err)
  60 + return
  61 + }
  62 +
55 //測試 63 //測試
56 logic.Test() 64 logic.Test()
57 go logic.StartHttpServe() 65 go logic.StartHttpServe()
src/mysql/dbmysql.go 0 → 100644
@@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
  1 +package mysql
  2 +
  3 +import (
  4 + "HttpServer/conf"
  5 + "common/logger"
  6 + "database/sql"
  7 + "strconv"
  8 +)
  9 +
  10 +var (
  11 + m_game_db *sql.DB
  12 +)
  13 +
  14 +func InitMysql() error {
  15 + db, err := InitMysqlByConf(conf.GetGameDBConf())
  16 + if err != nil {
  17 + return err
  18 + }
  19 + m_game_db = db
  20 + return nil
  21 +}
  22 +
  23 +func InitMysqlByConf(cfg conf.MysqlConf) (*sql.DB, error) {
  24 + url := cfg.User + ":" + cfg.Pwd + "@tcp(" + cfg.Ip + ":" + strconv.Itoa(cfg.Port) + ")/" + cfg.Database + "?charset=utf8"
  25 + db, err := sql.Open("mysql", url)
  26 + if err != nil {
  27 + logger.Notic("Open mysql failed:%v url:%v", err, url)
  28 + return nil, err
  29 + }
  30 + return db, nil
  31 +}
0 \ No newline at end of file 32 \ No newline at end of file