|
@@ -1,16 +1,14 @@
|
|
package cn.com.qmth.examcloud.reports.commons.util;
|
|
package cn.com.qmth.examcloud.reports.commons.util;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
|
|
import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
|
|
import cn.com.qmth.examcloud.reports.commons.bean.BaseReport;
|
|
import cn.com.qmth.examcloud.reports.commons.bean.BaseReport;
|
|
-import cn.com.qmth.examcloud.reports.commons.enums.MqType;
|
|
|
|
|
|
+import cn.com.qmth.examcloud.reports.commons.enums.Tag;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
import cn.com.qmth.examcloud.web.support.IpUtil;
|
|
import cn.com.qmth.examcloud.web.support.IpUtil;
|
|
import cn.com.qmth.examcloud.web.support.ServletUtil;
|
|
import cn.com.qmth.examcloud.web.support.ServletUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.aliyun.openservices.ons.api.*;
|
|
import com.aliyun.openservices.ons.api.*;
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -25,39 +23,43 @@ public class ReportsUtil {
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ReportsUtil.class);
|
|
private static final Logger LOG = LoggerFactory.getLogger(ReportsUtil.class);
|
|
|
|
|
|
- private static final String KEY = "mq-report-data";
|
|
|
|
|
|
+ private static final String GROUP_PRODUCER_PREFIX = "ec_producer_";
|
|
|
|
|
|
- private static Producer producer;
|
|
|
|
|
|
+ private static final String GROUP_CONSUMER_PREFIX = "ec_consumer_";
|
|
|
|
+
|
|
|
|
+ private static final String TOPIC_PREFIX = "ec_topic_";
|
|
|
|
|
|
- private static String mqType = PropertyHolder.getString("$report.mq-type", "rocketmq");
|
|
|
|
|
|
+ private static final String DATA_KEY = "ec_mq_data";
|
|
|
|
+
|
|
|
|
+ private static Producer producer;
|
|
|
|
|
|
- private static Boolean reportEnable = PropertyHolder.getBoolean("$report.enable", false);
|
|
|
|
|
|
+ private static Boolean reportEnable = PropertyHolder.getBoolean("examcloud.rocketmq.enable", false);
|
|
|
|
|
|
static {
|
|
static {
|
|
if (reportEnable) {
|
|
if (reportEnable) {
|
|
- if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
|
|
- Properties properties = new Properties();
|
|
|
|
- // AccessKeyId 阿里云身份验证,在阿里云服务器管理控制台创建。
|
|
|
|
- properties.put(PropertyKeyConst.AccessKey, PropertyHolder.getString("$rocketmq-accesskey"));
|
|
|
|
- // AccessKeySecret 阿里云身份验证,在阿里云服务器管理控制台创建。
|
|
|
|
- properties.put(PropertyKeyConst.SecretKey, PropertyHolder.getString("$rocketmq-secretkey"));
|
|
|
|
- // 设置发送超时时间,单位毫秒。
|
|
|
|
- properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
|
|
|
|
- // 设置 TCP 接入域名,进入控制台的实例详情页面的 TCP 协议客户端接入点区域查看。
|
|
|
|
- properties.put(PropertyKeyConst.NAMESRV_ADDR, PropertyHolder.getString("$rocketmq-namesrv-addr"));
|
|
|
|
-
|
|
|
|
- producer = ONSFactory.createProducer(properties);
|
|
|
|
- // 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可。
|
|
|
|
- producer.start();
|
|
|
|
- } else {
|
|
|
|
- LOG.error("value of property[$report.mq-type] is wrong!");
|
|
|
|
- }
|
|
|
|
|
|
+ Properties properties = new Properties();
|
|
|
|
+ // AccessKeyId 阿里云身份验证,在阿里云服务器管理控制台创建。
|
|
|
|
+ properties.put(PropertyKeyConst.AccessKey, PropertyHolder.getString("examcloud.rocketmq.accesskey"));
|
|
|
|
+ // AccessKeySecret 阿里云身份验证,在阿里云服务器管理控制台创建。
|
|
|
|
+ properties.put(PropertyKeyConst.SecretKey, PropertyHolder.getString("examcloud.rocketmq.secretkey"));
|
|
|
|
+ // 设置发送超时时间,单位毫秒。
|
|
|
|
+ properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
|
|
|
|
+ // 设置 TCP 接入域名,进入控制台的实例详情页面的 TCP 协议客户端接入点区域查看。
|
|
|
|
+ properties.put(PropertyKeyConst.NAMESRV_ADDR, PropertyHolder.getString("examcloud.rocketmq.namesrv_addr"));
|
|
|
|
+
|
|
|
|
+ // 生产组名称
|
|
|
|
+ properties.put(PropertyKeyConst.GROUP_ID, curProducerGroup());
|
|
|
|
+
|
|
|
|
+ producer = ONSFactory.createProducer(properties);
|
|
|
|
+ // 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可。
|
|
|
|
+ producer.start();
|
|
|
|
+ LOG.warn("Rocketmq Producer started...");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private static void sendReportRocket(Boolean onException) {
|
|
private static void sendReportRocket(Boolean onException) {
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
- List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(KEY);
|
|
|
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(DATA_KEY);
|
|
if (CollectionUtils.isNotEmpty(list)) {
|
|
if (CollectionUtils.isNotEmpty(list)) {
|
|
for (BaseReport b : list) {
|
|
for (BaseReport b : list) {
|
|
if (!onException || (onException && b.getReportOnException())) {
|
|
if (!onException || (onException && b.getReportOnException())) {
|
|
@@ -82,7 +84,7 @@ public class ReportsUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- ThreadLocalUtil.set(KEY, null);
|
|
|
|
|
|
+ ThreadLocalUtil.set(DATA_KEY, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -93,10 +95,10 @@ public class ReportsUtil {
|
|
}
|
|
}
|
|
setReportCommonData(report);
|
|
setReportCommonData(report);
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
- List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(KEY);
|
|
|
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(DATA_KEY);
|
|
if (list == null) {
|
|
if (list == null) {
|
|
list = new ArrayList<BaseReport>();
|
|
list = new ArrayList<BaseReport>();
|
|
- ThreadLocalUtil.set(KEY, list);
|
|
|
|
|
|
+ ThreadLocalUtil.set(DATA_KEY, list);
|
|
}
|
|
}
|
|
list.add(report);
|
|
list.add(report);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -105,9 +107,7 @@ public class ReportsUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public static void sendReport(Boolean onException) {
|
|
public static void sendReport(Boolean onException) {
|
|
- if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
|
|
- sendReportRocket(onException);
|
|
|
|
- }
|
|
|
|
|
|
+ sendReportRocket(onException);
|
|
}
|
|
}
|
|
|
|
|
|
private static void setReportCommonData(BaseReport report) {
|
|
private static void setReportCommonData(BaseReport report) {
|
|
@@ -118,16 +118,26 @@ public class ReportsUtil {
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
LOG.debug("获取本机IP出错", e);
|
|
LOG.debug("获取本机IP出错", e);
|
|
}
|
|
}
|
|
|
|
+
|
|
report.setReportHost(ip);
|
|
report.setReportHost(ip);
|
|
report.setRemoteHost(IpUtil.getRemoteIp(ServletUtil.getRequest()));
|
|
report.setRemoteHost(IpUtil.getRemoteIp(ServletUtil.getRequest()));
|
|
report.setReportTime(new Date());
|
|
report.setReportTime(new Date());
|
|
}
|
|
}
|
|
|
|
|
|
- public static String getReportTopic() {
|
|
|
|
- String topic = PropertyHolder.getString("$report.mq-topic");
|
|
|
|
- if (StringUtils.isBlank(topic)) {
|
|
|
|
- throw new StatusException("$report.mq-topic 未配置");
|
|
|
|
- }
|
|
|
|
- return topic;
|
|
|
|
|
|
+ public static String curTopic(Tag tag) {
|
|
|
|
+ return TOPIC_PREFIX + tag.name().toLowerCase() + "_" + curProfile();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static String curConsumerGroup(Tag tag) {
|
|
|
|
+ return GROUP_CONSUMER_PREFIX + tag.name().toLowerCase() + "_" + curProfile();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String curProducerGroup() {
|
|
|
|
+ return GROUP_PRODUCER_PREFIX + "_" + curProfile();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String curProfile() {
|
|
|
|
+ return PropertyHolder.getString("spring.profiles.active", "");
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|