Spring Cloud 集成kafka

pom 依赖

 
            org.springframework.kafka
            spring-kafka
            2.2.7.RELEASE
        

yml文件配置

kafka:
    bootstrap-servers: 172.31.209.90:9092 #(kafka生产集群ip+port端口)
    producer:
      buffer-memory: 40960
      retries: 0
      batch-size: 4096
      properties:
        linger.ms: 1
    consumer:
      enable-auto-commit: true #(是否自动提交)
      auto-commit-interval: 100ms
      auto-offset-reset: latest #(实时生产,实时消费,不会从头开始消费)
      group-id: test #(消费组)
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer

监听器

Slf4j
@Component
public class EhrAgreementListener {
   @KafkaListener(topics = {"annul"}, groupId = "myContainer2")
    public void annul1(ConsumerRecord record) {
        log.info("groupId = myContainer2, message = " + record.toString());
    }

    @KafkaListener(topics = {"signed"}, groupId = "myContainer2")
    public void signed2(ConsumerRecord record) {
        log.info("groupId = myContainer2, message = " + record.toString());
    }

}
新建个controller 写个方法,项目启动直接成功
@Autowired
private KafkaTemplate kafkaTemplate;

public void sendTest(String msg) {
    kafkaTemplate.send("signed", msg);
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(kafka)