Exemple #1
0
func New(servicename, serviceport string) (*Ring, error) {
	// XXX 在Mac上调试时,Mac没有eth0接口,会导致ring.New()失败。
	// 建议增加一个参数,允许用户传入指定的Interface(因此用户程序可以将该配置写入配置文件)。
	// 目前考虑到API兼容性,在上述方法获得一致同意前,不改动API,而是从环境变量读取。
	iface, ok := os.LookupEnv("RING_SERVER_INTERFACE")
	if !ok {
		iface = "eth0"
	}
	localIp, err := externalIP(iface)
	if err != nil {
		return nil, err
	}
	mc, err := cache.NewCache("memory", `{"interval":60}`)
	if err != nil {
		return nil, err
	}

	serverId := localIp + ":" + serviceport + ":" + strconv.Itoa(os.Getpid()) + ":" + strconv.FormatInt(startTime, 10)

	r := Ring{
		selfServerId: serverId,
		servicename:  servicename,
		hashring:     hashring.New([]string{serverId}),
		stop:         make(chan struct{}),
		cache:        mc,
	}
	r.register()
	r.subscribe()
	return &r, nil
}
Exemple #2
0
func (this *Ring) refreshHashring() {
	var serverIds []string
	serverIds = append(serverIds, this.selfServerId)
	hs, err := service.Get(this.servicename)

	for err != nil {
		errEtcd := err.(*etcd.EtcdError)
		logger.Println("Lost etcd registrationg for", this.servicename, ":", errEtcd.ErrorCode)
		time.Sleep(1 * time.Second)
		hs, err = service.Get(this.servicename)
		if err == nil {
			logger.Println("Recover etcd connection for", this.servicename)
		}
	}

	for idx, h := range hs {
		logger.Printf("No.%v host %v\n", idx, h.Name)
		serverIds = append(serverIds, h.Name)
	}

	this.Lock()
	defer this.Unlock()
	this.hashring = hashring.New(serverIds)
	this.cache.ClearAll()
}