Search in sources :

Example 1 with QueryConfigInLBResponse

use of beans.response.QueryConfigInLBResponse in project MSEC by Tencent.

the class QueryConfigInLB method exec.

public QueryConfigInLBResponse exec(QueryConfigInLBRequest request) {
    QueryConfigInLBResponse response = new QueryConfigInLBResponse();
    Logger logger = Logger.getLogger(QueryConfigInLB.class);
    String svc = request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name();
    AccessZooKeeper azk = new AccessZooKeeper();
    try {
        //读LB系统里的配置信息
        byte[] ret = LoadBalance.readOneServiceConfigInfo(azk, svc);
        if (ret == null) {
            response.setStatus(100);
            response.setMessage("read failed.");
            return response;
        }
        String s = new String(ret, Charset.forName("UTF-8"));
        logger.error(s);
        //json格式解析
        ArrayList<IPPortPair> ip_list = new ArrayList<>();
        JSONObject obj = new JSONObject(s);
        JSONArray arr = obj.getJSONArray("IPInfo");
        for (//每一个IP
        int i = 0; //每一个IP
        i < arr.length(); //每一个IP
        ++i) {
            obj = arr.getJSONObject(i);
            JSONArray arr2 = obj.getJSONArray("ports");
            for (int j = 0; j < arr2.length(); j++) {
                //该IP上的每一个端口
                IPPortPair pair = new IPPortPair();
                pair.setPort(arr2.getInt(j));
                pair.setIp(obj.getString("IP"));
                ip_list.add(pair);
            }
        }
        response.setIp_list(ip_list);
        response.setStatus(0);
        response.setMessage("success");
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(100);
        response.setMessage(e.getMessage());
        return response;
    } finally {
        azk.disconnect();
    }
}
Also used : IPPortPair(beans.request.IPPortPair) JSONObject(org.json.JSONObject) QueryConfigInLBResponse(beans.response.QueryConfigInLBResponse) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Aggregations

IPPortPair (beans.request.IPPortPair)1 QueryConfigInLBResponse (beans.response.QueryConfigInLBResponse)1 ArrayList (java.util.ArrayList)1 AccessZooKeeper (ngse.org.AccessZooKeeper)1 Logger (org.apache.log4j.Logger)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1