baseInit.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package util
  2. import (
  3. "app.yhyue.com/moapp/jybase/es"
  4. elastic "app.yhyue.com/moapp/jybase/esv7"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "app.yhyue.com/moapp/jybase/mysql"
  7. "app.yhyue.com/moapp/jybase/redis"
  8. "app.yhyue.com/moapp/jyfs/rpc/filesystemclient"
  9. "github.com/zeromicro/go-zero/core/discov"
  10. "github.com/zeromicro/go-zero/zrpc"
  11. "google.golang.org/grpc"
  12. "gorm.io/gorm"
  13. "log"
  14. "strings"
  15. )
  16. type MysqlDBConfig struct {
  17. DriverName string
  18. DataSourceName string
  19. MaxOpenConn int
  20. MaxIdleConn int
  21. MaxConnLifeTime int
  22. }
  23. type EsConfig struct {
  24. Addr string
  25. Pool int
  26. UserName string
  27. Password string
  28. }
  29. type MongoConfig struct {
  30. Main Mongo
  31. Logger Mongo
  32. }
  33. type Mongo struct {
  34. Address string `json:"address"`
  35. Size int `json:"size"`
  36. DbName string `json:"dbName"`
  37. UserName string `json:"userName,optional"`
  38. Password string `json:"password,optional"`
  39. }
  40. var (
  41. jyDocsDB *gorm.DB
  42. FileSystem filesystemclient.FileSystem
  43. mgoLog *mongodb.MongodbSim
  44. mgoMain *mongodb.MongodbSim
  45. esV7 es.Es
  46. )
  47. func InitDB(url, driverName string, maxOpenConn, maxIdle int) {
  48. jyDocsDB = mysql.GormMysql(url, driverName, maxOpenConn, maxIdle, nil)
  49. if jyDocsDB != nil {
  50. log.Printf("----------->【jy_docs】 DB :[%s] 初始化成功!<--------------", url)
  51. } else {
  52. log.Fatalf("----------->【jy_docs】 DB初始化失败<--------------")
  53. }
  54. }
  55. func InitEs(addr string, poolSize int, userName, password string) {
  56. log.Printf("----------->【jy_docs】 elastic :[%s] init<--------------", addr)
  57. elastic.InitElasticSizeByAuth(addr, poolSize, userName, password)
  58. }
  59. func InitOss(etcd discov.EtcdConf) {
  60. grpcOpts := []zrpc.ClientOption{
  61. zrpc.WithDialOption(grpc.WithWriteBufferSize(40 * 1024 * 1024)),
  62. zrpc.WithDialOption(grpc.WithReadBufferSize(40 * 1024 * 1024)),
  63. zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(40 * 1024 * 1024))), // 设置接收消息的最大字节数,这里也是40MB
  64. zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(40 * 1024 * 1024))), // 设置发送消息的最大字节数,这里是40MB
  65. }
  66. conf := zrpc.RpcClientConf{
  67. Etcd: etcd,
  68. }
  69. client := zrpc.MustNewClient(conf, grpcOpts...)
  70. FileSystem = filesystemclient.NewFileSystem(client)
  71. }
  72. func InitRedis(rc []string) {
  73. //初始化 redis
  74. if len(rc) > 0 {
  75. log.Printf("----------->【jy_docs】 redis :[%s] init<--------------", strings.Join(rc, ","))
  76. redis.InitRedisBySize(strings.Join(rc, ","), 100, 30, 300)
  77. }
  78. }
  79. func InitMongo(mgo MongoConfig) {
  80. if mgo.Main.Address != "" {
  81. log.Printf("----------->【jy_docs】 mongodb main:[%s] init<--------------", mgo.Main.Address)
  82. mgoMain = &mongodb.MongodbSim{
  83. MongodbAddr: mgo.Main.Address,
  84. Size: mgo.Main.Size,
  85. DbName: mgo.Main.DbName,
  86. UserName: mgo.Main.UserName,
  87. Password: mgo.Main.Password,
  88. }
  89. mgoMain.InitPool()
  90. }
  91. if mgo.Logger.Address != "" {
  92. log.Printf("----------->【jy_docs】 mongodb log:[%s] init<--------------", mgo.Logger.Address)
  93. mgoLog = &mongodb.MongodbSim{
  94. MongodbAddr: mgo.Logger.Address,
  95. Size: mgo.Logger.Size,
  96. DbName: mgo.Logger.DbName,
  97. UserName: mgo.Logger.UserName,
  98. Password: mgo.Logger.Password,
  99. }
  100. mgoLog.InitPool()
  101. }
  102. }
  103. // esv7
  104. func InitESV7(addr string, poolSize int, userName, password string) {
  105. if addr != "" {
  106. log.Printf("----------->【jy_docs】 elasticSearch :[%s] init<--------------", addr)
  107. esV7 = &es.EsV7{
  108. Address: addr,
  109. UserName: userName,
  110. Password: password,
  111. Size: poolSize,
  112. }
  113. esV7.Init()
  114. }
  115. }
  116. func GetJyMgoLoggerDB() *mongodb.MongodbSim {
  117. return mgoLog
  118. }
  119. func GetJyMgoMainDB() *mongodb.MongodbSim {
  120. return mgoMain
  121. }
  122. func GetJyDocsDB() *gorm.DB {
  123. return jyDocsDB
  124. }
  125. func GetESV7DB() es.Es {
  126. return esV7
  127. }