kakfa 入门
Posted liduanwen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kakfa 入门相关的知识,希望对你有一定的参考价值。
springboot官网
Apache Kafka is supported by providing auto-configuration of the spring-kafka
project. Kafka configuration is controlled by external configuration properties in spring.kafka.*
. For example, you might declare the following section in 。
通过提供spring-kafka项目的自动配置支持Apache ,Kafka配置由spring.kafka中的外部配置属性控制。 例如,您可以声明以下部分:
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=myGroup
Spring’s KafkaTemplate
is auto-configured and you can autowire them directly in your own beans:
Spring的KafkaTemplate是自动配置的,您可以直接在自己的bean中自动连接:
@Component public class MyBean { private final KafkaTemplate kafkaTemplate; @Autowired public MyBean(KafkaTemplate kafkaTemplate) { this.kafkaTemplate = kafkaTemplate; } // ... }
When the Apache Kafka infrastructure is present, any bean can be annotated with@KafkaListener
to create a listener endpoint. If noKafkaListenerContainerFactory
has been defined,
a default one is configured automatically with keys defined inspring.kafka.listener.*
.
当Apache Kafka存在时,任何bean都可以用@KafkaListener注释来创建一个侦听器端点。 如果没有定义KafkaListenerContainerFactory,
使用spring.kafka.listener中定义的键自动配置默认值。
The following component creates a listener endpoint on the someTopic
topic:
以下组件在someTopic主题上创建一个侦听器端点:@Componentpublic class MyBean {
@KafkaListener(topics = "someTopic")
public void processMessage(String content) {
// ...
}
}
The properties supported by auto configuration are shown in Appendix A, Common application properties. Note that these properties
(hyphenated or camelCase) map directly to the Apache Kafka dotted properties for the most part, refer to the Apache Kafka documentation for details.
The first few of these properties apply to both producers and consumers, but can be specified at the producer or consumer level if you wish to use different
values for each. Apache Kafka designates properties with an importance: HIGH, MEDIUM and LOW. Spring Boot auto configuration supports all HIGH importance properties,
some selected MEDIUM and LOW, and any that do not have a default value.Only a subset of the properties supported by Kafka are available via theKafkaProperties
class.
If you wish to configure the producer or consumer with additional properties that are not directly supported, use the following:
自动配置支持的属性如附录A,常用应用程序属性所示。 请注意,这些属性(连字符或camelCase)大部分地直接映射到Apache Kafka虚线属性,有关详细信息,请参阅Apache Kafka文档。这些属性中的前几个适用于生产者和消费者,
但如果您希望使用不同的产品,则可以在生产者或消费者层面进行指定每个值。 Apache Kafka指定具有重要性的属性:HIGH,MEDIUM和LOW。 Spring Boot自动配置支持所有HIGH重要性, 一些选择MEDIUM和LOW,任何没有默认值只有Kafka
支持的属性的一小部分可通过KafkaProperties类获得。 如果您希望使用不直接支持的其他属性来配置生产者或消费者,请使用以下内容:spring.kafka.properties.foo.bar=baz
spring.kafka.consumer.properties.fiz.buz=qux spring,kafka.producer.properties.baz.qux=fiz
启动kafka-server:PS E:\工作软件及资料\软件工作空间\kafka_2.12-0.11.0.1\bin\windows> ./kafka-server-start.bat ..\..\config\server.properties
创建topic : E:\工作软件及资料\软件工作空间\kafka_2.12-0.11.0.1\bin\windows> ./kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic test1
Created topic "test1".
启动生产者控制台窗口:E:\工作软件及资料\软件工作空间\kafka_2.12-0.11.0.1\bin\windows> ./kafka-console-producer.bat --broker-list localhost:9092 --topic test1
启动消费者控制台窗口: E:\工作软件及资料\软件工作空间\kafka_2.12-0.11.0.1\bin\windows> ./kafka-console-consumer.bat --zookeeper localhost:2181 --from-beginning --topic test1
以上是关于kakfa 入门的主要内容,如果未能解决你的问题,请参考以下文章