【Spring Cloud Alibaba】3.新版SpringCloudAlibaba整合Kafka

全弘深
2023-12-01

一、背景

废话不多说,直接上代码

二、pom.xml

<properties>
    <jdk.version>1.8</jdk.version>
    <java.version>1.8</java.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>

    <spring.boot.version>2.5.5</spring.boot.version>
    <spring.cloud.version>2020.0.4</spring.cloud.version>
    <spring.cloud.bootstrap.version>3.0.4</spring.cloud.bootstrap.version>
    <alibaba.cloud.version>2021.1</alibaba.cloud.version>
    <alibaba.fastjson.version>1.2.73</alibaba.fastjson.version>
    <stream.cloud.version>3.1.4</stream.cloud.version>
    <kafka.cloud.version>3.1.4</kafka.cloud.version>
</properties>

<!-- 依赖版本号管理 -->
<dependencyManagement>
    <dependencies>
        <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/2.3.3.RELEASE-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies/Hoxton.SR8-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring.cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-alibaba-dependencies/2021.1-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${alibaba.cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-stream-dependencies -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-dependencies</artifactId>
            <version>${stream.cloud.version}</version>
            <type>pom</type>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-stream-kafka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
            <version>${kafka.cloud.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>


<dependencies>
    <!--kafka-->
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
    </dependency>
</dependencies>

三、配置文件

spring.kafka.bootstrap-servers = 127.0.0.1:9092,127.0.0.2:9092
spring.kafka.producer.acks = 1
spring.kafka.producer.retries = 3
spring.kafka.producer.batch-size = 16384
spring.kafka.producer.key-serializer = org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer = org.apache.kafka.common.serialization.StringSerializer
spring.kafka.consumer.enable-auto-commit = true
spring.kafka.consumer.key-deserializer = org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer = org.apache.kafka.common.serialization.StringDeserializer
 类似资料: