123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package main
- import (
- "gd_admin/impl"
- "gd_admin/impl/action"
- "gd_admin/impl/admin"
- "gd_admin/impl/auth"
- "gd_admin/impl/rbac"
- "gd_admin/thirdparty/oss"
- "flag"
- "fmt"
- "os"
- "strings"
- "time"
- "gd_admin/common.in/clinit"
- "gd_admin/common.in/config"
- "gd_admin/common.in/logger"
- "gd_admin/common.in/task"
- "github.com/astaxie/beego/orm"
- _ "github.com/go-sql-driver/mysql"
- //"github.com/gomodule/redigo/redis"
- "github.com/smallnest/rpcx/server"
- "github.com/smallnest/rpcx/serverplugin"
- gconfig "gd_admin/common.in/config"
- "github.com/rcrowley/go-metrics"
- "gopkg.in/ini.v1"
- )
- var (
- // 这里可以改默认值
- configFile = flag.String("config", "/etc/gd_admin/app.conf", "config file location")
- GitCommit = "library-import"
- Version = "library-import"
- )
- func prepare(projectName, runmode, key, logDir string, etcdAddrs []string, discoveryType string) {
- var conf *config.Configure
- if discoveryType == "k8s" {
- conf = config.GetConfigForK8s()
- if conf == nil {
- fmt.Printf("get conf failed\n\n")
- os.Exit(1)
- }
- } else {
- // 先行于读配置
- clinit.InitEtcd(etcdAddrs)
- fmt.Println("init etcd:",etcdAddrs)
- conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
- if conf == nil {
- fmt.Printf("get conf failed\n\n")
- os.Exit(1)
- }
- fmt.Println("conf:",*conf)
- }
- appname := conf.Rpc.Admin.Name
- // 指定mysql数据库,若无则使用默认数据库
- mysqldb := conf.Rpc.Admin.MysqlDB
- if mysqldb == "" {
- mysqldb = conf.Mysql.DefaultDB
- }
- // 指定redis数据库,若无则使用默认数据库
- /*redisdb := conf.Rpc.Admin.RedisDB
- if redisdb == "" {
- redisdb = conf.Redis.DefaultDB
- }*/
- // 连接数据库服务器
- clinit.InitMySQL(&clinit.MysqlConfig{
- User: conf.Mysql.User,
- Password: conf.Mysql.Password,
- Addr: conf.Mysql.Addr,
- DB: mysqldb,
- Charset: conf.Mysql.Charset,
- MaxIdle: conf.Mysql.MaxIdle,
- MaxConn: conf.Mysql.MaxConn,
- })
- // 连接redis服务器
- /*cache.InitRedis(&cache.RedisConfig{
- Network: conf.Redis.Network,
- Address: conf.Redis.Address,
- Password: conf.Redis.Password,
- DB: redisdb,
- ConnectTimeout: conf.Redis.ConnectTimeout,
- ReadTimeout: conf.Redis.ReadTimeout,
- WriteTimeout: conf.Redis.WriteTimeout,
- IdleTimeout: conf.Redis.IdleTimeout,
- MaxActive: conf.Redis.MaxActive,
- MaxIdle: conf.Redis.MaxIdle,
- Wait: conf.Redis.Wait,
- })
- */
- // 初始化logger
- ms, _ := conf.Rpc.Admin.Log.MaxSize.Int64()
- mb, _ := conf.Rpc.Admin.Log.MaxBackups.Int64()
- ma, _ := conf.Rpc.Admin.Log.MaxAge.Int64()
- maxSize := int(ms)
- maxBackups := int(mb)
- maxAge := int(ma)
- disableStacktrace := (conf.Rpc.Admin.Log.DisableStacktrace == "true")
- // 通用logger
- commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname),
- maxSize, maxBackups, maxAge, disableStacktrace)
- // 单独设置
- accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname),
- maxSize, maxBackups, maxAge)
- thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname),
- maxSize, maxBackups, maxAge)
- // 设置三方日志
- oss.SetLogger(thirdpartyLogger)
- //thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname),
- // maxSize, maxBackups, maxAge)
- // access日志
- task.SetLogger(accessLogger)
- admin.SetLogger(commonLogger)
- rbac.SetLogger(commonLogger)
- auth.SetLogger(commonLogger)
- action.SetLogger(commonLogger)
- // 设置需要使用logger的地方
- // common日志
- impl.RegisterOrmModel()
- if runmode != "prod" {
- orm.Debug = true
- //redis.Debug = true
- }
- }
- func start(conf *config.Configure, serveAddr string, etcdAddrs []string) {
- s := server.NewServer()
- fmt.Println("conf:",*conf,serveAddr ,etcdAddrs)
- r := &serverplugin.EtcdRegisterPlugin{
- ServiceAddress: fmt.Sprintf("tcp@%s", serveAddr),
- EtcdServers: etcdAddrs,
- BasePath: conf.Rpc.BasePath,
- Metrics: metrics.NewRegistry(),
- UpdateInterval: time.Minute,
- }
- fmt.Println("111111111111",etcdAddrs)
- if err := r.Start(); err != nil {
- fmt.Printf("start failed. error:%s\n\n", err)
- os.Exit(1)
- }
- fmt.Println("22222222222222222222")
- s.Plugins.Add(r)
- fmt.Println("22222222222222222222")
- s.RegisterName(conf.Rpc.Admin.Name, new(impl.Rcvr), "")
- fmt.Println("22222222222222222222")
- s.Serve("tcp", serveAddr)
- }
- func startPeerToPeer(conf *config.Configure, serveAddr string) {
- s := server.NewServer()
- s.RegisterName(conf.Rpc.Admin.Name, new(impl.Rcvr), "")
- s.Serve("tcp", serveAddr)
- }
- func main() {
- flag.Parse()
- cfg, err := ini.Load(*configFile)
- if err != nil {
- fmt.Printf("Fail to read file: %v\n\n", err)
- os.Exit(1)
- }
- //appname := cfg.Section("").Key("appname").String()
- logDir := cfg.Section("").Key("log_dir").String()
- runmode := cfg.Section("").Key("runmode").String()
- //serveAddr := cfg.Section("").Key("serve_addr").String()
- etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
- encryptKey := cfg.Section("").Key("encrypt_key").String()
- discoveryType := cfg.Section("").Key("discovery_type").String()
- projectName := cfg.Section("").Key("project_name").String()
- fmt.Println("etcd addr",etcdAddrs)
- prepare(projectName, runmode, encryptKey, logDir, etcdAddrs, discoveryType)
- serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Rpc.Admin.ServiceName,gconfig.Conf.Rpc.Admin.ServicePort.String())
- if discoveryType == "etcd" {
- fmt.Println("start etcd")
- start(gconfig.Conf, serveAddr, etcdAddrs)
- } else {
- startPeerToPeer(gconfig.Conf, serveAddr)
- }
- return
- }
|