6ac3feb0624ba0393afdc2855020ccb487cdc4c8.svn-base 938 B

1234567891011121314151617181920212223242526272829303132333435
  1. package loadbalancer
  2. import (
  3. // "fmt"
  4. "github.com/longjoy/micro-go-course/section28/goods/pkg/discovery"
  5. "testing"
  6. )
  7. func TestHash(t *testing.T) {
  8. instances := make([]*discovery.InstanceInfo, 3)
  9. instances[0] = &discovery.InstanceInfo{}
  10. instances[0].Address = "192.168.1.1"
  11. instances[1] = &discovery.InstanceInfo{}
  12. instances[1].Address = "192.168.1.2"
  13. instances[2] = &discovery.InstanceInfo{}
  14. instances[2].Address = "192.168.1.3"
  15. nodeWeight := make(map[string]int)
  16. instanceMap := make(map[string]*discovery.InstanceInfo)
  17. for i := 0; i < len(instances); i++ {
  18. instance := instances[i]
  19. nodeWeight[instance.Address] = i + 1
  20. instanceMap[instance.Address] = instance
  21. println("info:" + instance.Address)
  22. }
  23. // 建立Hash环
  24. hash := NewHashRing()
  25. // 添加各个服务实例到环上
  26. hash.AddNodes(nodeWeight)
  27. // 根据请求的key来获取对应的服务实例
  28. host := hash.GetNode("2")
  29. println("ddd" + host)
  30. }