图书馆管理系统 Netbeans IDE 11.3 servlet+SOAP+vue 分布式系统,多服务器多数据库
学校作业,要求:
是分布式系统课的作业,要求要有容错性,多服务器,多数据库,坏了还可以运行。
老师的限制很多,不能用springboot,不能用数据库主从同步,需要自己完成。
最终效果:
具有各种错误提示功能
数据库设计:
前端:
加密:
后端:
架构:
load blancer的实现:
数据同步:
就放一下做Loadblancer功能的代码吧:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
ArrayList<String> ips = new ArrayList<String>(){
{
add("172.20.10.5");
add("172.20.10.3");
}
};
public String roundRobin(){
String serverIp;
synchronized(index){
if (index >= ips.size()){
index = 0;
}
serverIp= ips.get(index);
index ++;
}
return serverIp;
}
public static boolean ping(String ipAddress) throws Exception {
int timeOut = 3000 ; // 超时应该在3钞以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的,false则不可。
return status;
}
public String getIP(){
try {
String ip=roundRobin();
if(ping(ip)==true){
return ip;
}
else{
return getIP();
}
} catch (Exception ex) {
}
return "";
}