创建引擎组时,我们可以指示特殊的负载平衡策略。xorm上有5个负载平衡。它们是RandomPolicy,WeightRandomPolicy,RoundRobinPolicy,WeightRoundRobinPolicy和LeastConnPolicy。您还可以根据GroupPolicy界面实施自己的策略。
import (
    _ "github.com/lib/pq"
    "xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
	conns := []string{
		"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
	}
    
    var err error
	eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RandomPolicy())
}
import (
    _ "github.com/lib/pq"
    "xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
    conns := []string{
		"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
	}
    
	var err error
	// set the weights
	eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRandomPolicy([]int{2, 3}))
}
import (
    _ "github.com/lib/pq"
    "xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
    conns := []string{
		"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
	}
    
    var err error
	eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RoundRobinPolicy())
}
import (
    _ "github.com/lib/pq"
    "xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
    conns := []string{
		"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
	}
    
    var err error
    // set the weights
	eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRoundRobinPolicy([]int{2, 3}))
}
import (
    _ "github.com/lib/pq"
    "xorm.io/xorm"
)
var eg *xorm.EngineGroup
func main() {
    conns := []string{
		"postgres://postgres:root@localhost:5432/test?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
		"postgres://postgres:root@localhost:5432/test2?sslmode=disable",
	}
    
    var err error
	eg, err = xorm.NewEngineGroup("postgres", conns, xorm.LeastConnPolicy())
}原文:https://www.cnblogs.com/yangxinpython/p/12717964.html