标签:lin 消费 turn create npe package http ati context
1、实战
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.12</artifactId> <version>2.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-stream-kafka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-kafka</artifactId> <version>3.0.0.RELEASE</version> </dependency>
package com.an.kafka.config; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.serialization.StringSerializer; 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 java.util.HashMap; import java.util.Map; /** * @description: 生产者配置 * @author: anpeiyong * @date: Created in 2019/12/12 14:44 * @since: */ @Configuration @EnableKafka public class KafkaProducerConfig { @Bean public ProducerFactory producerFactory() { return new DefaultKafkaProducerFactory(producerConfigs()); } @Bean public Map producerConfigs() { Map props =new HashMap(); //Kafka生产者配置 props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.8.172:9092"); props.put(ProducerConfig.RETRIES_CONFIG,0); props.put(ProducerConfig.BATCH_SIZE_CONFIG,16384); props.put(ProducerConfig.LINGER_MS_CONFIG,1); props.put(ProducerConfig.BUFFER_MEMORY_CONFIG,33554432); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); return props; } @Bean public KafkaTemplate kafkaTemplate() { return new KafkaTemplate(producerFactory()); } }
package com.an.kafka.producer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @description: Kafka生产者 * @author: anpeiyong * @date: Created in 2019/12/12 14:54 * @since: */ @RestController @RequestMapping("kafkaProducer") public class KafkaProducer { @Autowired private KafkaTemplate kafkaTemplate; @GetMapping("send/{msg}") public String send(@PathVariable("msg") String msg){ kafkaTemplate.send("an","antest", msg);//topic_name是指发送信息的目标主题 return "success"; } }
package com.an.kafka.config; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.common.serialization.StringDeserializer; import org.springframework.boot.autoconfigure.kafka.KafkaProperties; 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 java.util.HashMap; import java.util.Map; /** * @description: 消费者配置 * @author: anpeiyong * @date: Created in 2019/12/12 15:06 * @since: */ @Configuration @EnableKafka public class KafkaConsumerConfig { @Bean public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() { ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>(); factory.setConsumerFactory(consumerFactory()); return factory; } @Bean public ConsumerFactory consumerFactory() { return new DefaultKafkaConsumerFactory<>(consumerConfigs()); } @Bean public Map consumerConfigs() { Map props =new HashMap<>(); //消费者参数设置 props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.8.172:9092"); props.put(ConsumerConfig.GROUP_ID_CONFIG,"test-consumer-group"); props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true); props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000"); props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,"15000"); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); return props; } @Bean public KafkaProperties.Listener listener() { return new KafkaProperties.Listener(); } }
package com.an.kafka.consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; /** * @description: kafka消费者 * @author: anpeiyong * @date: Created in 2019/12/12 15:10 * @since: */ @Component public class KafkaConsumer { @KafkaListener(topics ="an") public void listen (ConsumerRecord record){ System.out.printf("topic = %s, offset = %d, value = %s \n", record.topic(), record.offset(), record.value()); } }
结果: topic = an, offset = 0, value = hello
标签:lin 消费 turn create npe package http ati context
原文地址:https://www.cnblogs.com/anpeiyong/p/12033176.html