springboot和kafka集成
springboot和kafka集成
啊哈关关 发表于6个月前
springboot和kafka集成
  • 发表于 6个月前
  • 阅读 56
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

1.pom.xml文件

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

2.发送方的配置

package com.test.frame.kafka.configuration;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.converter.StringJsonMessageConverter;

import java.util.HashMap;
import java.util.Map;

/**
 * created by guanguan on 2017/9/6
 **/
@Configuration
@EnableKafka
public class KafkaProducerConfiguration {

    @Value("${kafka.bootstrap_servers}")
    private String bootstrapServers;


    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }


    @Bean
    public ProducerFactory<String,String> producerFactory(){
        return new DefaultKafkaProducerFactory<String,String>(producerConfigs());
    }


    @Bean
    public KafkaTemplate<String,?> kafkaTemplate(){
       
        KafkaTemplate<String, String> template = new KafkaTemplate<>(producerFactory());
        //设置后可以传送实体
        template.setMessageConverter(new StringJsonMessageConverter());
        return template;    }


    @Bean
    public Producer producer(){
        return  new Producer();
    }

}

3.消费者方的配置

package com.test.frame.kafka.configuration;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.support.converter.StringJsonMessageConverter;

import java.util.HashMap;
import java.util.Map;

/**
 * created by guanguan on 2017/9/6
 **/
@Configuration
@EnableKafka
public class KafkaConsumerConfiguration {


    @Value("${kafka.bootstrap_servers}")
    private String bootstrapServers;

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props1 = new HashMap<>();
        props1.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props1.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props1.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props1.put(ConsumerConfig.GROUP_ID_CONFIG, "jd-group");  //统一在一个组内

        return props1;

    }


    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<String, String>(consumerConfigs());
    }


    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setMessageConverter(new StringJsonMessageConverter());
        return factory;
    }

}

4.发送

package com.test.frame.kafka.configuration;

import com.test.frame.kafka.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.support.MessageBuilder;

/**
 * created by guanguan on 2017/9/6
 **/

public class Producer {
    private static final Logger logger = LoggerFactory.getLogger(Producer.class);


    @Value("${kafka.topic}")
    private String topic;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;




    public void sendMessage(User payload) {
        kafkaTemplate
                .send(MessageBuilder.withPayload(payload).setHeader(KafkaHeaders.TOPIC, topic).build());
        logger.info("send message=> "+payload.toString());

    }


}

5.接收

package com.test.frame.kafka.configuration;

import com.test.frame.kafka.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * created by guanguan on 2017/9/6
 **/
@Component
public class Consumer {

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


    @KafkaListener(topics = "${kafka.topic}")
    public void recvMessage(User user) {

        logger.info("recv msg:=> " + user.toString());
    }
}

6.测试:

package com.test.frame.kafka.controller;


import com.test.frame.kafka.configuration.Producer;
import com.test.frame.kafka.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class ApiController {

    @Autowired
    Producer producer;


    @RequestMapping("/")
    public String testkafka(@RequestParam String test) {
        User user = new User();
        user.setName("hh");
        producer.sendMessage(user);

        return "send kafak ok!";
    }

}

表明已经接收成功。

application.yml文件




kafka:
  bootstrap_servers: localhost:9092
  topic: test-topic

 

标签: springboot kafka
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 2
博文 113
码字总数 55845
×
啊哈关关
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: