db.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. 初始数据库应用
  3. */
  4. package init
  5. import (
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "strings"
  8. "jyBXBase/entity"
  9. "app.yhyue.com/moapp/jybase/esv1"
  10. "app.yhyue.com/moapp/jybase/mongodb"
  11. "app.yhyue.com/moapp/jybase/mysql"
  12. "app.yhyue.com/moapp/jybase/redis"
  13. )
  14. var (
  15. MainMysql *mysql.Mysql
  16. BaseServiceMysql *mysql.Mysql
  17. Mgo mongodb.MongodbSim
  18. MgoEnt mongodb.MongodbSim
  19. MgoBidding mongodb.MongodbSim
  20. )
  21. //
  22. func MongoDBInit(em *entity.Mongo) {
  23. //初始化 mongodb
  24. if em.Main.Address != "" {
  25. logx.Info("--初始化 mongodb--")
  26. Mgo = mongodb.MongodbSim{
  27. MongodbAddr: em.Main.Address,
  28. Size: em.Main.Size,
  29. DbName: em.Main.DbName,
  30. }
  31. Mgo.InitPool()
  32. }
  33. //
  34. if em.Bidding.Address != "" {
  35. logx.Info("--初始化 mongodb bidding--")
  36. MgoBidding = mongodb.MongodbSim{
  37. MongodbAddr: em.Bidding.Address,
  38. Size: em.Bidding.Size,
  39. DbName: em.Bidding.DbName,
  40. UserName: em.Bidding.UserName,
  41. Password: em.Bidding.Password,
  42. }
  43. MgoBidding.InitPool()
  44. }
  45. //
  46. if em.Ent.Address != "" {
  47. logx.Info("--初始化 mongodb ent--")
  48. MgoEnt = mongodb.MongodbSim{
  49. MongodbAddr: em.Ent.Address,
  50. Size: em.Ent.Size,
  51. DbName: em.Ent.DbName,
  52. UserName: em.Ent.UserName,
  53. Password: em.Ent.Password,
  54. }
  55. MgoEnt.InitPool()
  56. }
  57. }
  58. //
  59. func MysqlInit(em *entity.Mysql) {
  60. //初始化 mysql-main
  61. if em.Main.Address != "" {
  62. logx.Info("--初始化 main mysql--")
  63. MainMysql = &mysql.Mysql{
  64. Address: em.Main.Address,
  65. UserName: em.Main.UserName,
  66. PassWord: em.Main.Password,
  67. DBName: em.Main.DbName,
  68. MaxOpenConns: em.Main.MaxOpenConns,
  69. MaxIdleConns: em.Main.MaxIdleConns,
  70. }
  71. MainMysql.Init()
  72. }
  73. //初始化 mysql-BaseService
  74. if em.BaseService.Address != "" {
  75. logx.Info("--初始化 推送 mysql--")
  76. BaseServiceMysql = &mysql.Mysql{
  77. Address: em.BaseService.Address,
  78. UserName: em.BaseService.UserName,
  79. PassWord: em.BaseService.Password,
  80. DBName: em.BaseService.DbName,
  81. MaxOpenConns: em.BaseService.MaxOpenConns,
  82. MaxIdleConns: em.BaseService.MaxIdleConns,
  83. }
  84. BaseServiceMysql.Init()
  85. }
  86. }
  87. //
  88. func RedisInit(rm *entity.RedisStuct) {
  89. //初始化 redis
  90. if len(rm.Addr) > 0 {
  91. logx.Info("--初始化 redis--")
  92. redis.InitRedisBySize(strings.Join(rm.Addr, ","), 100, 30, 300)
  93. }
  94. }
  95. //
  96. func EsInit(es *entity.EsStruct) {
  97. //初始化 elasticsearch
  98. if es.Addr != "" {
  99. logx.Info("--初始化 elasticsearch--")
  100. elastic.InitElasticSize(es.Addr, es.Size)
  101. }
  102. }