项目目录
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--kafka-->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2.配置yaml文件
server:
port: 8081
spring:
application:
name: kafka-springboot
# kafka配置信息
kafka:
bootstrap-servers: 127.0.0.1:9092 # 集群用逗号分隔
producer: # 生产者
retries: 3 # 失败重试次数
batch-size: 16384
buffer-memory: 33554432
acks: 1
# 指定消息key和消息体的编解码方式
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
consumer: # 消费者
group-id: default-group # 消费组
enable-auto-commit: false
# auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
listener:
ack-mode: MANUAL_IMMEDIATE
(此处的127.0.0.1替换成自己的服务器地址)
3.定义一个实体类:
@Data
@Accessors(chain = true)
public class UserLog {
private String username;
private String userid;
private String state;
}
4.定义生产者
@Component
public class UserLogProducer {
@Autowired
private KafkaTemplate kafkaTemplate;
/**
* 发送数据 * @param userid
*/
public void sendLog(String userid) {
UserLog userLog = new UserLog();
userLog.setUsername("jhp").setUserid(userid).setState("0");
System.err.println("发送用户日志数据:" + userLog);
kafkaTemplate.send("austin", JSON.toJSONString(userLog));
}
}
5.定义消费者
@Component
@Slf4j
public class UserLogConsumer {
@KafkaListener(topics = {"austin"}, groupId = "austinGroup1")
public void consumer(ConsumerRecord<?, ?> consumerRecord) { //判断是否为null
Optional<?> kafkaMessage = Optional.ofNullable(consumerRecord.value());
log.info(">>>>>>>>>> record =" + kafkaMessage);
//判断对象是否存在
if (kafkaMessage.isPresent()) {
//得到Optional实例中的值
Object message = kafkaMessage.get();
System.err.println("消费消息:" + message);
}
}
}
6.定义接口
@RestController
public class KafkaTestController {
@Autowired
private UserLogProducer userLogProducer;
/**
* test insert
*/
@GetMapping("/kafka/insert")
public String insert(String userId) {
userLogProducer.sendLog(userId);
return null;
}
}