添加kafka

This commit is contained in:
wanggeng 2021-12-23 19:49:31 +08:00
parent 71bb3c75d5
commit 0eed03283d
5 changed files with 130 additions and 0 deletions

View File

@ -75,6 +75,11 @@
</dependency>
<!-- session share end -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -0,0 +1,57 @@
package cn.com.tenlion.usercenter.kafka;
import cn.com.tenlion.usercenter.service.teammembergrid.ITeamMemberGridService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
/**
* @ClassName: KafKaPublishConsumer
* @Description: kafka消费者
* @Author: wanggeng
* @Date: 2021/12/23 10:06 AM
* @Version: 1.0
*/
@Component
public class KafKaPublishConsumer implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
@Autowired
private ITeamMemberGridService teamMemberGridService;
private MongoTemplate mongoTemplate;
public KafKaPublishConsumer(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
@KafkaListener(topics = {"C0021"})
public void gridMember(ConsumerRecord<?, ?> record) {
JSONObject publishObject = JSON.parseObject(record.value().toString());
System.out.println("收到消息:" + publishObject.toJSONString());
}
@KafkaListener(topics = {"C0022"})
public void mapGrid(ConsumerRecord<?, ?> record) {
JSONObject publishObject = JSON.parseObject(record.value().toString());
System.out.println("收到消息:" + publishObject.toJSONString());
}
@KafkaListener(topics = {"C0023"})
public void mapGridPoint(ConsumerRecord<?, ?> record) {
JSONObject publishObject = JSON.parseObject(record.value().toString());
System.out.println("收到消息:" + publishObject.toJSONString());
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}

View File

@ -0,0 +1,17 @@
package cn.com.tenlion.usercenter.pojo.dtos.kafka;
/**
* @ClassName: SyncTableDTO
* @Description:
* @Author: wanggeng
* @Date: 2021/12/23 12:34 PM
* @Version: 1.0
*/
public class SyncDataDTO<T> {
private String uid;
private String tableNumber;
private String action;
private String
}

View File

@ -0,0 +1,30 @@
package cn.com.tenlion.usercenter.service.grid.impl;
import ink.wgink.interfaces.map.IGridSaveAfterHandler;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
/**
* @ClassName: GridSaveAfterHandlerImpl
* @Description: 网格新增
* @Author: wanggeng
* @Date: 2021/12/23 11:27 AM
* @Version: 1.0
*/
@Service
public class GridSaveAfterHandlerImpl implements IGridSaveAfterHandler {
@Autowired
private KafkaTemplate kafkaTemplate;
@Override
public void handle(String gridId, String gridCode, String gridName, String fillColor, String areaCode, String areaName) {
JSONObject gridJSONObject = new JSONObject();
gridJSONObject.put("uid", gridId);
gridJSONObject.put("action", "save");
}
}

View File

@ -67,6 +67,27 @@ spring:
max-wait: 1ms
max-idle: 8
min-idle: 0
kafka:
bootstrap-servers: 192.168.0.156:9093
producer:
# 写入失败时重试次数。当leader节点失效一个repli节点会替代成为leader节点此时可能出现写入失败
# 当retris为0时produce不会重复。retirs重发此时repli节点完全成为leader节点不会产生消息丢失。
retries: 0
#procedure要求leader在考虑完成请求之前收到的确认数用于控制发送记录在服务端的持久化其值可以为如下
#acks = 0 如果设置为零,则生产者将不会等待来自服务器的任何确认,该记录将立即添加到套接字缓冲区并视为已发送。在这种情况下,无法保证服务器已收到记录,并且重试配置将不会生效(因为客户端通常不会知道任何故障),为每条记录返回的偏移量始终设置为-1。
#acks = 1 这意味着leader会将记录写入其本地日志但无需等待所有副本服务器的完全确认即可做出回应在这种情况下如果leader在确认记录后立即失败但在将数据复制到所有的副本服务器之前则记录将会丢失。
#acks = all 这意味着leader将等待完整的同步副本集以确认记录这保证了只要至少一个同步副本服务器仍然存活记录就不会丢失这是最强有力的保证这相当于acks = -1的设置。
#可以设置的值为all, -1, 0, 1
acks: 1
consumer:
group-id: SmartCity
# smallest和largest才有效如果smallest重新0开始读取如果是largest从logfile的offset读取。一般情况下我们都是设置smallest
auto-offset-reset: earliest
# 设置自动提交offset
enable-auto-commit: true
# 如果'enable.auto.commit'为true则消费者偏移自动提交给Kafka的频率以毫秒为单位默认值为5000。
auto-commit-interval: 100
max-poll-records: 5
mybatis:
config-location: classpath:mybatis/mybatis-config.xml