1. 程式人生 > >springboot簡易使用kafka

springboot簡易使用kafka

寫在前面:本文將使用的是kafka單節點docker環境
讀者如果想在Windows安裝執行Kafka環境,請參考 https://www.cnblogs.com/flower1990/p/7466882.html

程式碼地址: https://github.com/Blankwhiter/kafka

第一步 搭建kafka環境

參考教程 https://blog.csdn.net/belonghuang157405/article/details/82149257

第二步 springboot整合kafka

1.引入kafka包
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <
artifactId
>
kafka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>kafka</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</
groupId
>
<artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入kafka--> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.編寫kafka配置
application.yml

spring:
  kafka:
    bootstrap-servers: 192.168.9.219:9092
    consumer:
      group-id: consumer
      enable-auto-commit: true
      auto-commit-interval: 1000s

3.編寫訊息消費者
kafkaConsumer.java

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaConsumer {

    /**
     * 接收kafka訊息 可接收多個topic訊息
     * @param message
     */
    @KafkaListener(topics = {"test1"})
    public void receiveMessage(String message){
        System.out.println("--------開始接收訊息--------");
        System.out.println("        接收訊息 :"+message);
        System.out.println("--------結束接收訊息--------");
    }
}

4.編寫訊息消費者
MessageController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    /**
     * 配置kafka系統元件
     */
    @Autowired
    KafkaAdmin kafkaAdmin;

    /**
     * kafkaTemplate 傳送訊息Template
     */
    @Autowired
    KafkaTemplate kafkaTemplate;

    /**
     * 傳送訊息
     */
    @RequestMapping("/send")
    public String  sendMessage(){
        kafkaTemplate.send("test1","test - msg");
        return "success";
    }

}

5.啟動專案,並再瀏覽器訪問 http://localhost:8080/send
即可在控制檯看到打印出的訊息

------------開始接收訊息----------
            接收訊息 :test - msg
------------結束接收訊息----------