static {
   SNUrl =
       StringUtil.isEmpty(Env.get("SNUrl"))
           ? "http://g3.letv.com/stat/hosts?cdnid=999999"
           : Env.get("SNUrl");
   NPUrl =
       StringUtil.isEmpty(Env.get("NPUrl"))
           ? "http://g3.letv.com/stat/nodes?format=1"
           : Env.get("NPUrl");
 }
/**
 * 直播rttp业务流程控制
 *
 * @author kk
 */
public abstract class AbstractLiveRttpServlet extends BaseServlet implements IliveRttpReceiver {

  private static final Logger LOG = LoggerFactory.getLogger(AbstractLiveRttpServlet.class);
  public static final int KAFKA_POOL_SIZE_FOR_LIVE_RTTP =
      StringUtils.isEmpty(Env.get("kafkaPoolSizeForLiveRttp"))
          ? 1
          : Integer.parseInt(Env.get("kafkaPoolSizeForLiveRttp"));
  protected static final SequenceUtil sequence = new SequenceUtil();
  private ReceiverExceptionHandler exceptionHandler = new LiveExceptionHandler(); // 指定异常处理机制

  @Override
  protected void execute(HttpServletRequest request, HttpServletResponse response) {

    final JsonWapper wapper = new JsonWapper();
    String flag = Constants.RESULT_SUCCESS;
    String body = "";
    final String uri = request.getRequestURI();
    try {
      body = getParameters(request);
      addStormData(body);
    } catch (Throwable e) {
      flag = Constants.RESULT_FAILED;
      ReceiverExceptionData exceptionData = new ReceiverExceptionData();
      exceptionHandler.handleEventException(e, body, exceptionData, uri);
      JSONObject jsonObject = wapper.nextItem();
      jsonObject.put(body, exceptionData.getCode().value);
      wapper.endItem(jsonObject);
    } finally {
      try {
        wapper.writeStore(response, flag);
      } catch (Exception e) {
        LOG.error("writeResponse err: ", e);
      }
    }
  }

  private String getParameters(HttpServletRequest request) throws Exception {

    String p = "";
    String method = request.getMethod();
    if ("GET".equals(method)) {
      p = URLDecoder.decode(convertToUTF8(request.getQueryString()), "UTF-8");
    } else if ("POST".equals(method)) {
      p = getBodyString(request.getInputStream());
    }
    return p;
  }

  @Override
  public void addCacheData(Object param) throws Exception {

    // 暂时无操作
  }
}
예제 #3
0
/**
 * 直播flv服务端入口
 *
 * @author kk
 */
public class FlvSSServlet extends AbstractBaseLiveMultipleServlet {

  private static final Logger LOG = LoggerFactory.getLogger(FlvSSServlet.class);

  private static final String FLV_TOPIC = Env.get("liveFlvServerTopic");

  private static enum PARAM {
    timestamp,
    streamid,
    bw,
    cip,
    sip,
    uuid
  };

  private static final String KEY_PREFIX = "flv";
  private static final String intranetTag = "999";
  private static final PARAM[] params = PARAM.values();

  @Override
  public boolean validateParams(Map<String, String> paramMap) throws ValidateException {

    if (null == paramMap || paramMap.size() < 6) {
      throw new ValidateException("args miss");
    }
    for (int i = 0; i < params.length; i++) {
      if (StringUtils.isEmpty(paramMap.get(params[i].toString()))) {
        throw new ValidateException(params[i].toString() + " miss");
      }
    }
    return true;
  }

  @Override
  public String[] splitParams(String str) throws Exception {

    return str.split("\n");
  }

  @Override
  public void handleParams(Map<String, String> param) throws Exception {

    // 默认内网
    String pid = intranetTag;
    String type = "out";

    String slogtime = param.get(PARAM.timestamp.toString()); // 时间戳
    param.put(PARAM.timestamp.toString() + "_bak", slogtime);
    String clientip = param.get(PARAM.cip.toString()); // 客户端IP
    String serverip = param.get(PARAM.sip.toString()); // 服务端IP
    if (NodeMapping.SPMapping.containsKey(clientip)) { // 如果可以映射成功,代表是外网
      type = "in";
      if (NodeMapping.SPMapping.containsKey(serverip)) {
        pid = NodeMapping.SPMapping.get(serverip);
      } else {
        pid = "0";
      }
    }
    param.put(PARAM.timestamp.toString(), slogtime);
    param.put("pid", pid);
    // 带宽计算
    long flow = Long.valueOf(param.get(PARAM.bw.toString())); // 一分钟内的流量合
    long bw = flow * 8 / 60;
    param.put(PARAM.bw.toString(), String.valueOf(bw));
    // 码率截取
    String streamid = param.get(PARAM.streamid.toString());
    String sp = "-1";
    int index = streamid.lastIndexOf("_");
    if (-1 != index) {
      sp = streamid.substring(index + 1);
    }
    param.put("sp", sp);
    // 其他字段,为了storm统一
    param.put("type", type);
  }

  @Override
  public void addCacheData(Object param) throws Exception {

    Map<String, String> paramMap = (Map<String, String>) param;
    String pid = paramMap.get("pid");
    String keu_prefix =
        KEY_PREFIX
            + "_"
            + paramMap.get(PARAM.streamid.toString())
            + "_"
            + paramMap.get(PARAM.timestamp.toString());

    // 内网日志不会总到全国
    if (!StringUtils.equals(pid, intranetTag)) {
      String onlineKeyQ = keu_prefix + "_Q_online"; // 全国汇总
      long r2 = XMemcacheUtil.incrementToCache(onlineKeyQ, 1, 1, DEFAULT_EXPIRE_TIME);

      int bw = Math.round(Float.parseFloat(paramMap.get(PARAM.bw.toString())));
      String bwKeyQ = keu_prefix + "_Q_bw";
      long r4 = XMemcacheUtil.incrementToCache(bwKeyQ, bw, bw, DEFAULT_EXPIRE_TIME);
    }
  }

