修改小区发送kafka消息
This commit is contained in:
@@ -183,6 +183,17 @@
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
<version>2.8.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.28</version> <!-- 确保版本是最新的 -->
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
||||
+15
-1
@@ -5,6 +5,7 @@ import com.fangjia.bkoff.model.DistrictModel;
|
||||
import com.fangjia.bkoff.model.JsonModel;
|
||||
import com.fangjia.bkoff.util.AddressParser;
|
||||
import com.fangjia.bkoff.util.GeocodeUtils;
|
||||
import com.fangjia.bkoff.util.KafkaUtils;
|
||||
import com.fangjia.core.Constants;
|
||||
import com.fangjia.core.RegionData;
|
||||
import com.fangjia.core.util.*;
|
||||
@@ -89,6 +90,9 @@ public class DistrictController {
|
||||
private DealPriceService dealPriceService;
|
||||
private AverageStatisticsService averageStatisticsService;
|
||||
|
||||
private KafkaUtils kafkaUtils;
|
||||
|
||||
|
||||
private static List<String> distTypes = Arrays.asList("多层", "小高层", "超高层", "高层", "花园洋房", "老洋房", "四合院");
|
||||
private static List<String> villaTypes = Arrays.asList("独栋", "双拼", "联排", "叠加");
|
||||
private static List<String> shopTypes = Arrays.asList("多层", "小高层", "超高层", "高层", "独栋", "双拼", "联排", "叠加");
|
||||
@@ -2284,7 +2288,14 @@ public class DistrictController {
|
||||
data.put("markMap", markMap);
|
||||
data.put("modificationDate", new Date());
|
||||
districtService.updateById(data, district.getId());
|
||||
|
||||
JSONObject massage = new JSONObject();
|
||||
massage.put("city",district.getCity());
|
||||
massage.put("region",district.getRegion());
|
||||
massage.put("name",district.getName());
|
||||
massage.put("estateType2",district.getEstateType2());
|
||||
massage.put("id",district.getId());
|
||||
massage.put("type",2);
|
||||
kafkaUtils.sendMessage("seaweed-data-topic", massage.toString());
|
||||
//创建更新任务,用为更新常州农商行数据
|
||||
createJnTask(district.getId(), null);
|
||||
msg = "保存成功了!!!";
|
||||
@@ -4212,4 +4223,7 @@ public class DistrictController {
|
||||
this.averageStatisticsService = averageStatisticsService;
|
||||
}
|
||||
|
||||
public void setKafkaUtils(KafkaUtils kafkaUtils) {
|
||||
this.kafkaUtils = kafkaUtils;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.fangjia.bkoff.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.kafka.support.SendResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* 通用 Kafka 工具类
|
||||
* 提供异步发送 Kafka 消息的功能
|
||||
*
|
||||
* @author shenjian
|
||||
* @since 2024/06/21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component // 将其标注为 Spring 管理的组件
|
||||
public class KafkaUtils {
|
||||
|
||||
private final KafkaTemplate<Integer, String> kafkaTemplate;
|
||||
private final ExecutorService executorService; // 用于异步执行
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Spring 会自动注入 KafkaTemplate
|
||||
*
|
||||
* @param kafkaTemplate KafkaTemplate 用于发送消息
|
||||
*/
|
||||
public KafkaUtils(KafkaTemplate<Integer, String> kafkaTemplate) {
|
||||
this.kafkaTemplate = kafkaTemplate;
|
||||
// 初始化一个线程池,使用 10 个线程
|
||||
this.executorService = Executors.newFixedThreadPool(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步发送消息到指定的 Kafka 主题
|
||||
*
|
||||
* @param topic Kafka 主题
|
||||
* @param data 消息内容
|
||||
*/
|
||||
public void sendMessage(final String topic, final String data) {
|
||||
executorService.submit(new Callable<Void>() {
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
try {
|
||||
SendResult<Integer, String> sendResult = kafkaTemplate.send(topic, data).get(); // 阻塞直到发送完成
|
||||
log.info("Message sent successfully: {}", sendResult);
|
||||
} catch (Exception ex) {
|
||||
log.error("Kafka send message error = {}, topic = {}", ex.getMessage(), topic);
|
||||
log.error("Data: {}", data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭线程池
|
||||
* 在程序结束时需要调用该方法以释放资源
|
||||
*/
|
||||
public void shutdown() {
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -78,3 +78,29 @@ merchantno=10000469946
|
||||
appKey=SQKK10000469946
|
||||
#\u7EDF\u4E00\u9274\u6743\u7ED1\u5361\u8BF7\u6C42
|
||||
authbindcardreqUri=/rest/v1.0/paperorder/unified/pay
|
||||
|
||||
# Kafka bootstrap servers
|
||||
kafka.bootstrap-servers=192.168.0.89:19092,192.168.0.90:19092,192.168.0.91:19092
|
||||
|
||||
# Producer configuration
|
||||
kafka.producer.retries=0
|
||||
kafka.producer.batch-size=16384
|
||||
kafka.producer.buffer-memory=254288
|
||||
kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
|
||||
kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
|
||||
kafka.producer.auto-topic-creation.auto-create=true
|
||||
kafka.producer.properties.linger.ms=1
|
||||
kafka.producer.properties.session.timeout.ms=15000
|
||||
kafka.producer.properties.sasl.mechanism=PLAIN
|
||||
kafka.producer.properties.security.protocol=SASL_PLAINTEXT
|
||||
kafka.producer.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="Goojia123456";
|
||||
|
||||
# Consumer configuration
|
||||
kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
|
||||
kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
|
||||
kafka.consumer.group-id=log-data-group
|
||||
kafka.consumer.auto-offset-reset=earliest
|
||||
kafka.consumer.properties.session.timeout.ms=15000
|
||||
kafka.consumer.properties.sasl.mechanism=PLAIN
|
||||
kafka.consumer.properties.security.protocol=SASL_PLAINTEXT
|
||||
kafka.consumer.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="Goojia123456";
|
||||
|
||||
Reference in New Issue
Block a user