123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- // Copyright 2019 getensh.cc. All rights reserved.
- // Use of this source code is governed by getensh.cc.
- package main
- import (
- "context"
- "adm-dws/common.in/cache"
- "adm-dws/common.in/mq"
- "adm-dws/impl"
- "adm-dws/impl/handle"
- "adm-dws/impl/task"
- "adm-dws/model"
- "adm-dws/pb"
- dutils "adm-dws/utils"
- "flag"
- "fmt"
- "log"
- "os"
- "os/signal"
- "strings"
- "syscall"
- "time"
- //"adm-dws/common.in/cache"
- "adm-dws/common.in/clinit"
- "adm-dws/common.in/config"
- "adm-dws/common.in/logger"
- "adm-dws/common.in/utils"
- _ "github.com/go-sql-driver/mysql"
- "gopkg.in/ini.v1"
- )
- var (
- // 这里可以改默认值
- appConfigFile = flag.String("appconfig", "/etc/adm-dws/app.conf", "app config file location")
- configFile = flag.String("config", "/etc/adm/common.json", "config file location")
- version = flag.Bool("version", false, "config file location")
- GitCommit = "library-import"
- Version = "library-import"
- )
- func showVersion() {
- fmt.Println("Version: ", Version)
- fmt.Println("GitCommit:", GitCommit)
- }
- func prepare(filename string,etcdAddrs []string, discoveryType string) {
- var conf *config.Configure
- if discoveryType == "k8s" {
- config.SetConfigFile(filename)
- conf = config.GetConfigForK8s()
- if conf == nil {
- fmt.Printf("get conf failed\n\n")
- os.Exit(1)
- }
- } else {
- config.SetConfigFile(filename)
- conf = config.GetConfigForK8s()
- if conf == nil {
- fmt.Printf("get conf failed\n\n")
- os.Exit(1)
- }
- clinit.EctdHandler(etcdAddrs)
- // 先行于读配置
- /*clinit.InitEtcd(etcdAddrs)
- conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
- if conf == nil {
- fmt.Printf("get conf failed\n\n")
- os.Exit(1)
- }
- */
- //rpc_apis.Init(etcdAddrs, conf)
- }
- // 指定mysql数据库,若无则使用默认数据库
- mysqldb := conf.Rpc.AdmDws.MysqlDb
- if mysqldb == "" {
- mysqldb = conf.Mysql.Db
- }
- fmt.Println("init mysql start")
- // 连接数据库服务器
- clinit.InitMysqlGorm(
- conf.Mysql.User,
- conf.Mysql.Password,
- conf.Mysql.Addr,
- mysqldb,
- conf.Mysql.Charset,
- conf.Mysql.MaxIdle,
- conf.Mysql.MaxConn,
- conf.RunMode != "prod",
- )
- fmt.Println("init mysql end")
- // 指定redis数据库,若无则使用默认数据库
- redisdb := conf.Rpc.AdmDws.RedisDb
- if redisdb == "" {
- redisdb = conf.Redis.Db
- }
- fmt.Println("init redis start")
- // 连接redis服务器
- cache.InitRedis(&cache.RedisConfig{
- Addrs: strings.Split(conf.Redis.Addrs, ","),
- Password: conf.Redis.Password,
- DB: redisdb,
- PoolSize: conf.Redis.PoolSize,
- MinIdleConns: conf.Redis.MinIdleConns,
- MaxRetries: conf.Redis.MaxRetries,
- IsCluster: conf.Redis.IsCluster,
- })
- fmt.Println("init redis end")
- fmt.Println("init client start")
- // 建立rpc客户端
- conns := pb.SetupClients()
- for _, conn := range conns {
- defer conn.Close()
- }
- fmt.Println("init client end")
- ms, _ := conf.Log.MaxSize.Int64()
- mb, _ := conf.Log.MaxBackups.Int64()
- ma, _ := conf.Log.MaxAge.Int64()
- maxSize := int(ms)
- maxBackups := int(mb)
- maxAge := int(ma)
- disableStacktrace := (conf.Log.Stacktrace == "true")
- // 通用logger
- commonLogger := logger.InitLogger(conf.RunMode, fmt.Sprintf("%s/%s.log", conf.Log.Path, conf.Rpc.AdmDws.ServiceName), conf.Rpc.AdmDws.ServiceName, conf.Log.Level,
- maxSize, maxBackups, maxAge, disableStacktrace)
- // 单独设置
- accessLogger := logger.NewInfoLogger(conf.RunMode, fmt.Sprintf("%s/%s-access.log", conf.Log.Path, conf.Rpc.AdmDws.ServiceName), conf.Rpc.AdmDws.ServiceName, conf.Log.Level,
- maxSize, maxBackups, maxAge)
- // 设置需要使用logger的地方
- dutils.SetLogger(commonLogger)
- model.SetLogger(commonLogger)
- task.SetLogger(commonLogger)
- handle.SetLogger(commonLogger)
- handle.SetAccessLogger(accessLogger)
- fmt.Println("loading map start")
- task.Load()
- fmt.Println("loading map end")
- fmt.Println("init rabbitmq start")
- // 初始化rabbitmq
- consumerCount, _ := conf.DwsRabbitmq.ConsumerCount.Int64()
- fmt.Println("consumerCount:", consumerCount)
- if consumerCount == 0 {
- consumerCount = 2
- }
- fmt.Printf("rmqaddr:%v,%v,%v\n", conf.DwsRabbitmq.Addr, conf.AdsRabbitmq.Addr, conf.Md5Rabbitmq.Addr)
- dwsMq := mq.InitRabbitmq(
- conf.DwsRabbitmq.Addr,
- conf.DwsRabbitmq.Username,
- conf.DwsRabbitmq.Passwrod,
- conf.DwsRabbitmq.Vhost,
- conf.DwsRabbitmq.ExchangeName,
- conf.DwsRabbitmq.QueueName,
- conf.DwsRabbitmq.RouteBindKey,
- impl.HandleDwsData,
- true,
- int(consumerCount),
- )
- mq.SetDwsMq(dwsMq)
- adsMq := mq.InitRabbitmq(
- conf.AdsRabbitmq.Addr,
- conf.AdsRabbitmq.Username,
- conf.AdsRabbitmq.Passwrod,
- conf.AdsRabbitmq.Vhost,
- conf.AdsRabbitmq.ExchangeName,
- conf.AdsRabbitmq.QueueName,
- conf.AdsRabbitmq.RouteBindKey,
- nil,
- true,
- 0,
- )
- mq.SetAdsMq(adsMq)
- /*
- md5Mq := mq.InitRabbitmq(
- conf.Md5Rabbitmq.Addr,
- conf.Md5Rabbitmq.Username,
- conf.Md5Rabbitmq.Passwrod,
- conf.Md5Rabbitmq.Vhost,
- conf.Md5Rabbitmq.ExchangeName,
- conf.Md5Rabbitmq.QueueName,
- conf.Md5Rabbitmq.RouteBindKey,
- nil,
- true,
- 0,
- )
- mq.SetMd5Mq(md5Mq)
- */
- dwsMq.StartConsumer()
- fmt.Println("init rabbitmq end")
- }
- func start() {
- // 优雅关闭服务器
- sigChan := make(chan os.Signal, 1)
- // 捕获信号
- signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
- sigValue := <-sigChan
- log.Printf("Got a signal:%v", sigValue)
- // 不管什么行为,都等待5秒退出
- log.Println("Start to shutdown server...")
- _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- log.Println("Shutdown server finished.")
- }
- func main() {
- flag.Parse()
- if *version {
- showVersion()
- }
- cfg, err := ini.Load(*appConfigFile)
- if err != nil {
- fmt.Printf("Fail to read file: %v\n\n", err)
- os.Exit(1)
- }
- runmode := cfg.Section("").Key("runmode").String()
- etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
- discoveryType := cfg.Section("").Key("discovery_type").String()
- utils.SetRunmode(runmode)
- prepare(*configFile,etcdAddrs, discoveryType)
- go utils.Free()
- start()
- return
- }
|