当几个golang 应用用的同一个数据库,譬如电商系统切开了, cart , customer 应用都在本机,单独去配置文件填写,那么每个应用都要填写
可以通过linux的环境变量设置值,然后读取
github库包:github.com/coreos/pkg/flagutil
vim /etc/profile
添加如下内容:
export GOROOT=/usr/local/go
export GOPATH=/root/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
export TIX_AES_KEY=xxxxxxxxx
export TIX_NODE_NO=1
export TIX_AUTH_CRED=xxxxxxxxxx
export TIX_CLIENT_SECRET=123456
export TIX_XOR_KEY=666666
export TIX_CMS_DB_HOST=127.0.0.1
export TIX_CMS_DB_PORT=5433
export TIX_CMS_DB_USER=root
export TIX_CMS_DB_PASSWORD=123456
export TIX_CMS_DB_NAME=cms
export TIX_DB_HOST=127.0.0.1
export TIX_DB_PORT=5434
export TIX_DB_USER=root
export TIX_DB_PASSWORD=123456
export TIX_DB_NAME=newDb
export TIX_REDIS_MAIN=127.0.0.1:6380
export TIX_AMQP_URL=amqp://rabbitmq:rabbitmq@127.0.0.1:5672/
export TIX_PG_DSN_MASTER=postgres://cms:4passworddddd@dev.xxxx.to:5002/tix_new_db?sslmode=disable
export TIX_PG_DSN_SLAVE=postgres://cms:4passworddddd@dev.xxxx.to:5002/tix_new_db?sslmode=disable
export TIX_CLIENT_SECRET_CMS=66666666
source /etc/profile
golang中读取值
package repository
import (
"context"
"flag"
"fmt"
"xxxxxx/logging"
"github.com/coreos/pkg/flagutil"
)
var (
datasource *DataSource
dsnMaster string
dsnSlave string
psqlMaxCon int
psqlMaxIdleCon int
redisMain string
)
var log = logging.MustGetLogger()
func InitFactoryV2() error {
flags := flag.NewFlagSet("user-auth", flag.PanicOnError)
flags.IntVar(&psqlMaxCon, "DB_MAX_CON", 20, "PSQL db max connection")
flags.IntVar(&psqlMaxIdleCon, "DB_MAX_IDLE_CON", 10, "PSQL db max idle connection")
flags.StringVar(&dsnMaster, "PG_DSN_MASTER", "", "PG SQL data source name for master")
flags.StringVar(&dsnSlave, "PG_DSN_SLAVE", "", "PG SQL data source name for slave")
//redis
flags.StringVar(&redisMain, "REDIS_MAIN", "", "redis main connection")
err := flagutil.SetFlagsFromEnv(flags, "TIX")
if err != nil {
return err
}
if dsnMaster == "" {
return fmt.Errorf("Unable to get environtment variable for dsn master, make sure you already set it!")
}
if dsnSlave == "" {
return fmt.Errorf("Unable to get environtment variable for dsn Slave, make sure you already set it!")
}
if redisMain == "" {
return fmt.Errorf("Unable to get environtment variable for redis main, make sure you already set it!")
}
dsn := fmt.Sprintf("%s;%s", dsnMaster, dsnSlave) //append master and slave(s)
datasource = NewDBConnectionWithWrapper(dsn, psqlMaxCon, psqlMaxIdleCon)
log.Info("Database Connection Started")
err = OrderPreparedStatement(datasource)
if err != nil {
log.Error(err)
return err
}
err = TicketPreparedStatement(datasource)
if err != nil {
log.Error(err)
return err
}
log.Info("Prepared Statement Done")
//initting redis
InitRedis(redisMain)
log.Info("redis initted")
// init cache data
InitCacheMovieMarathon(context.Background())
log.Info("Init cache in-memory data done")
return nil
}
也就是代码部分:
flags := flag.NewFlagSet("user-auth", flag.PanicOnError)
flags.IntVar(&psqlMaxCon, "DB_MAX_CON", 20, "PSQL db max connection")
flags.IntVar(&psqlMaxIdleCon, "DB_MAX_IDLE_CON", 10, "PSQL db max idle connection")
flags.StringVar(&dsnMaster, "PG_DSN_MASTER", "", "PG SQL data source name for master")
flags.StringVar(&dsnSlave, "PG_DSN_SLAVE", "", "PG SQL data source name for slave")
//redis
flags.StringVar(&redisMain, "REDIS_MAIN", "", "redis main connection")
err := flagutil.SetFlagsFromEnv(flags, "TIX")