當前位置: 首頁>>代碼示例>>Golang>>正文


Golang clientv3.NewCluster函數代碼示例

本文整理匯總了Golang中github.com/coreos/etcd/clientv3.NewCluster函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewCluster函數的具體用法?Golang NewCluster怎麽用?Golang NewCluster使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewCluster函數的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestMemberRemove

func TestMemberRemove(t *testing.T) {
	defer testutil.AfterTest(t)

	clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
	defer clus.Terminate(t)

	capi := clientv3.NewCluster(clus.Client(1))
	resp, err := capi.MemberList(context.Background())
	if err != nil {
		t.Fatalf("failed to list member %v", err)
	}

	_, err = capi.MemberRemove(context.Background(), resp.Members[0].ID)
	if err != nil {
		t.Fatalf("failed to remove member %v", err)
	}

	resp, err = capi.MemberList(context.Background())
	if err != nil {
		t.Fatalf("failed to list member %v", err)
	}

	if len(resp.Members) != 2 {
		t.Errorf("number of members = %d, want %d", len(resp.Members), 2)
	}
}
開發者ID:QuentinPerez,項目名稱:etcd,代碼行數:26,代碼來源:cluster_test.go

示例2: TestMemberUpdate

func TestMemberUpdate(t *testing.T) {
	defer testutil.AfterTest(t)

	clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
	defer clus.Terminate(t)

	capi := clientv3.NewCluster(clus.RandClient())
	resp, err := capi.MemberList(context.Background())
	if err != nil {
		t.Fatalf("failed to list member %v", err)
	}

	urls := []string{"http://127.0.0.1:1234"}
	_, err = capi.MemberUpdate(context.Background(), resp.Members[0].ID, urls)
	if err != nil {
		t.Fatalf("failed to update member %v", err)
	}

	resp, err = capi.MemberList(context.Background())
	if err != nil {
		t.Fatalf("failed to list member %v", err)
	}

	if !reflect.DeepEqual(resp.Members[0].PeerURLs, urls) {
		t.Errorf("urls = %v, want %v", urls, resp.Members[0].PeerURLs)
	}
}
開發者ID:XiangrongFan,項目名稱:etcd,代碼行數:27,代碼來源:cluster_test.go

示例3: ExampleCluster_memberLeader

func ExampleCluster_memberLeader() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints,
		DialTimeout: dialTimeout,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()

	capi := clientv3.NewCluster(cli)

	resp, err := capi.MemberLeader(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("leader:", resp.Name)
	// leader: infra1
}
開發者ID:vsayer,項目名稱:etcd,代碼行數:19,代碼來源:example_cluster_test.go

示例4: ExampleCluster_memberList

func ExampleCluster_memberList() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints,
		DialTimeout: dialTimeout,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()

	capi := clientv3.NewCluster(cli)

	resp, err := capi.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("members:", len(resp.Members))
	// members: 3
}
開發者ID:vsayer,項目名稱:etcd,代碼行數:19,代碼來源:example_cluster_test.go

示例5: ExampleCluster_memberAdd

func ExampleCluster_memberAdd() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints[:2],
		DialTimeout: dialTimeout,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()

	capi := clientv3.NewCluster(cli)

	peerURLs := endpoints[2:]
	mresp, err := capi.MemberAdd(context.Background(), peerURLs)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("added member.PeerURLs:", mresp.Member.PeerURLs)
	// added member.PeerURLs: [http://localhost:32380]
}
開發者ID:vsayer,項目名稱:etcd,代碼行數:20,代碼來源:example_cluster_test.go

示例6: ExampleCluster_memberRemove

func ExampleCluster_memberRemove() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints[1:],
		DialTimeout: dialTimeout,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()

	capi := clientv3.NewCluster(cli)

	resp, err := capi.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	_, err = capi.MemberRemove(context.Background(), resp.Members[0].ID)
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:vsayer,項目名稱:etcd,代碼行數:22,代碼來源:example_cluster_test.go

示例7: ExampleCluster_memberUpdate

func ExampleCluster_memberUpdate() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints,
		DialTimeout: dialTimeout,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()

	capi := clientv3.NewCluster(cli)

	resp, err := capi.MemberList(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	peerURLs := []string{"http://localhost:12378"}
	_, err = capi.MemberUpdate(context.Background(), resp.Members[0].ID, peerURLs)
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:vsayer,項目名稱:etcd,代碼行數:23,代碼來源:example_cluster_test.go

示例8: TestMemberRemove

func TestMemberRemove(t *testing.T) {
	defer testutil.AfterTest(t)

	clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
	defer clus.Terminate(t)

	capi := clientv3.NewCluster(clus.Client(1))
	resp, err := capi.MemberList(context.Background())
	if err != nil {
		t.Fatalf("failed to list member %v", err)
	}

	rmvID := resp.Members[0].ID
	// indexes in capi member list don't necessarily match cluster member list;
	// find member that is not the client to remove
	for _, m := range resp.Members {
		mURLs, _ := types.NewURLs(m.PeerURLs)
		if !reflect.DeepEqual(mURLs, clus.Members[1].ServerConfig.PeerURLs) {
			rmvID = m.ID
			break
		}
	}

	_, err = capi.MemberRemove(context.Background(), rmvID)
	if err != nil {
		t.Fatalf("failed to remove member %v", err)
	}

	resp, err = capi.MemberList(context.Background())
	if err != nil {
		t.Fatalf("failed to list member %v", err)
	}

	if len(resp.Members) != 2 {
		t.Errorf("number of members = %d, want %d", len(resp.Members), 2)
	}
}
開發者ID:XiangrongFan,項目名稱:etcd,代碼行數:37,代碼來源:cluster_test.go

示例9: GetLeader

// GetLeader returns the index of leader and error if any.
func (c *cluster) GetLeader() (int, error) {
	if c.v2Only {
		return 0, nil
	}
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   c.GRPCURLs,
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		return 0, err
	}
	defer cli.Close()
	clus := clientv3.NewCluster(cli)
	mem, err := clus.MemberLeader(context.Background())
	if err != nil {
		return 0, err
	}
	for i, name := range c.Names {
		if name == mem.Name {
			return i, nil
		}
	}
	return 0, fmt.Errorf("no leader found")
}
開發者ID:lrita,項目名稱:etcd,代碼行數:25,代碼來源:cluster.go


注:本文中的github.com/coreos/etcd/clientv3.NewCluster函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。