springboot整合kafka

添加依赖:

       
            org.springframework.kafka
            spring-kafka
        

添加配置:

spring:
   kafka:
     bootstrap-servers: localhost:9092
     consumer:
       group-id: yunjv-2-ns-biyi

发送消息:

@RestController
public class KafkaTestController {
    @Resource
    private KafkaTemplate kafkaTemplate;

    @PostMapping("/inner/kafka/postMsg")
    public void sendMessage(@RequestBody String msg) {
        System.out.println("【发送方】发送的消息------>>>" + msg);
        kafkaTemplate.send("yj-biyi", msg);//使用send方法发送消息,需要传入topic名称
    }

}

消费消息:

@Component
public class KafkaMessageListener {

    @Autowired
    private InnerCreateNsController innerCreateNsController;

    @KafkaListener(topics = "yj-biyi")
    public void onMessage(ConsumerRecord record) {
        String jsonValue = record.value();
        System.out.println("【接收方】接收的消息------>>>" + jsonValue);
    }
}

你可能感兴趣的:(springboot,spring,boot,kafka,java)