Spring注解方式集成Kafka(spring-kafka的使用)

  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3.   
  4. import org.apache.kafka.clients.producer.ProducerConfig;  
  5. import org.apache.kafka.common.serialization.StringSerializer;  
  6. import org.springframework.context.annotation.Bean;  
  7. import org.springframework.context.annotation.Configuration;  
  8. import org.springframework.kafka.annotation.EnableKafka;  
  9. import org.springframework.kafka.core.DefaultKafkaProducerFactory;  
  10. import org.springframework.kafka.core.KafkaTemplate;  
  11. import org.springframework.kafka.core.ProducerFactory;  
  12.   
  13. import cn.disruptive.bigDataPlatform.agentServer.config.ReadConfigation;  
  14.   
  15. /** 
  16.  *  
  17.  * Description:Kafka生产者 
  18.  * Date:        2017年7月11日 
  19.  *  
  20.  * 依赖: 
  21.         <dependency> 
  22.             <groupId>org.springframework.kafka</groupId> 
  23.             <artifactId>spring-kafka</artifactId> 
  24.             <version>1.0.5.RELEASE</version> 
  25.         </dependency> 
  26.  *  
  27.  * 使用案例: 
  28.     @Resource 
  29.     private KafkaTemplate kafkaTemplate; 
  30.      
  31.     调用方法发送数据: 
  32.     kafkaTemplate.send(topic, msg); 
  33.  *  
  34.  */  
  35. @Configuration  
  36. @EnableKafka  
  37. public class KafkaProducer {  
  38.   
  39.     /** 
  40.      *  
  41.      * Description:获取配置 
  42.      * Date:        2017年7月11日 
  43.      * @author      shaqf 
  44.      */  
  45.     public Map<String, Object> producerConfigs() {  
  46.         Map<String, Object> props = new HashMap<>();  
  47.         // kafka.metadata.broker.list=10.16.0.214:9092,10.16.0.215:9092,10.16.0.216:9092  
  48.         props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, ReadConfigation.getConfigItem("kafka.metadata.broker.list"));  
  49.         props.put(ProducerConfig.RETRIES_CONFIG, 0);  
  50.         props.put(ProducerConfig.BATCH_SIZE_CONFIG, 4096);  
  51.         props.put(ProducerConfig.LINGER_MS_CONFIG, 1);  
  52.         props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 40960);  
  53.         props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);  
  54.         props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);  
  55.         return props;  
  56.     }  
  57.   
  58.     /** 获取工厂 */  
  59.     public ProducerFactory<String, String> producerFactory() {  
  60.         return new DefaultKafkaProducerFactory<>(producerConfigs());  
  61.     }  
  62.   
  63.     /** 注册实例 */  
  64.     @Bean  
  65.     public KafkaTemplate<String, String> kafkaTemplate() {  
  66.         return new KafkaTemplate<>(producerFactory());  
  67.     }  
  68. }</pre><br>  
  69. <br>  
  70. <pre></pre>  
  71. <p></p>  
  72. <pre></pre>  
  73. <pre code_snippet_id="2478280" snippet_file_name="blog_20170711_2_9410437" name="code" class="java"></pre><pre code_snippet_id="2478280" snippet_file_name="blog_20170711_2_9410437" name="code" class="java">import java.util.HashMap;  
  74. import java.util.Map;  
  75.   
  76. import org.apache.kafka.clients.consumer.ConsumerConfig;  
  77. import org.apache.kafka.common.serialization.StringDeserializer;  
  78. import org.springframework.context.annotation.Bean;  
  79. import org.springframework.context.annotation.Configuration;  
  80. import org.springframework.kafka.annotation.EnableKafka;  
  81. import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;  
  82. import org.springframework.kafka.config.KafkaListenerContainerFactory;  
  83. import org.springframework.kafka.core.ConsumerFactory;  
  84. import org.springframework.kafka.core.DefaultKafkaConsumerFactory;  
  85. import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;  
  86.   
  87. import cn.disruptive.bigDataPlatform.agentServer.config.ReadConfigation;  
  88.   
  89.   
  90. /** 
  91.  *  
  92.  * Description:Kafka消费者 
  93.  * Date:        2017年7月11日 
  94.  *  
  95.  * 依赖: 
  96.         <dependency> 
  97.             <groupId>org.springframework.kafka</groupId> 
  98.             <artifactId>spring-kafka</artifactId> 
  99.             <version>1.0.5.RELEASE</version> 
  100.         </dependency> 
  101.  *  
  102.  * 使用案例: 
  103.     @KafkaListener(topics = { "taskCmd" }) 
  104.     public void taskCmd(ConsumerRecord<?, ?> record) { 
  105.         Object message = record.value(); 
  106.         logger.info("收到管理平台命令:" + message); 
  107.     } 
  108.  *  
  109.  */  
  110.   
  111. @Configuration  
  112. @EnableKafka  
  113. public class KafkaConsumer {  
  114.   
  115.     /** 
  116.      *  
  117.      * Description:获取配置 
  118.      * Date:        2017年7月11日 
  119.      * @author      shaqf 
  120.      */  
  121.     public Map<String, Object> consumerConfigs() {  
  122.         Map<String, Object> props = new HashMap<>();  
  123.         // kafka.metadata.broker.list=10.16.0.214:9092,10.16.0.215:9092,10.16.0.216:9092  
  124.         props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, ReadConfigation.getConfigItem("kafka.metadata.broker.list"));  
  125.         props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);  
  126.         props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");  
  127.         props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");  
  128.         props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);  
  129.         props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);  
  130.         props.put(ConsumerConfig.GROUP_ID_CONFIG, "Agent-Server-1.0.2");  
  131.         props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");  
  132.         return props;  
  133.     }  
  134.   
  135.     /** 获取工厂 */  
  136.     public ConsumerFactory<String, String> consumerFactory() {  
  137.         return new DefaultKafkaConsumerFactory<>(consumerConfigs());  
  138.     }  
  139.   
  140.     /** 获取实例 */  
  141.     @Bean  
  142.     public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {  
  143.         ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();  
  144.         factory.setConsumerFactory(consumerFactory());  
  145.         factory.setConcurrency(3);  
  146.         factory.getContainerProperties().setPollTimeout(3000);  
  147.         return factory;  
  148.     }  
  149. }
  150. 还有一种方式,直接在配置文件里面添加Kafka地址即可,以上代码都可以省略  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值