Kafka设置
启动zk
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
启动kafka
bin/kafka-server-start.sh config/server.properties
依赖
compile 'org.springframework.kafka:spring-kafka'
application.properties
#============== kafka ===================
# 指定kafka 代理地址,可以多个
# suppress inspection "UnusedProperty"
spring.kafka.bootstrap-servers=YourServerHostName/IP:9092
#=============== provider =======================
spring.kafka.producer.retries=0
# 每次批量发送消息的数量
spring.kafka.producer.batch-size=16384
spring.kafka.producer.buffer-memory=33554432
# 指定消息key和消息体的编解码方式
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
#=============== consumer =======================
# 指定默认消费者group id
spring.kafka.consumer.group-id=test-consumer-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.enable-auto-commit=true
spring.kafka.consumer.auto-commit-interval=100
# 指定消息key和消息体的编解码方式
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
Kafka producer
@Component
@Slf4j
public class MessageSender {
@Autowired
private KafkaTemplate<String,String> kafkaTemplate;
private Gson gson = new GsonBuilder().create();
public void send() {
Message m = new Message();
m.setId(System.currentTimeMillis());
m.setName(UUID.randomUUID().toString());
m.setDate(new Date());
log.info("+++++++++++++++++++++ message = {}", gson.toJson(m));
kafkaTemplate.send("myTopic",gson.toJson(m));
}
}
Kafka consumer
@Component
@Slf4j
public class MessageReceiver {
@KafkaListener(topics = {"myTopic"})
public void listen(ConsumerRecord<?,?> record) {
Optional<?> message = Optional.ofNullable(record.value());
if (message.isPresent()) {
Object m = message.get();
log.info("------------------record"+ record);
log.info("------------------message"+ m);
}
}
}
main
@SpringBootApplication
public class KafkaApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(KafkaApplication.class, args);
MessageSender sender = context.getBean(MessageSender.class);
for (int i = 0; i < 3; i++) {
sender.send();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Show topic
bin/kafka-topics.sh --list --zookeeper localhost:2181
来源:oschina
链接:https://my.oschina.net/u/4316069/blog/4060142