wangchuanjin 2 anni fa
parent
commit
fd4f65af83
1 ha cambiato i file con 10 aggiunte e 0 eliminazioni
  1. 10 0
      etcd/etcd.go

+ 10 - 0
etcd/etcd.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"context"
 	"fmt"
 	"fmt"
 	"log"
 	"log"
+	"math/rand"
 	"os"
 	"os"
 	"os/signal"
 	"os/signal"
 	"syscall"
 	"syscall"
@@ -54,6 +55,15 @@ func (e *etcd) register(key, value string) error {
 	return nil
 	return nil
 }
 }
 
 
+func (e *etcd) GetOne(key string) string {
+	list, err := e.Get(key)
+	if err != nil || list == nil || len(list) == 0 {
+		return ""
+	}
+	rander := rand.New(rand.NewSource(time.Now().UnixNano()))
+	return list[rander.Intn(len(list))]
+}
+
 func (e *etcd) Get(key string) ([]string, error) {
 func (e *etcd) Get(key string) ([]string, error) {
 	client, err := getClient(e.endpoints)
 	client, err := getClient(e.endpoints)
 	if err != nil {
 	if err != nil {