main.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package main
  4. import (
  5. "flag"
  6. "fmt"
  7. "gd_adm_data/impl"
  8. "gd_adm_data/impl/v1/query"
  9. "log"
  10. "os"
  11. "os/signal"
  12. "strings"
  13. "syscall"
  14. "time"
  15. "git.getensh.com/common/gopkgsv2/database"
  16. "gd_adm_data/common.in/cache"
  17. "gd_adm_data/common.in/clinit"
  18. "gd_adm_data/common.in/config"
  19. "gd_adm_data/common.in/logger"
  20. "gd_adm_data/common.in/task"
  21. "gd_adm_data/common.in/utils"
  22. _ "github.com/go-sql-driver/mysql"
  23. "github.com/smallnest/rpcx/server"
  24. "github.com/smallnest/rpcx/serverplugin"
  25. gconfig "gd_adm_data/common.in/config"
  26. "github.com/rcrowley/go-metrics"
  27. "gopkg.in/ini.v1"
  28. )
  29. var (
  30. // 这里可以改默认值
  31. configFile = flag.String("config", "/etc/gd_adm_data/app.conf", "config file location")
  32. version = flag.Bool("version", false, "config file location")
  33. GitCommit = "library-import"
  34. Version = "library-import"
  35. )
  36. func showVersion() {
  37. fmt.Println("Version: ", Version)
  38. fmt.Println("GitCommit:", GitCommit)
  39. }
  40. func prepare(projectName, runmode, key, logDir string, etcdAddrs []string, discoveryType string) {
  41. var conf *config.Configure
  42. if discoveryType == "k8s" {
  43. conf = config.GetConfigForK8s()
  44. if conf == nil {
  45. fmt.Printf("get conf failed\n\n")
  46. os.Exit(1)
  47. }
  48. } else {
  49. // 先行于读配置
  50. clinit.InitEtcd(etcdAddrs)
  51. utils.SetEtcdKeyApi(clinit.GetEtcdClient())
  52. conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
  53. if conf == nil {
  54. fmt.Printf("get conf failed\n\n")
  55. os.Exit(1)
  56. }
  57. }
  58. appname := conf.Rpc.AdmData.Name
  59. // 指定mysql数据库,若无则使用默认数据库
  60. mysqldb := conf.Rpc.AdmData.MysqlDB
  61. if mysqldb == "" {
  62. mysqldb = conf.AdmMysql.DefaultDB
  63. }
  64. // 指定redis数据库,若无则使用默认数据库
  65. redisdb := conf.Rpc.AdmData.RedisDB
  66. if redisdb == "" {
  67. redisdb = conf.Redis.DefaultDB
  68. }
  69. // 连接数据库服务器
  70. maxIdle, _ := conf.AdmMysql.MaxIdle.Int64()
  71. maxConn, _ := conf.AdmMysql.MaxConn.Int64()
  72. database.Setup(conf.AdmMysql.User,
  73. conf.AdmMysql.Password,
  74. conf.AdmMysql.Addr,
  75. mysqldb,
  76. conf.AdmMysql.Charset,
  77. int(maxIdle),
  78. int(maxConn),
  79. true)
  80. /*clinit.InitMySQL(&clinit.MysqlConfig{
  81. User: conf.Mysql.User,
  82. Password: conf.Mysql.Password,
  83. Addr: conf.Mysql.Addr,
  84. DB: mysqldb,
  85. Charset: conf.Mysql.Charset,
  86. MaxIdle: conf.Mysql.MaxIdle,
  87. MaxConn: conf.Mysql.MaxConn,
  88. })*/
  89. // 初始化mongodb数据库
  90. //clinit.InitMongo(conf.Mongo.Addr, conf.Mongo.User, conf.Mongo.Password)
  91. // 连接redis服务器
  92. cache.InitRedis(&cache.RedisConfig{
  93. Addrs: strings.Split(conf.Redis.Addrs, ","),
  94. Password: conf.Redis.Password,
  95. DB: redisdb,
  96. PoolSize: conf.Redis.PoolSize,
  97. MinIdleConns: conf.Redis.MinIdleConns,
  98. MaxRetries: conf.Redis.MaxRetries,
  99. IsCluster: conf.Redis.IsCluster,
  100. })
  101. //rpc_apis.Init(etcdAddrs, conf)
  102. // clinit.InitUniqueID()
  103. // clinit.InitElastic(conf.Elastic.Addr, conf.Elastic.Sniff == "true")
  104. // 初始化logger
  105. ms, _ := conf.Rpc.AdmData.Log.MaxSize.Int64()
  106. mb, _ := conf.Rpc.AdmData.Log.MaxBackups.Int64()
  107. ma, _ := conf.Rpc.AdmData.Log.MaxAge.Int64()
  108. maxSize := int(ms)
  109. maxBackups := int(mb)
  110. maxAge := int(ma)
  111. disableStacktrace := (conf.Rpc.AdmData.Log.DisableStacktrace == "true")
  112. // 通用logger
  113. commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), appname, conf.Rpc.AdmData.Log.Level,
  114. maxSize, maxBackups, maxAge, disableStacktrace)
  115. // 单独设置
  116. accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), appname, conf.Rpc.AdmData.Log.Level,
  117. maxSize, maxBackups, maxAge)
  118. // 设置需要使用logger的地方
  119. // common日志
  120. // access日志
  121. query.SetLogger(commonLogger)
  122. task.SetLogger(accessLogger)
  123. // 第三方日志
  124. }
  125. func start(conf *config.Configure, serveAddr string, etcdAddrs []string) {
  126. s := server.NewServer()
  127. r := &serverplugin.EtcdRegisterPlugin{
  128. ServiceAddress: fmt.Sprintf("tcp@%s", serveAddr),
  129. EtcdServers: etcdAddrs,
  130. BasePath: conf.Rpc.BasePath,
  131. Metrics: metrics.NewRegistry(),
  132. UpdateInterval: time.Minute,
  133. }
  134. if err := r.Start(); err != nil {
  135. fmt.Printf("start failed. error:%s\n\n", err)
  136. os.Exit(1)
  137. }
  138. s.Plugins.Add(r)
  139. s.RegisterName(conf.Rpc.AdmData.Name, new(impl.Rcvr), "")
  140. go func() {
  141. if err := s.Serve("tcp", serveAddr); err != nil {
  142. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  143. }
  144. }()
  145. // 捕获信号
  146. sigChan := make(chan os.Signal, 1)
  147. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  148. sigValue := <-sigChan
  149. log.Printf("Got a signal:%v", sigValue)
  150. r.Stop()
  151. time.Sleep(5 * time.Second)
  152. log.Println("Shutdown server finished.")
  153. }
  154. func startPeerToPeer(conf *config.Configure, serveAddr string) {
  155. s := server.NewServer()
  156. s.RegisterName(conf.Rpc.AdmData.Name, new(impl.Rcvr), "")
  157. s.Serve("tcp", serveAddr)
  158. }
  159. func main() {
  160. flag.Parse()
  161. if *version {
  162. showVersion()
  163. }
  164. cfg, err := ini.Load(*configFile)
  165. if err != nil {
  166. fmt.Printf("Fail to read file: %v\n\n", err)
  167. os.Exit(1)
  168. }
  169. //appname := cfg.Section("").Key("appname").String()
  170. runmode := cfg.Section("").Key("runmode").String()
  171. logDir := cfg.Section("").Key("log_dir").String()
  172. //serveAddr := cfg.Section("").Key("serve_addr").String()
  173. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  174. encryptKey := cfg.Section("").Key("encrypt_key").String()
  175. discoveryType := cfg.Section("").Key("discovery_type").String()
  176. projectName := cfg.Section("").Key("project_name").String()
  177. utils.SetRunmode(runmode)
  178. prepare(projectName, runmode, encryptKey, logDir, etcdAddrs, discoveryType)
  179. go utils.Free()
  180. serveAddr := fmt.Sprintf("%s:%s", gconfig.Conf.Rpc.AdmData.ServiceName, gconfig.Conf.Rpc.AdmData.ServicePort.String())
  181. if discoveryType == "etcd" {
  182. start(gconfig.Conf, serveAddr, etcdAddrs)
  183. } else {
  184. startPeerToPeer(gconfig.Conf, serveAddr)
  185. }
  186. return
  187. }