use of beans.request.IPPortPair in project MSEC by Tencent.
the class LoadBalance method writeOneServiceConfigInfo.
//写一个服务的IP信息到LB
public static String writeOneServiceConfigInfo(AccessZooKeeper azk, String svcname, boolean isStandard, ArrayList<IPPortPair> iplist) throws Exception {
Logger logger = Logger.getLogger(LoadBalance.class);
/*
logger.info("generate IP->[port list] map...");
Map<String, String> map = new HashMap<String, String>();// ip -> ports
for (int i = 0; i < iplist.size(); i++) {
IPPortPair pair = iplist.get(i);
String ports = map.get(pair.getIp());
if (ports == null)
{
ports = "" + pair.getPort() ;
map.put(pair.getIp(), ports);
}
else
{
ports = ports + ", " + pair.getPort() ;
map.remove(pair.getIp());
map.put(pair.getIp(), ports);
}
}
StringBuffer data = new StringBuffer();
data.append("{ \"IPInfo\":["); //ipinfo是一个数组
int eleIndex = 0;
for (Map.Entry<String, String> entry : map.entrySet()) {
if (eleIndex > 0)
{
data.append(","); //与前面的数组元素分割起来
}
//数组的每一个元素,是一个对象,包括w t ip ports四个字段
data.append("{");
data.append("\"w\":100, \"t\":\"all\", \"IP\":\"");
data.append(entry.getKey());
//ports也是一个数组
data.append("\",\"ports\":[");
data.append(entry.getValue());
data.append("]");
data.append("}");//一个对象,或者说一个数组元素结束
eleIndex++;
}
data.append("]}");
*/
StringBuffer data = new StringBuffer();
if (isStandard) {
data.append("{ \"Policy\":\"standard\"");
} else {
data.append("{ \"Policy\":\"odd\"");
}
//ipinfo是一个数组
data.append(", \"IPInfo\":[");
int eleIndex = 0;
for (int i = 0; i < iplist.size(); ++i) {
if (eleIndex > 0) {
//与前面的数组元素分割起来
data.append(",");
}
//数组的每一个元素,是一个对象,包括w t ip ports四个字段
data.append("{");
IPPortPair oneEle = iplist.get(i);
String comm_proto = oneEle.getComm_proto();
if (comm_proto.equals("tcp and udp")) {
comm_proto = "all";
}
data.append("\"w\":100, \"t\":\"" + comm_proto + "\", \"IP\":\"");
data.append(oneEle.getIp());
//ports也是一个数组
data.append("\",\"ports\":[");
data.append(oneEle.getPort());
data.append("]");
//一个对象,或者说一个数组元素结束
data.append("}");
eleIndex++;
}
data.append("]}");
String path = "/nameservice/" + svcname;
logger.info("write LB server: path=" + path);
logger.info("data=" + data.toString());
String result = azk.write(path, data.toString().getBytes());
if (result == null || !result.equals("success")) {
logger.error("LB write failed:" + result);
return result;
}
return "success";
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class LoadBalance method writeIPConfigInfo.
//以IP为key的配置信息写入LB,whiteNameList为需要写的白名单IP列表,可以为null
public static String writeIPConfigInfo(AccessZooKeeper azk, ArrayList<String> whiteNameList) throws Exception {
Logger logger = Logger.getLogger(LoadBalance.class);
DBUtil util = new DBUtil();
if (util.getConnection() == null) {
return "db connect failed";
}
logger.info("write IP key info into LB ...");
HashMap<String, HashSet<String>> ip2Svcs = new HashMap<String, HashSet<String>>();
try {
//获得所有服务
ArrayList<SecondLevelService> services = getAllService(util);
if (services == null) {
logger.error("get all service failed");
return "get service list from db failed";
}
//对每个服务获取下属的IP
for (int i = 0; i < services.size(); i++) {
SecondLevelService svc = services.get(i);
ArrayList<IPPortPair> ips = getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
if (ips == null) {
logger.error("get ip list for " + svc.getSecond_level_service_name() + " failed.");
return "get ip list from db failed";
}
logger.info("get ip list for " + svc.getSecond_level_service_name() + " successfully.ip number" + ips.size());
//生成IP为key、servicename列表为value的反向映射关系
geneIP2Svcs(svc.getFirst_level_service_name() + "." + svc.getSecond_level_service_name(), ips, ip2Svcs);
}
logger.info("gene IP->service map successfully, map size:" + ip2Svcs.size());
if (whiteNameList == null) {
//删除掉整个ip配置信息
String path = "/nodeservices";
azk.deleteRecursive(path);
logger.info("delete whole tree:" + path);
} else {
//删除掉白名单中的ip配置信息
for (int i = 0; i < whiteNameList.size(); i++) {
String path = "/nodeservices/" + whiteNameList.get(i);
azk.deleteRecursive(path);
logger.info("delete node:" + path);
}
}
//往LB写入ip为key的数据
for (Map.Entry<String, HashSet<String>> entry : ip2Svcs.entrySet()) {
if (//如果只更新列表中的指定的IP列表
whiteNameList != null) {
if (whiteNameList.indexOf(entry.getKey()) < 0) {
continue;
}
}
String path = "/nodeservices/" + entry.getKey();
String data = "{\"services\":[";
Iterator<String> it = entry.getValue().iterator();
int eleIndex = 0;
while (it.hasNext()) {
if (eleIndex > 0) {
//与前一个元素的分割
data += ",";
}
data += "\"" + it.next() + "\"";
eleIndex++;
}
data += "]}";
logger.info("write LB, path=" + path);
logger.info("data=" + data);
String result = azk.write(path, data.getBytes());
if (result == null || !result.equals("success")) {
logger.error("write LB failed:" + result);
return result;
}
}
return "success";
} finally {
util.releaseConn();
}
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class LoadBalance method writeServiceConfigInfo.
//把所有服务的配置信息写入LB
public static String writeServiceConfigInfo(AccessZooKeeper azk) throws Exception {
DBUtil util = new DBUtil();
Logger logger = Logger.getLogger(LoadBalance.class);
if (util.getConnection() == null) {
return "db connect failed";
}
logger.info("write IP information of ALL services into LB...");
try {
//删除掉整个配置信息
String path = "/nameservice";
azk.deleteRecursive(path);
logger.info("delete whole tree:" + path);
//获得所有服务
ArrayList<SecondLevelService> services = getAllService(util);
if (services == null) {
return "get standard service list from db failed";
}
logger.info("getAllService() OK, service number:" + services.size());
//对每个服务获取下属的IP
for (int i = 0; i < services.size(); i++) {
SecondLevelService svc = services.get(i);
ArrayList<IPPortPair> ips = getIPPortInfoByServiceName(svc.getFirst_level_service_name(), svc.getSecond_level_service_name(), util);
if (ips == null) {
return "get ip list from db failed";
}
logger.info("get ip for " + svc.getSecond_level_service_name() + " OK, ip number" + ips.size());
//写到 LB系统里
String result = writeOneServiceConfigInfo(azk, svc.getFirst_level_service_name() + "/" + svc.getSecond_level_service_name(), svc.getType().equals("standard"), ips);
if (result == null || !result.equals("success")) {
logger.error("write service IP information into LB failed!" + result);
return result;
}
logger.info("write to LB ok");
}
return "success";
} finally {
util.releaseConn();
}
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class AddBusiLogField method doAddBusiLogField.
private String doAddBusiLogField(BusiLogField request) {
Logger logger = Logger.getLogger(QueryBusinessLog.class);
String sJson = String.format("{\"modifyFieldsReq\":{\"appName\":null, \"operator\":\"ADD\"," + "\"fieldName\":\"%s\",\"fieldType\":\"%s\"}}", request.getField_name(), request.getField_type());
logger.info("request to log server:" + sJson);
String lens = String.format("%10d", sJson.length());
ArrayList<IPPortPair> ipPortPairs = QueryBusinessLog.getBusiLogSrvIPPort();
if (ipPortPairs == null || ipPortPairs.size() < 1) {
return "get log server ip failed";
}
log_srv_ip = ipPortPairs.get(0).getIp();
log_srv_port = ipPortPairs.get(0).getPort();
logger.info(String.format("log server:%s:%d", log_srv_ip, log_srv_port));
Socket socket = new Socket();
try {
socket.setSoTimeout(5000);
socket.connect(new InetSocketAddress(log_srv_ip, log_srv_port), 3000);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(lens.getBytes());
outputStream.write(sJson.getBytes());
socket.shutdownOutput();
//收应答包
// length field 10bytes
byte[] buf = new byte[10240];
int total_len = 0;
while (total_len < 10) {
int len = socket.getInputStream().read(buf, total_len, 10 - total_len);
if (len <= 0) {
return "receive json len failed.";
}
total_len += len;
}
int jsonLen = new Integer(new String(buf, 0, total_len).trim()).intValue();
// logger.error(String.format("response json string len:%d", jsonLen));
if (jsonLen > buf.length) {
return "response json is too long";
}
total_len = 0;
while (total_len < jsonLen) {
int len = socket.getInputStream().read(buf, total_len, jsonLen - total_len);
if (len <= 0) {
return "receive json string failed.";
}
total_len += len;
}
String jsonStr = new String(buf, 0, total_len);
logger.info("log server resp:" + jsonStr);
JSONObject jsonObject = new JSONObject(jsonStr);
jsonObject = jsonObject.getJSONObject("modifyFieldsRsp");
int status = jsonObject.getInt("ret");
if (status != 0) {
String message = jsonObject.getString("errmsg");
return message;
}
return "success";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
} finally {
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
}
;
}
}
}
use of beans.request.IPPortPair in project MSEC by Tencent.
the class DeleteBusiLogField method doDeleteBusiLogField.
private String doDeleteBusiLogField(BusiLogField request) {
Logger logger = Logger.getLogger(QueryBusinessLog.class);
String sJson = String.format("{\"modifyFieldsReq\":{\"appName\":null, \"operator\":\"DEL\"," + "\"fieldName\":\"%s\",\"fieldType\":\"%s\"}}", request.getField_name(), request.getField_type());
logger.info("request to log server:" + sJson);
String lens = String.format("%10d", sJson.length());
ArrayList<IPPortPair> ipPortPairs = QueryBusinessLog.getBusiLogSrvIPPort();
if (ipPortPairs == null || ipPortPairs.size() < 1) {
return "get log server ip failed";
}
log_srv_ip = ipPortPairs.get(0).getIp();
log_srv_port = ipPortPairs.get(0).getPort();
logger.info(String.format("log server:%s:%d", log_srv_ip, log_srv_port));
Socket socket = new Socket();
try {
socket.setSoTimeout(5000);
socket.connect(new InetSocketAddress(log_srv_ip, log_srv_port), 3000);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(lens.getBytes());
outputStream.write(sJson.getBytes());
socket.shutdownOutput();
//收应答包
// length field 10bytes
byte[] buf = new byte[10240];
int total_len = 0;
while (total_len < 10) {
int len = socket.getInputStream().read(buf, total_len, 10 - total_len);
if (len <= 0) {
return "receive json len failed.";
}
total_len += len;
}
int jsonLen = new Integer(new String(buf, 0, total_len).trim()).intValue();
// logger.error(String.format("response json string len:%d", jsonLen));
if (jsonLen > buf.length) {
return "response json is too long";
}
total_len = 0;
while (total_len < jsonLen) {
int len = socket.getInputStream().read(buf, total_len, jsonLen - total_len);
if (len <= 0) {
return "receive json string failed.";
}
total_len += len;
}
String jsonStr = new String(buf, 0, total_len);
logger.info("log server resp:" + jsonStr);
JSONObject jsonObject = new JSONObject(jsonStr);
jsonObject = jsonObject.getJSONObject("modifyFieldsRsp");
int status = jsonObject.getInt("ret");
if (status != 0) {
String message = jsonObject.getString("errmsg");
return message;
}
return "success";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
} finally {
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
}
;
}
}
}
Aggregations