• 负载策略

    负载策略

    通过xorm.NewEngineGroup创建EngineGroup时,第三个参数为policies,我们可以通过该参数来指定Slave访问的负载策略。如创建EngineGroup时未指定,则默认使用轮询的负载策略。

    xorm中内置五种负载策略,分别为随机访问负载策略,权重随机访问负载策略,轮询访问负载策略,权重轮询访问负载策略和最小连接数访问负载策略。开发者也可以通过实现GroupPolicy接口,来实现自定义负载策略。

    • 随机访问负载策略
    1. import (
    2. _ "github.com/lib/pq"
    3. "github.com/xormplus/xorm"
    4. )
    5. var eg *xorm.EngineGroup
    6. func main() {
    7. conns := []string{
    8. "postgres://postgres:root@localhost:5432/test?sslmode=disable;",
    9. "postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
    10. "postgres://postgres:root@localhost:5432/test2?sslmode=disable",
    11. }
    12. var err error
    13. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RandomPolicy())
    14. }
    • 权重随机访问负载策略
    1. import (
    2. _ "github.com/lib/pq"
    3. "github.com/xormplus/xorm"
    4. )
    5. var eg *xorm.EngineGroup
    6. func main() {
    7. conns := []string{
    8. "postgres://postgres:root@localhost:5432/test?sslmode=disable;",
    9. "postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
    10. "postgres://postgres:root@localhost:5432/test2?sslmode=disable",
    11. }
    12. var err error
    13. //此时设置的test1数据库和test2数据库的随机访问权重为2和3
    14. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRandomPolicy([]int{2, 3}))
    15. }
    • 轮询访问负载策略
    1. import (
    2. _ "github.com/lib/pq"
    3. "github.com/xormplus/xorm"
    4. )
    5. var eg *xorm.EngineGroup
    6. func main() {
    7. conns := []string{
    8. "postgres://postgres:root@localhost:5432/test?sslmode=disable;",
    9. "postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
    10. "postgres://postgres:root@localhost:5432/test2?sslmode=disable",
    11. }
    12. var err error
    13. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RoundRobinPolicy())
    14. }
    • 权重轮询访问负载策略
    1. import (
    2. _ "github.com/lib/pq"
    3. "github.com/xormplus/xorm"
    4. )
    5. var eg *xorm.EngineGroup
    6. func main() {
    7. conns := []string{
    8. "postgres://postgres:root@localhost:5432/test?sslmode=disable;",
    9. "postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
    10. "postgres://postgres:root@localhost:5432/test2?sslmode=disable",
    11. }
    12. var err error
    13. //此时设置的test1数据库和test2数据库的轮询访问权重为2和3
    14. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRoundRobinPolicy([]int{2, 3}))
    15. }
    • 最小连接数访问负载策略
    1. import (
    2. _ "github.com/lib/pq"
    3. "github.com/xormplus/xorm"
    4. )
    5. var eg *xorm.EngineGroup
    6. func main() {
    7. conns := []string{
    8. "postgres://postgres:root@localhost:5432/test?sslmode=disable;",
    9. "postgres://postgres:root@localhost:5432/test1?sslmode=disable;",
    10. "postgres://postgres:root@localhost:5432/test2?sslmode=disable",
    11. }
    12. var err error
    13. eg, err = xorm.NewEngineGroup("postgres", conns, xorm.LeastConnPolicy())
    14. }
    • 自定义负载策略

    你也可以通过实现 GroupPolicy 接口来实现自定义负载策略。

    1. type GroupPolicy interface {
    2. Slave(*EngineGroup) *Engine
    3. }