This commit is contained in:
Your Name 2023-05-15 18:03:28 +08:00
parent 3f08420659
commit 8d384e3d20
40 changed files with 5 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import javax.annotation.Resource;
@Component
@RabbitListener(queues = "apiServers")
public class Receiver {
//心跳接收端关键代码
@Autowired
private DataServerStore dataServerStore;
@RabbitHandler

View File

@ -13,12 +13,14 @@ public class DataServerStore {
public synchronized void receivedHeart(String addr) {
this.serverHeart.put(addr, System.currentTimeMillis() / 1000);
// 保存服务端心跳
}
public Map<String,Long> getServerHeart(){
return this.serverHeart;
}
Set<String> getServerList() {
return this.serverHeart.keySet();
// 获取服务器列表
}
public String selectRandomServer() {
@ -28,5 +30,6 @@ public class DataServerStore {
}
int index = (int) (Math.random() * serverList.size());
return serverList.toArray()[index].toString();
//随机选择一个服务器
}
}

View File

@ -9,6 +9,7 @@ import java.net.InetAddress;
@Component
public class Sender {
// 心跳发送端关键代码
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired

View File