### **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 >