Updated on 2026-06-29 GMT+08:00

Example Code for Connecting Kafka to Spring Boot

This section applies to MRS 3.3.0 and later versions.

Function

Spring Boot is used to produce and consume Kafka clusters.

Sample Code

Use Spring Boot to implement Kafka production and consumption.

@RestController
public class MessageController {
    private final static Logger LOG = LoggerFactory.getLogger(MessageController.class);
    @Autowired
    private KafkaProperties kafkaProperties;
    @GetMapping("/produce")
    public String produce() {
        Producer producerThread = new Producer();
        producerThread.init(this.kafkaProperties);
        producerThread.start();
        String message = "Start to produce messages";
        LOG.info(message);
        return message;
    }
    @GetMapping("/consume")
    public String consume() {
        Consumer consumerThread = new Consumer();
        consumerThread.init(this.kafkaProperties);
        consumerThread.start();
        LOG.info("Start to consume messages");
        // Wait for 180s and close the consumer. Modification can be made during actual execution.
        try {
            Thread.sleep(consumerThread.getThreadAliveTime());
        } catch (InterruptedException e) {
            LOG.info("Occurred InterruptedException: ", e);
        } finally {
            consumerThread.close();
        }
        return String.format("Finished consume messages");
    }
}

Produce indicates the message production interface, consume indicates the message consumption interface, and KafkaProperties indicates the client parameter. The parameters need to be modified based on the actual service.

The KafkaProperties parameter in the sample code can be configured in springboot > kafka-examples > src > main > resources > application.properties (In MRS 3.6.0 and later versions, you need to configure this parameter in springboot > kafka-examples > kafka-springboot-examples > src > main > resources > application.properties.) or manually compiled in the application.properties file in the sample running environment. If no default value is specified, the configuration is mandatory.
  • bootstrap.servers: Broker address list of the Kafka cluster. The format is IP address: Port, IP address: Port, IP address: Port. In the IPv6 environment, add [] to the IP address, for example, [1:2:3:4:5:6:7:8]:21007.
  • security.protocol: authentication protocol used by the Kafka client. The default value is PLAINTEXT.
  • topic: name of the production and consumption topic. The default value is example-metric1.
  • isAsync: whether to use asynchronous production. The default value is false.
  • consumer.alive.time: lifetime of the consumer thread. The default value is 180000, in milliseconds.
  • server.port: port for accessing the Spring Boot server. The default value is 8080, which can be customized.
  • server.address: IP address bound to the Spring Boot server during the startup. The default value is 0.0.0.0, which needs to be changed to the IP address of the node where Spring Boot is deployed.
  • is.security.mode: whether the client connects to the cluster in security mode. The default value is false.