码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot整合Kfka

时间:2019-02-28 14:58:28      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:zook   static   art   wired   1.5   comm   over   manage   autowired   

1.首先在pom文件添加依赖

The managed version is 1.1.7.RELEASE The artifact is managed in org.springframework.boot:spring-boot-dependencies:1.5.9.RELEASE

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

2.yml文件内容如下:

spring:
  datasource:
    url: jdbc:postgresql://127.0.0.1:5432/dev
    username: dev
    password: dev
  dubbo:
    registry:
      address: zookeeper://127.0.0.1:2181
  kafka:
    bootstrap-servers: 127.0.0.1:9092
    consumer:
      group-id: grp1
                   

3.Consumer java类

@Component
public class JobTrackingConsumer implements BaseConsumer{

    private final static Logger logger = LoggerFactory.getLogger(JobTrackingConsumer.class);

    @Autowired
    private JobBasicCache cache;
    
    @KafkaListener(topics = {CommonConstant.FTP_CATEGPRY_ID, CommonConstant.HTTP_CATEGPRY_ID})
    @Override
    public void consume(ConsumerRecord<?, ?> consumer) {
        Optional<String> kafkaMessage = (Optional<String>) Optional.ofNullable(consumer.value());
        if (kafkaMessage.isPresent()) {
            logger.info("topic:{}, msg:{}", consumer.topic(), kafkaMessage.get());
            cache.push(consumer.topic().toString(), kafkaMessage.get());
        }
    }

}

 

Springboot整合Kfka

标签:zook   static   art   wired   1.5   comm   over   manage   autowired   

原文地址:https://www.cnblogs.com/changfangxing/p/10450055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!