init.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Middleground *middleground.Middleground
  25. Search_Thread chan bool
  26. ReqLimitInit *ReqLimit
  27. )
  28. type ReqLimit struct {
  29. DoPool chan struct{}
  30. WaitPool chan struct{}
  31. }
  32. // -2 等待池已满
  33. // -1 超时
  34. // 1:可以执行查询
  35. func (r *ReqLimit) Limit(ctx context.Context) int {
  36. ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
  37. defer cancel()
  38. select {
  39. case <-r.WaitPool:
  40. defer func() {
  41. r.WaitPool <- struct{}{}
  42. }()
  43. select {
  44. case <-r.DoPool:
  45. return 1
  46. case <-ctx.Done(): //超时
  47. return -1
  48. }
  49. default:
  50. return -2
  51. }
  52. }
  53. func (r *ReqLimit) Release() {
  54. r.DoPool <- struct{}{}
  55. }
  56. func init() {
  57. //基本配置
  58. conf.MustLoad(*configFile, &C)
  59. util.ReadConfig("etc/property.json", &Property)
  60. //数据库配置
  61. conf.MustLoad(*dbFile, &DB)
  62. //初始mongodb
  63. MongoDBInit(&DB.Mongo)
  64. //初始化msyql
  65. MysqlInit(&DB.Mysql)
  66. //初始redis
  67. RedisInit(&DB.Redis)
  68. //初始es
  69. EsInit(&DB.Es)
  70. NoLoginEsInit(&DB.EsNoLogin)
  71. EsFreeInit(&DB.EsFree)
  72. Search_Thread = make(chan bool, C.SearchConcurrency)
  73. NoLoginPoolInit(C.NoLoginSearch.Switch, C.NoLoginSearch.ExecutionNum, C.NoLoginSearch.Wait)
  74. //初始化标签
  75. LabelInit()
  76. //
  77. SearchLimitKey = fmt.Sprintf(C.LimitSearchText.LimitKey, "jy_limitSearchText")
  78. //初始化中台相关
  79. Middleground = middleground.NewMiddleground(C.Middleground.Etcd.Hosts).
  80. RegUserCenter(C.Middleground.UserCenterKey).
  81. RegPowerCheckCenter(C.Middleground.PowerCheckCenterKey).
  82. RegResourceCenter(C.Middleground.ResourceCenterKey)
  83. }