init.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package init
  2. import (
  3. util "app.yhyue.com/moapp/jybase/common"
  4. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/entity"
  5. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/internal/config"
  6. "context"
  7. "flag"
  8. "fmt"
  9. "time"
  10. "app.yhyue.com/moapp/jypkg/middleground"
  11. _ "github.com/go-sql-driver/mysql"
  12. "github.com/zeromicro/go-zero/core/conf"
  13. )
  14. var (
  15. configFile = flag.String("cf", "etc/bxcore.yaml", "the config file")
  16. C config.Config
  17. dbFile = flag.String("df", "etc/db.yaml", "the db file")
  18. DB config.Db
  19. logFile = flag.String("lf", "etc/logs.yaml", "the logs file")
  20. logc entity.Logc
  21. propertyFile = flag.String("pf", "etc/property.yaml", "the logs file")
  22. Property map[string][]string
  23. SearchLimitKey = "jy_limitSearchText_new" // 全文或附件搜索限制
  24. SearchLimitKeyNoLogin = "jy_limitSearchText_noLogin" //未登录
  25. Middleground *middleground.Middleground
  26. Search_Thread chan bool
  27. ReqLimitInit *ReqLimit
  28. )
  29. type ReqLimit struct {
  30. DoPool chan struct{}
  31. WaitPool chan struct{}
  32. }
  33. // -2 等待池已满
  34. // -1 超时
  35. // 1:可以执行查询
  36. func (r *ReqLimit) Limit(ctx context.Context) int {
  37. ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
  38. defer cancel()
  39. select {
  40. case <-r.WaitPool:
  41. defer func() {
  42. r.WaitPool <- struct{}{}
  43. }()
  44. select {
  45. case <-r.DoPool:
  46. return 1
  47. case <-ctx.Done(): //超时
  48. return -1
  49. }
  50. default:
  51. return -2
  52. }
  53. }
  54. func (r *ReqLimit) Release() {
  55. r.DoPool <- struct{}{}
  56. }
  57. func init() {
  58. //基本配置
  59. conf.MustLoad(*configFile, &C)
  60. util.ReadConfig("etc/property.json", &Property)
  61. //数据库配置
  62. conf.MustLoad(*dbFile, &DB)
  63. //初始mongodb
  64. MongoDBInit(&DB.Mongo)
  65. //初始化msyql
  66. MysqlInit(&DB.Mysql)
  67. //初始redis
  68. RedisInit(&DB.Redis)
  69. //初始es
  70. EsInit(&DB.Es)
  71. NoLoginEsInit(&DB.EsNoLogin)
  72. EsFreeInit(&DB.EsFree)
  73. Search_Thread = make(chan bool, C.SearchConcurrency)
  74. NoLoginPoolInit(C.NoLoginSearch.Switch, C.NoLoginSearch.ExecutionNum, C.NoLoginSearch.Wait)
  75. //初始化标签
  76. LabelInit()
  77. //
  78. SearchLimitKey = fmt.Sprintf(C.LimitSearchText.LimitKey, "jy_limitSearchText")
  79. //初始化中台相关
  80. Middleground = middleground.NewMiddleground(C.Middleground.Etcd.Hosts).
  81. RegUserCenter(C.Middleground.UserCenterKey).
  82. RegPowerCheckCenter(C.Middleground.PowerCheckCenterKey).
  83. RegResourceCenter(C.Middleground.ResourceCenterKey)
  84. }