init.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/ClickHouse/clickhouse-go/v2"
  6. "github.com/ClickHouse/clickhouse-go/v2/lib/driver"
  7. log "github.com/donnie4w/go-logger/logger"
  8. "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  9. "runtime"
  10. "time"
  11. )
  12. var (
  13. SourceMgo, QyxyMgo *MongodbSim
  14. SpiMgo, ExtMgo *MongodbSim
  15. MysqlGlobalTool *Mysql
  16. IsLocal, IsDev bool
  17. ClickHouseConn driver.Conn
  18. EsClinet *elastic.Elastic
  19. TimeLayout = "2006年01月02日"
  20. TimeLayout_New = "2006-01-02 15:04:05"
  21. Url = "https://www.jianyu360.cn/article/content/%s.html"
  22. )
  23. func InitGlobalVar() {
  24. IsLocal = true
  25. initMgo()
  26. //initMysql()
  27. //initClickHouse()
  28. //initEs()
  29. }
  30. func initEs() {
  31. url := "http://127.0.0.1:13003"
  32. if !IsLocal {
  33. url = "http://172.17.4.184:19805"
  34. }
  35. EsClinet = &elastic.Elastic{
  36. S_esurl: url,
  37. I_size: 10,
  38. Username: "es_all",
  39. Password: "TopJkO2E_d1x",
  40. }
  41. EsClinet.InitElasticSize()
  42. }
  43. func initClickHouse() {
  44. ClickHouseConn, _ = connectClickhouse()
  45. }
  46. // 初始化mgo
  47. // 初始化mgo
  48. func initMgo() {
  49. if IsLocal {
  50. SourceMgo = &MongodbSim{
  51. MongodbAddr: "127.0.0.1:12005",
  52. DbName: "qfw",
  53. Size: 10,
  54. UserName: "zhengkun",
  55. Password: "zk@123123",
  56. }
  57. SourceMgo.InitPoolDirect()
  58. QyxyMgo = &MongodbSim{
  59. MongodbAddr: "127.0.0.1:12005",
  60. DbName: "mixdata",
  61. Size: 10,
  62. UserName: "zhengkun",
  63. Password: "zk@123123",
  64. }
  65. QyxyMgo.InitPoolDirect()
  66. SpiMgo = &MongodbSim{
  67. MongodbAddr: "127.0.0.1:12004",
  68. DbName: "zhengkun",
  69. Size: 10,
  70. UserName: "",
  71. Password: "",
  72. }
  73. SpiMgo.InitPoolDirect()
  74. ExtMgo = &MongodbSim{
  75. MongodbAddr: "127.0.0.1:12001",
  76. DbName: "qfw",
  77. Size: 10,
  78. UserName: "",
  79. Password: "",
  80. }
  81. ExtMgo.InitPoolDirect()
  82. } else {
  83. SourceMgo = &MongodbSim{
  84. MongodbAddr: "172.17.189.140:27080,172.17.189.141:27081",
  85. DbName: "qfw",
  86. Size: 10,
  87. UserName: "zhengkun",
  88. Password: "zk@123123",
  89. }
  90. SourceMgo.InitPool()
  91. QyxyMgo = &MongodbSim{
  92. MongodbAddr: "172.17.189.140:27080,172.17.189.141:27081",
  93. DbName: "mixdata",
  94. Size: 10,
  95. UserName: "zhengkun",
  96. Password: "zk@123123",
  97. }
  98. QyxyMgo.InitPool()
  99. SpiMgo = &MongodbSim{
  100. MongodbAddr: "172.17.4.181:27001",
  101. DbName: "mixdata",
  102. Size: 10,
  103. UserName: "",
  104. Password: "",
  105. }
  106. SpiMgo.InitPool()
  107. ExtMgo = &MongodbSim{
  108. MongodbAddr: "172.17.4.85:27080",
  109. DbName: "qfw",
  110. Size: 10,
  111. UserName: "",
  112. Password: "",
  113. }
  114. ExtMgo.InitPool()
  115. }
  116. }
  117. func initMysql() {
  118. username, password := "zhengkun", "Zk#20220824"
  119. address := "127.0.0.1:15001"
  120. if !IsLocal {
  121. address = "172.17.4.242:4000"
  122. }
  123. if IsDev {
  124. username, password = "root", "=PDT49#80Z!RVv52_z"
  125. address = "192.168.3.217:4000"
  126. }
  127. MysqlGlobalTool = &Mysql{
  128. Address: address,
  129. UserName: username,
  130. PassWord: password,
  131. DBName: "global_common_data",
  132. }
  133. MysqlGlobalTool.Init()
  134. }
  135. // 创建clickhouse连接
  136. func connectClickhouse() (driver.Conn, error) {
  137. var (
  138. ctx = context.Background()
  139. /*
  140. 外网地址:cc-2ze9tv451wov14w9e.public.clickhouse.ads.aliyuncs.com:9000
  141. 内网地址:cc-2ze9tv451wov14w9e.clickhouse.ads.aliyuncs.com:9000
  142. */
  143. conn, err = clickhouse.Open(&clickhouse.Options{
  144. Addr: []string{"cc-2ze9tv451wov14w9e.clickhouse.ads.aliyuncs.com:9000"},
  145. DialTimeout: 10 * time.Second,
  146. MaxIdleConns: 3,
  147. MaxOpenConns: 30,
  148. Auth: clickhouse.Auth{
  149. Database: "information",
  150. Username: "biservice",
  151. Password: "Bi_top95215#",
  152. },
  153. Debugf: func(format string, v ...interface{}) {
  154. fmt.Printf(format, v)
  155. },
  156. })
  157. )
  158. if err != nil {
  159. return nil, err
  160. }
  161. if err := conn.Ping(ctx); err != nil {
  162. if exception, ok := err.(*clickhouse.Exception); ok {
  163. fmt.Printf("Exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
  164. }
  165. return nil, err
  166. }
  167. return conn, nil
  168. }
  169. // 暂时无用...
  170. func GetNewInfo(index, query string) map[string]interface{} {
  171. //log.Println("query -- ", query)
  172. client := EsClinet.GetEsConn()
  173. defer EsClinet.DestoryEsConn(client)
  174. res := map[string]interface{}{}
  175. if client != nil {
  176. defer func() {
  177. if r := recover(); r != nil {
  178. log.Debug("[E]", r)
  179. for skip := 1; ; skip++ {
  180. _, file, line, ok := runtime.Caller(skip)
  181. if !ok {
  182. break
  183. }
  184. go log.Debug("%v,%v\n", file, line)
  185. }
  186. }
  187. }()
  188. searchResult, err := client.Search().Index(index).Source(query).Do(context.TODO())
  189. if err != nil {
  190. log.Debug("从ES查询出错", err.Error())
  191. return nil
  192. }
  193. if searchResult.Hits != nil {
  194. resNum := searchResult.Hits.TotalHits.Value
  195. res["total"] = resNum
  196. }
  197. if searchResult.Aggregations != nil {
  198. bidamount_sum, _ := searchResult.Aggregations.Sum("bidamount_sum")
  199. res["bidamount_sum"] = *bidamount_sum.Value
  200. }
  201. }
  202. return res
  203. }