优化判断语句
parent
aee28d568e
commit
9f17578564
|
@ -1,8 +1,5 @@
|
||||||
package com.couplet.analyze.msg.model;
|
package com.couplet.analyze.msg.model;
|
||||||
|
|
||||||
import com.couplet.analyze.common.event.AnalyzeEventCache;
|
|
||||||
import com.couplet.analyze.common.contents.AnalyzeEventContents;
|
|
||||||
import com.couplet.analyze.common.event.AnalyzeEventCache;
|
|
||||||
import com.couplet.analyze.common.event.AnalyzeEventCache;
|
import com.couplet.analyze.common.event.AnalyzeEventCache;
|
||||||
import com.couplet.analyze.msg.domain.CoupletMsgData;
|
import com.couplet.analyze.msg.domain.CoupletMsgData;
|
||||||
import com.couplet.analyze.msg.service.IncidentService;
|
import com.couplet.analyze.msg.service.IncidentService;
|
||||||
|
@ -12,14 +9,18 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.*;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import static com.couplet.analyze.msg.utils.MsgUtils.hexToString;
|
import static com.couplet.analyze.msg.utils.MsgUtils.hexToString;
|
||||||
import static com.couplet.analyze.msg.utils.MsgUtils.sendMsg;
|
import static com.couplet.analyze.msg.utils.MsgUtils.sendMsg;
|
||||||
|
@ -38,9 +39,38 @@ public class ModelsKafkaMessage {
|
||||||
private static final String TOPIC_NAME = "online";
|
private static final String TOPIC_NAME = "online";
|
||||||
private static final String BOOTSTRAP_SERVERS = "39.103.133.136:9092";
|
private static final String BOOTSTRAP_SERVERS = "39.103.133.136:9092";
|
||||||
|
|
||||||
|
|
||||||
|
//线程池,用于异步处理消息到来时的业务逻辑
|
||||||
|
private ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
|
//kafka消费者实例化为类成员变量
|
||||||
|
KafkaConsumer<String, String> consumer;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AnalyzeEventCache analyzeEventCache;
|
private AnalyzeEventCache analyzeEventCache;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//kafka消费者初始化
|
||||||
|
@PostConstruct
|
||||||
|
public void initKafkaConsumer() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
|
||||||
|
// props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-consumer-group");
|
||||||
|
props.put(ConsumerConfig.GROUP_ID_CONFIG, "ddd");
|
||||||
|
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
|
||||||
|
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
|
||||||
|
|
||||||
|
//消费者
|
||||||
|
consumer = new KafkaConsumer<>(props);
|
||||||
|
//订阅主题
|
||||||
|
consumer.subscribe(Collections.singletonList(TOPIC_NAME));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消费者配置
|
* 消费者配置
|
||||||
*
|
*
|
||||||
|
@ -48,19 +78,14 @@ public class ModelsKafkaMessage {
|
||||||
*/
|
*/
|
||||||
@Scheduled(fixedDelay = 50)
|
@Scheduled(fixedDelay = 50)
|
||||||
public void consumerMessages() {
|
public void consumerMessages() {
|
||||||
Properties props = new Properties();
|
executorService.execute(this::consumer);
|
||||||
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
|
|
||||||
props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-consumer-group");
|
|
||||||
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
|
|
||||||
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
|
|
||||||
|
|
||||||
//创建消费者
|
|
||||||
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void consumer() {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
//订阅主题
|
|
||||||
consumer.subscribe(Collections.singletonList(TOPIC_NAME));
|
|
||||||
//持续消费消息
|
//持续消费消息
|
||||||
while (true) {
|
while (true) {
|
||||||
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
|
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
|
||||||
|
@ -75,7 +100,7 @@ public class ModelsKafkaMessage {
|
||||||
IncidentService incidentService = SpringUtils.getBean(string);
|
IncidentService incidentService = SpringUtils.getBean(string);
|
||||||
incidentService.incident(msgData);
|
incidentService.incident(msgData);
|
||||||
}
|
}
|
||||||
// 发送消息
|
// 发送消息
|
||||||
// rabbitTemplate.convertAndSend("couplet-code-queue",msgData,message -> {
|
// rabbitTemplate.convertAndSend("couplet-code-queue",msgData,message -> {
|
||||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||||
// return message;
|
// return message;
|
||||||
|
@ -90,9 +115,8 @@ public class ModelsKafkaMessage {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
log.error(e.getMessage());
|
||||||
} finally {
|
|
||||||
consumer.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,9 +16,11 @@ spring:
|
||||||
discovery:
|
discovery:
|
||||||
# 服务注册地址
|
# 服务注册地址
|
||||||
server-addr: 121.89.211.230:8848
|
server-addr: 121.89.211.230:8848
|
||||||
|
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
|
||||||
config:
|
config:
|
||||||
# 配置中心地址
|
# 配置中心地址
|
||||||
server-addr: 121.89.211.230:8848
|
server-addr: 121.89.211.230:8848
|
||||||
|
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
|
||||||
# 配置文件格式
|
# 配置文件格式
|
||||||
file-extension: yml
|
file-extension: yml
|
||||||
# 共享配置
|
# 共享配置
|
||||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
||||||
discovery:
|
discovery:
|
||||||
# 服务注册地址
|
# 服务注册地址
|
||||||
server-addr: 121.89.211.230:8848
|
server-addr: 121.89.211.230:8848
|
||||||
|
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
|
||||||
config:
|
config:
|
||||||
# 配置中心地址
|
# 配置中心地址
|
||||||
server-addr: 121.89.211.230:8848
|
server-addr: 121.89.211.230:8848
|
||||||
|
namespace: a439ce3f-2c42-4b4c-9c4d-c8db49933c15
|
||||||
# 配置文件格式
|
# 配置文件格式
|
||||||
file-extension: yml
|
file-extension: yml
|
||||||
# 共享配置
|
# 共享配置
|
||||||
|
|
Loading…
Reference in New Issue