|
|
package com.yoho.ops.kafka;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
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;
|
|
|
|
|
|
@Configuration
|
|
|
@EnableKafka
|
|
|
public class ReceiverConfig {
|
|
|
|
|
|
@Value("${kafka.servers.bootstrap}")
|
|
|
private String bootstrapServers;
|
|
|
|
|
|
@Bean
|
|
|
public Map<String, Object> consumerConfigs() {
|
|
|
Map<String, Object> props = new HashMap<>();
|
|
|
// list of host:port pairs used for establishing the initial connections
|
|
|
// to the Kakfa cluster
|
|
|
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
|
|
|
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
|
|
|
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
|
|
|
// consumer groups allow a pool of processes to divide the work of
|
|
|
// consuming and processing records
|
|
|
props.put(ConsumerConfig.GROUP_ID_CONFIG, "helloworld");
|
|
|
|
|
|
return props;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public ConsumerFactory<String, String> consumerFactory() {
|
|
|
return new DefaultKafkaConsumerFactory<>(consumerConfigs());
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
|
|
|
ConcurrentKafkaListenerContainerFactory<String, String> factory =
|
|
|
new ConcurrentKafkaListenerContainerFactory<>();
|
|
|
factory.setConsumerFactory(consumerFactory());
|
|
|
|
|
|
return factory;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public Receiver receiver() {
|
|
|
return new Receiver();
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|