SpringBoot -- Kafka(二) Demo

前置工作

  • Kafka 、zookeeper環境已經完成
  • JDK完成安裝(kafka環境依賴jvm)
  • 瞭解kafka、zookeeper各種的作用

Demo

  • 依然使用現有的feignserver
  • 有說Springboot 1.5+ 已經支持spring-integration 無縫對接
  • 本文依然使用的 1.4+,所以還是採用config方式

引入kafka依賴

build.gradle

compile ('org.springframework.integration:spring-integration-core:4.3.6.RELEASE')
compile ('org.springframework.kafka:spring-kafka:1.1.0.RELEASE')

配置參數:broker、zk、group等
application.yml

bootcwenao:
  kafka:
    binder:
      brokers: localhost:9092
      zk-nodes: localhost:2181
    group: cwenao-group

創建KafkaProducersConfig並使用@Configuration、@EnableKafka

/**
 * @author cwenao
 * @version $Id KafkaConfig.java, v 0.1 2017-01-20 9:51 cwenao Exp $$
 */

@Configuration
@EnableKafka
public class KafkaProducersConfig {
    @Value("${bootcwenao.kafka.binder.brokers}")
    private String brokers;

    @Bean("kafkaTemplate")
    public KafkaTemplate<String, String> kafkaTemplate() {
        KafkaTemplate<String, String> kafkaTemplate = new KafkaTemplate<String, String>(producerFactory());
        return kafkaTemplate;
    }

    public ProducerFactory<String, String> producerFactory() {

        // set the producer properties
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);
        properties.put(ProducerConfig.RECEIVE_CONFIG, 0);
        properties.put(ProducerConfig.BATCH_SIZE_CONFIG, 4096);
        properties.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 40960);
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        return new DefaultKafkaProducerFactory<String, String>(properties);
    }
}

創建Listener 處理消息,使用@KafkaListener,用戶監聽topic

/**
 * @author cwenao
 * @version $Id KafkaListeners.java, v 0.1 2017-01-21 21:31 cwenao Exp $$
 */
public class KafkaListeners {

    @KafkaListener(topics = {"bootcwenaoTopic"})
    public void testListener(ConsumerRecord<?, ?> record) {

        Optional<?> messages = Optional.ofNullable(record.value());

        if (messages.isPresent()) {
            Object msg = messages.get();
            System.out.println("  this is the testTopic send message: " + msg);
        }
    }
}

創建KafkaConsumerConfig並使用@Configuration、@EnableKafka

/**
 * @author cwenao
 * @version $Id KafkaConsumerConfig.java, v 0.1 2017-01-21 21:11 cwenao Exp $$
 */
@Configuration
@EnableKafka
public class KafkaConsumerConfig {

    @Value("${bootcwenao.kafka.binder.brokers}")
    private String brokers;

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

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {

        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<String, String>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(4);
        factory.getContainerProperties().setPollTimeout(4000);

        return factory;
    }

    @Bean
    public KafkaListeners kafkaListeners() {
        return new KafkaListeners();
    }

    public ConsumerFactory<String, String> consumerFactory() {

        Map<String, Object> properties = new HashMap<String, Object>();

        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
        properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, group);
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");

        return new DefaultKafkaConsumerFactory<String, String>(properties);
    }
}

測試

創建生產者發送信息方法testKafka

/**
 * @author cwenao
 * @version $Id FeignController.java, v 0.1 2017-01-15 13:50 cwenao Exp $$
 */
@Controller
public class FeignController {

    @Autowired
    KafkaTemplate kafkaTemplate;

    @RequestMapping("/testKafka")
    @ResponseBody

    public void testkafka(String message) {
        kafkaTemplate.send("bootcwenaoTopic", "bootcwnao", message);
    }
}

依次啓動 discovery、configserver、 apigateway、feignserver

瀏覽器中輸入 http://localhost:10002/servers/testKafka?message=aaaaaaaaaaabbbbb

可以看到KafkaListeners中輸出 this is the testTopic send message: aaaaaaaaaaabbbbb

可能的錯誤


  • kafka版本不對,現在這種方式只能支持0.10.x.x
  • kafka配置沒有對外開放host、port

  • advertised.host.name、advertised.port
  • broker-list配置的不對

代碼

代碼請移步 Github參考地址

如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start
公衆號_k171

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章