Browse Source

feat:增加方法

wangchuanjin 2 years ago
parent
commit
fd4f65af83
1 changed files with 10 additions and 0 deletions
  1. 10 0
      etcd/etcd.go

+ 10 - 0
etcd/etcd.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"log"
+	"math/rand"
 	"os"
 	"os/signal"
 	"syscall"
@@ -54,6 +55,15 @@ func (e *etcd) register(key, value string) error {
 	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) {
 	client, err := getClient(e.endpoints)
 	if err != nil {