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())
    }
  • 相关阅读:
    Azure 2 月新公布
    协合新能源康大海:我们每多发一度电,就为蓝天白云多贡献了一份力量
    Azure:陪伴你们,是我最长情的告白
    微软加速器上海首期启航,拓展云端智慧创新
    Azure杯年会Cosplay大赛,速来围观!
    Azure 1 月新公布
    Azure进阶攻略 | VS2015和Azure,想要在一起其实很容易
    Azure进阶攻略丨如何驾驭罢工的Linux虚机网卡?
    Azure 12 月新公布
    利用枚举找到列表中重复元素的索引
  • 原文地址:https://www.cnblogs.com/yangxinpython/p/12717964.html
Copyright © 2011-2022 走看看