  @Override
  public void addStormData(Object param) throws Exception {

    Map<String, String> paramMap = (Map<String, String>) param;
    String pid = paramMap.get("pid");
    KafkaProcessManager.getServerMsgProcessById(
            (Math.abs((int) sequence.next() % KAFKA_POOL_SIZE_FOR_LIVE_SERVER)))
        .sendObjectKafka(preprosBodyString(paramMap, pid), FLV_TOPIC);
  }

  @Override
  public String getFormat() {

    return "yyyyMMddHHmm";
  }

  private String preprosBodyString(Map<String, String> paramMap, String pid) {

    StringBuilder result = new StringBuilder();
    result.append(paramMap.get(PARAM.timestamp.toString())).append("\t");
    result.append(paramMap.get(PARAM.streamid.toString())).append("\t");
    result.append(paramMap.get("sp")).append("\t");
    String bw = null;
    try {
      bw = String.valueOf(Math.round(Float.parseFloat(paramMap.get(PARAM.bw.toString()))));
    } catch (NumberFormatException e) {
      bw = paramMap.get(PARAM.bw.toString());
    }
    result.append(bw).append("\t");
    result.append(paramMap.get(PARAM.sip.toString())).append("\t");
    result.append(paramMap.get(PARAM.cip.toString())).append("\t");
    result.append(paramMap.get("type")).append("\t");
    result.append(paramMap.get(PARAM.uuid.toString())).append("\t");
    result.append(pid);
    LOG.debug(result.toString());
    return result.toString();
  }
}
 @Scheduled(cron = "${cloudLivePresureCron}")
 public void insertBatch() {
   for (int i = 0; i < Integer.parseInt(Env.get("consumeThreadNum")); i++) {
     consumeExecutor.execute(new ConsumeTask());
   }
 }
예제 #5
0
/**
 * 消息处理服务
 *
 * @author kk
 */
public class MessageServer {

  private static final Logger log = LoggerFactory.getLogger(MessageServer.class);

  private static MessageServer server;
  private static final String MESSAGE_TOPIC =
      StringUtil.isEmpty(Env.get("messageTopic")) ? "messageTopic" : Env.get("messageTopic");
  private static final int NUM_PERSISTENCE_PROCESSORS = 6; //
  private static final int NUM_MESSAGE_PROCESSORS =
      StringUtil.isEmpty(Env.get("numSendProcessors"))
          ? Runtime.getRuntime().availableProcessors()
          : Integer.parseInt(Env.get("numSendProcessors"));
  // private ThreadPoolExecutor threadsPoolMessage;
  private ThreadPoolExecutor threadsPoolpersistence;
  private volatile boolean isRun = false;

  private MessageServer() {

    threadsPoolpersistence =
        new ThreadPoolExecutor(
            NUM_PERSISTENCE_PROCESSORS,
            NUM_PERSISTENCE_PROCESSORS,
            10l,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue(5));
    // threadsPoolMessage = new ThreadPoolExecutor(NUM_MESSAGE_PROCESSORS,
    // NUM_MESSAGE_PROCESSORS, 10l,
    // TimeUnit.SECONDS, new ArrayBlockingQueue(5));
  }

  public static synchronized void startMessageServer() throws Exception {

    if (server != null) return;
    server = new MessageServer();
    try {
      server.start();
    } catch (Exception ex) {
      log.error("MessageServer start error: " + ex.getMessage());
      throw ex;
    }
    log.info("============ MessageServer Is Running. ============");
  }

  public static synchronized void stopMessageServer() throws Exception {

    if (server == null) return;
    try {
      server.stop();
      server = null;
    } catch (Exception ex) {
      log.error("MessageServer stop error: " + ex.getMessage());
      throw ex;
    }
    log.info("============ MessageServer Has stopped. ============");
  }

  private void start() throws Exception {

    isRun = true;
    for (int i = 0; i < NUM_PERSISTENCE_PROCESSORS; i++) {
      threadsPoolpersistence.execute(new PersistenceThread());
    }
    // for (int i = 0; i < NUM_MESSAGE_PROCESSORS; i++) {
    // threadsPoolMessage.execute(new SendThread());
    // }
  }

  private void stop() throws Exception {

    if (null != server) {
      isRun = false;
      threadsPoolpersistence.shutdown();
      // threadsPoolMessage.shutdown();
    }
  }

  class PersistenceThread implements Runnable {

    public PersistenceThread() {}

    public void run() {

      while (isRun) {
        try {
          KafkaProcessManager.getPersistenceProcessById(0)
              .sendObjectKafka(MessageUtil.takePersistence());
        } catch (Throwable t) {
          log.error("PersistenceThread err:", t);
        }
      }
    }
  }

  /*
   * class SendThread implements Runnable{ KafkaUtil k = new
   * KafkaUtil(MESSAGE_TOPIC); public SendThread() { } public void run() { try
   * { while (isRun) { try { k.sendObjectKafka(MessageUtil.takeMessage()); }
   * catch (Throwable t) { log.error("SendThread err:", t); } } } finally {
   * k.close(); } } }
   */
}