Search in sources :

Example 6 with AccessZooKeeper

use of ngse.org.AccessZooKeeper in project MSEC by Tencent.

the class RefreshOddCapacity method exec.

public JsonRPCResponseBase exec(CapacityRequest request) {
    JsonRPCResponseBase response = new JsonRPCResponseBase();
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    Logger logger = Logger.getLogger(RefreshOddCapacity.class);
    //把ip列表刷新到LB的服务器里并下发给各个客户端
    AccessZooKeeper azk = new AccessZooKeeper();
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setStatus(100);
        response.setMessage("db connect failed");
        return response;
    }
    try {
        //step2: 获得该服务下所有enabled的IP,写入LB系统
        ArrayList<IPPortPair> ips = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
        if (ips == null) {
            response.setStatus(100);
            response.setMessage("get ip info from db failed");
            return response;
        }
        logger.info("get enabled IP, they will be write to LB server, number:" + ips.size());
        result = LoadBalance.writeOneServiceConfigInfo(azk, request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name(), false, ips);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        logger.info("write into LB server successfully.");
        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();
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) JsonRPCResponseBase(ngse.org.JsonRPCResponseBase) IPPortPair(beans.request.IPPortPair) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Example 7 with AccessZooKeeper

use of ngse.org.AccessZooKeeper 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)7 AccessZooKeeper (ngse.org.AccessZooKeeper)7 Logger (org.apache.log4j.Logger)7 DBUtil (ngse.org.DBUtil)6 ArrayList (java.util.ArrayList)5 JsonRPCResponseBase (ngse.org.JsonRPCResponseBase)4 CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 CapacityDetailInfo (beans.dbaccess.CapacityDetailInfo)1 SecondLevelService (beans.dbaccess.SecondLevelService)1 QueryCapacityDetailResponse (beans.response.QueryCapacityDetailResponse)1 QueryCapacityListResponse (beans.response.QueryCapacityListResponse)1 QueryConfigInLBResponse (beans.response.QueryConfigInLBResponse)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1