Commit 789b2919 authored by zengtianlai3's avatar zengtianlai3

befor update

parent 5f7b7eda
package iot.sixiang.license.balance;
import iot.sixiang.license.entity.Server;
import iot.sixiang.license.service.ServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;
@Component
@Slf4j
public class BalanceManager {
@Autowired
private ServerService serverService;
private Map<String, Server> allServers = null;
public BalanceManager() {
allServers = new HashMap<String, Server>();
}
@PostConstruct
public void init() {
List<Server> servers = serverService.getServerList(0, 20);
for (Server server : servers) {
String serverIp = server.getServerIp();
allServers.put(serverIp, server);
}
}
public Server getBalanceServer() {
int count = allServers.size();
if (count == 0) {
return null;
} else {
Random random = new Random();
int index = random.nextInt(count);
List<Server> servers = new ArrayList<>(allServers.values());
return servers.get(index);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment