我正在开发网站构建器并将每个网站数据存储在不同的数据库中 我的问题是如何正确有效地处理多个数据库连接。 所有数据库和代码都在同一台服务器
答案 0 :(得分:2)
我已经创建了自己的方式连接到多个连接数据库。
首先我为postgre创建基本文件:
type PostgreHost struct {
Driver string
Database string
Username string
Ssl string
Password string
}
type PostgreSystem interface {
Init()
Connect() (*sqlx.DB, error)
}
var logger *log.Logger
func (p PostgreHost) Init() {
logger = log.New(os.Stderr,
"Postgre",
log.Ldate|log.Ltime|log.Lshortfile)
}
func (p *PostgreHost) Connect() (*sqlx.DB, error) {
connection := fmt.Sprintf("user=%v password= %v dbname=%v sslmode=%v", p.Username, p.Password, p.Database, p.Ssl)
db, err := sqlx.Connect(
p.Driver,
connection)
if err != nil {
logger.Fatal(err)
return nil, err
}
return db, nil
}
func GetPostgreDb(postgre PostgreSystem) (*sqlx.DB, error) {
return postgre.Connect()
}
然后调用它来创建连接列表,如this:
//we create different types of databse connection here
func SystemConnection() map[string]interface{} {
listConnection := make(map[string]interface{})
var err error
// create redis connection
redisConn := RedisHost{
Address: "localhost:6379",
Password: "",
DB: 0,
}
redisConnection, err := redisConn.Connect()
if err != nil {
panic(err)
}
// create postgre connection
postgreConn := PostgreHost{
Driver: "postgres",
Database: "postgres",
Username: "postgres",
Ssl: "disable",
Password: "root",
}
// you can create your another connection here :
postgreConn2 := PostgreHost{
Driver: "postgres",
Database: "postgres",
Username: "postgres",
Ssl: "disable",
Password: "root",
}
postgreConnection, err := GetPostgreDb(&postgreConn)
if err != nil {
panic(err)
}
postgreConnection2, err := GetPostgreDb(&postgreConn2)
if err != nil {
panic(err)
}
listConnection["redis"] = redisConnection
listConnection["postgre"] = postgreConnection
listConnection["postgre2"] = postgreConnection2
return listConnection
}
最后从地图调用所有连接:
//getting list of all the connection.
listConnection := database.SystemConnection()
//getting redis connection convert it from interface to *redisClient.
redisConn := listConnection["redis"].(*redis.Client)
// get postgre connection.
postgreConn := listConnection["postgre"].(*sqlx.DB)
postgreConn2 := listConnection["postgre2"].(*sqlx.DB)
您可以从here获取所有源代码。它仍然在进步,但希望你能得到这个想法。希望它有所帮助。
答案 1 :(得分:2)
map[string]Context
)获取上下文。