Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
            页面加载耗时 0.00 毫秒·物理内存 156.5MB ·虚拟内存 1438.3MB
        
        欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
        
        
        通过 xorm.NewEngineGroup 创建 EngineGroup 时,第三个参数为 policies,我们可以通过该参数来指定 Slave 访问的负载策略。如创建 EngineGroup 时未指定,则默认使用轮询的负载策略。
xorm 中内置五种负载策略,分别为随机访问负载策略、权重随机访问负载策略、轮询访问负载策略、权重轮询访问负载策略和最小连接数访问负载策略。开发者也可以通过实现 GroupPolicy 接口,来实现自定义负载策略。
import (
    _ "github.com/lib/pq"
    "github.com/xormplus/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"
    "github.com/xormplus/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
    //此时设置的test1数据库和test2数据库的随机访问权重为2和3
    eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRandomPolicy([]int{2, 3}))
}
import (
    _ "github.com/lib/pq"
    "github.com/xormplus/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"
    "github.com/xormplus/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
    //此时设置的test1数据库和test2数据库的轮询访问权重为2和3
    eg, err = xorm.NewEngineGroup("postgres", conns, xorm.WeightRoundRobinPolicy([]int{2, 3}))
}
import (
    _ "github.com/lib/pq"
    "github.com/xormplus/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())
}
你也可以通过实现 GroupPolicy 接口来实现自定义负载策略。
type GroupPolicy interface {
    Slave(*EngineGroup) *Engine
}
                        
                        
            
            
            xorm 支持将一个 struct 映射为数据库中对应的一张表。名称映射规则主要负责结构体名称到表名和结构体 field 到表字段的名称映射。由 core.IMapper 接口的实现者来管理,xorm内置了三种IMa ...