Skip to content

Commit 7611484

Browse files
committed
随机获取ApiKey优化
1 parent 4e7ca9f commit 7611484

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/main/java/com/ydyno/config/MyAiConfig.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.springframework.boot.context.properties.ConfigurationProperties;
2121
import org.springframework.context.annotation.Configuration;
2222
import org.springframework.stereotype.Component;
23-
import java.util.Collections;
2423
import java.util.List;
24+
import java.util.Random;
2525

2626
/**
2727
* @author Zheng Jie
@@ -66,11 +66,14 @@ public class MyAiConfig {
6666
public String getApiKey() {
6767
// 转为 List
6868
List<String> keyList = ListUtil.toList(keys.split(","));
69-
// 打乱顺序
70-
if(keyList.size() > 1){
71-
Collections.shuffle(keyList);
69+
if(keyList.size() == 1){
70+
return keyList.get(0);
7271
}
72+
// 打乱顺序
73+
Random random = new Random();
74+
// 获取0~list.size()之间的随机数
75+
int index = random.nextInt(keyList.size());
7376
// 获取一个 key
74-
return keyList.get(0);
77+
return keyList.get(index);
7578
}
7679
}

0 commit comments

Comments
 (0)