【发布时间】:2015-12-29 23:01:03
【问题描述】:
有 2 台后端机器将提供相同的内容。我正在尝试创建一个循环设置和故障安全,以便如果一个出现故障,则应将请求发送给另一个。
至于循环赛,我的代码运行良好。
为了安全起见,我正在尝试将一个 IP 更改为某个无效 ip(没有运行任何服务),然后它应该连接到另一个。但它正在被挂断:
client = new Socket(ip, port);
可以为此做些什么?如果我没有在 2 秒内获得连接,我不想重试(如果这是 java 所做的,我可以减少重试超时吗?)。
另外,经过很长时间,它会抛出IllegalStateException(我完全没想到)
这是我的 Connect.java 构造函数:
//Variable declartion
private static int balancer = 0;
private static boolean[] down = {false, false};
private static int[] countDown = {0, 0};
private static final int maxRequests = 10000;
private static int countRequests = 0;
//Constructor
public Connect() throws NullPointerException{
int use = balancer;
int flag = 0;
countRequests = (countRequests + 1) % maxRequests;
if(countRequests == 0){
down[0] = down[1] = false;
}
if((balancer == 0 && down[0] == false) || down[1] == true){
ip = Config.IP1;
port = Config.PORT1;
use = 0;
System.out.println("balancer = 0");
}
else{
ip = Config.IP2;
port = Config.PORT2;
use = 1;
System.out.println("balancer = 1");
}
LOGGER.setLevel(Level.INFO);
// Single simple socket
try {
client = new Socket(ip, port);
System.out.println("client");
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
if(client == null || dos == null || dis == null){
throw new IOException("Connection could not be opened properly\n");
}
LOGGER.log(Level.INFO, "Connection to : " + ip + " Successfully opened");
balancer = (balancer == 0) ? 1 : 0;
countDown[use] = 0;
flag = 1;
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
countDown[use] ++;
if(countDown[use] >= 3){
down[use] = true;
}
LOGGER.log(Level.SEVERE, ": " + ip + " " + e.getMessage());
}
finally{ //if connection was not opened on first
if(flag == 0){
if(use == 0){
ip = Config.IP2;
port = Config.PORT2;
use = 1;
}
else{
ip = Config.IP1;
port = Config.PORT1;
use = 0;
}
try {
client = new Socket(ip, port);
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
if(client == null || dos == null || dis == null){
throw new IOException("Connection could not be opened properly\n");
}
LOGGER.log(Level.INFO, "Connection to " + ip + " Successfully opened");
balancer = (balancer == 0) ? 1 : 0;
countDown[use] = 0;
flag = 1;
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
countDown[use] ++;
if(countDown[use] >= 3){
down[use] = true;
}
LOGGER.log(Level.SEVERE, ": " + ip + " " + e.getMessage());
throw new NullPointerException("Connection could not be opened properly on both machines\n");
}
}
}
}
我所期望的是 IO/NullPointer-Exception 然后 catch 块将被执行
【问题讨论】:
-
出于好奇:除非这是出于“学习目的”;自己实现一些东西是什么感觉……以前已经做过很多次了;那么,这很难正确吗?您看,您的源代码表明您是 Java 的绝对初学者……所以:您真的认为重新发明轮子是获得实践的良好开端吗?
-
@Jägermeister,你为什么说,
hard是正确的。这是一个简单的实现,所以我想我会自己做,考虑到我们的系统不应该有任何单点故障。 -
处理多个分布式系统并不容易。如果您的系统包含更多代码,例如您发布的代码;这已经是失败了。很抱歉这么说;但是这段代码绝对是可怕的。
-
我自己标记了帖子重复...虽然我之前尝试过搜索但似乎没有使用正确的关键字。
-
@Jägermeister,实际上这是一个特定的场景,我们不会很快看到更多的后端机器(可能根本不会),所以这有点硬编码。我想知道你为什么说这段代码很糟糕(我想学习——我是初学者,好吧)或者有什么可以改进的。如果你有时间,我们可以讨论一下。