日志发送改造
This commit is contained in:
@@ -234,5 +234,10 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>3.14.2</version>
|
<version>3.14.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.kafka</groupId>
|
||||||
|
<artifactId>kafka_2.13</artifactId>
|
||||||
|
<version>3.4.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
+7
-1
@@ -4,6 +4,7 @@ import com.fangjia.dal.QueryParam;
|
|||||||
import com.fangjia.dal.SortParam;
|
import com.fangjia.dal.SortParam;
|
||||||
import com.fangjia.dal.mongo.open.dao.OpenUriLogDao;
|
import com.fangjia.dal.mongo.open.dao.OpenUriLogDao;
|
||||||
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
||||||
|
import com.fangjia.service.util.kafka.KafkaProducerUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -13,9 +14,12 @@ import java.util.List;
|
|||||||
public class OpenUriLogServiceImpl implements OpenUriLogService {
|
public class OpenUriLogServiceImpl implements OpenUriLogService {
|
||||||
|
|
||||||
private OpenUriLogDao openUriLogDao;
|
private OpenUriLogDao openUriLogDao;
|
||||||
|
private KafkaProducerUtils kafkaProducerUtils;
|
||||||
|
|
||||||
public void save(OpenUriLog openUriLog) {
|
public void save(OpenUriLog openUriLog) {
|
||||||
this.openUriLogDao.save(openUriLog);
|
this.openUriLogDao.save(openUriLog);
|
||||||
|
// ¸ÄÔìΪÏòKafkaÖз¢ËÍÏûÏ¢
|
||||||
|
kafkaProducerUtils.sendLog(openUriLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -95,5 +99,7 @@ public class OpenUriLogServiceImpl implements OpenUriLogService {
|
|||||||
this.openUriLogDao = openUriLogDao;
|
this.openUriLogDao = openUriLogDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setKafkaProducerUtils(KafkaProducerUtils kafkaProducerUtils) {
|
||||||
|
this.kafkaProducerUtils = kafkaProducerUtils;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
package com.fangjia.service.util.kafka;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
||||||
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
|
import org.apache.kafka.common.serialization.StringSerializer;
|
||||||
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Service
|
||||||
|
public class KafkaProducerUtils implements DisposableBean {
|
||||||
|
public final KafkaProducer<String, String> producer;
|
||||||
|
|
||||||
|
public KafkaProducerUtils() {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.0.100:30092");
|
||||||
|
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
|
||||||
|
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
|
||||||
|
|
||||||
|
this.producer = new KafkaProducer<String, String>(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendLog(OpenUriLog openUriLog) {
|
||||||
|
try {
|
||||||
|
String msg = JSONObject.toJSONString(openUriLog);
|
||||||
|
ProducerRecord<String, String> record = new ProducerRecord<String, String>("log-data-topic", msg);
|
||||||
|
this.producer.send(record);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() throws Exception {
|
||||||
|
producer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user