init.go 2.7 KB

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