前置工作
- 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