zoukankan      html  css  js  c++  java
  • go xorm,负载均衡

    创建引擎组时,我们可以指示特殊的负载平衡策略。xorm上有5个负载平衡。它们是RandomPolicyWeightRandomPolicyRoundRobinPolicyWeightRoundRobinPolicyLeastConnPolicy您还可以根据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}))
    }
    
    • 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
    	eg, err = xorm.NewEngineGroup("postgres", conns, xorm.RoundRobinPolicy())
    }
    
    • WeightRoundRobinPolicy
    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}))
    }
    
    • 最低Conn政策
    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())
    }
  • 相关阅读:
    关于JavaWeb项目汉字乱码问题
    使用pipenv
    python使用imap-tools模块下载邮件中的附件
    Python新增功能, 函数的参数类型提示.
    centos83+django3.1+ASGI+nginx部署.
    python3.9 pip本身的升级
    windows+django3.1+ASGI+nginx部署
    k8s 单节点开发环境用hostPath配置mysql的持久化存储
    Rust的设计中为什么要区分不可变变量和常量?
    Vscode + Python + Django开发环境常见问题
  • 原文地址:https://www.cnblogs.com/yangxinpython/p/12717964.html
Copyright © 2011-2022 走看看