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}))
}
  • 最小接続ポリシー
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())
}

おすすめ

転載: www.cnblogs.com/yangxinpython/p/12717964.html