多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### **3.1.4 使用 Spring Boot** ***** Spring Boot 可以使事情变得更加简单。 以下 Spring Boot 应用程序将三个消息发送到一个主题,接收它们,然后停止。 ~~~ @SpringBootApplication public class Application implements CommandLineRunner { public static Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args).close(); } @Autowired private KafkaTemplate<String, String> template; private final CountDownLatch latch = new CountDownLatch(3); @Override public void run(String... args) throws Exception { this.template.send("myTopic", "foo1"); this.template.send("myTopic", "foo2"); this.template.send("myTopic", "foo3"); latch.await(60, TimeUnit.SECONDS); logger.info("All received"); } @KafkaListener(topics = "myTopic") public void listen(ConsumerRecord<?, ?> cr) throws Exception { logger.info(cr.toString()); latch.countDown(); } } ~~~ <br > Boot 负责大多数配置。当我们使用本地代理时,我们需要的唯一的配置如下: **例子1. application.properties** ``` spring.kafka.consumer.group-id=foo spring.kafka.consumer.auto-offset-reset=earliest ``` 配置解析: * group-id:此消费者所隶属的消费组的唯一标识,即消费组的名称。 * auto-offset-reset:在 Kafka 中每当消费者查找不到所记录的消费位移时,就会根据消费者客户端参数 auto.offset.reset 的配置来决定如何从开始进行消费,这个参数的默认值为 “latest”,表示从分区末尾开始消费消息。如果将 auto.offset.reset 参数配置为“earliest”,那么消费者会从起始处,也就是 0 开始消费。 <br >