微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

springboot集成kafka

1.前置配置

  • pom

    <properties>
    	<maven.compiler.source>8</maven.compiler.source>
    	<maven.compiler.target>8</maven.compiler.target>
    </properties>
    
    <parent>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
    	<version>2.1.5.RELEASE</version>
    </parent>
    
    <dependencies>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter</artifactId>
    	</dependency>
    	<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>
    
  • application.properties

    spring.kafka.bootstrap-servers=127.0.0.1:9092,127.0.0.1:9093,127.0.0.1:9094
    
    spring.kafka.producer.retries=5
    spring.kafka.producer.acks=all
    spring.kafka.producer.batch-size=16384
    spring.kafka.producer.buffer-memory=33554432
    spring.kafka.producer.transaction-id-prefix=transaction-id-
    spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
    spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
    spring.kafka.producer.properties.enable.idempotence=true
    
    spring.kafka.consumer.group-id=group1
    spring.kafka.consumer.auto-offset-reset=earliest
    spring.kafka.consumer.enable-auto-commit=true
    spring.kafka.consumer.auto-commit-interval=100
    spring.kafka.consumer.properties.isolation.level=read_committed
    spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
    spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
    
  • 日志

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%p %d{yyyy-MM-dd HH:mm:ss} - %m%n</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <!-- 控制台输出日志级别 -->
        <root level="ERROR">
            <appender-ref ref="STDOUT" />
        </root>
    
        <logger name="org.springframework.kafka" level="INFO"  additivity="false">
            <appender-ref ref="STDOUT" />
        </logger>
    
        <!--事务控制-->
        <logger name="org.springframework.kafka.transaction" level="debug"  additivity="false">
            <appender-ref ref="STDOUT" />
        </logger>
    
    </configuration>
    

2.代码

  • 启动类
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.KafkaListeners;
import org.springframework.messaging.handler.annotation.SendTo;
import java.io.IOException;

/**
 * 程序启动入口
 */
@SpringBootApplication
@EnableKafka
public class KafkaSpringBootApplication {
    public static void main(String[] args) throws IOException {
        SpringApplication.run(KafkaSpringBootApplication.class, args);
        system.in.read();
    }

    /**
     * 消费者topic04,同时将消息发送给topic05
     * @param cr
     * @return
     */
    @KafkaListeners(value = {@KafkaListener(topics = {"topic04"})})
    @SendTo(value = {"topic05"})
    public String listenner(ConsumerRecord<?, ?> cr) {
        System.out.println("消费到数据:"+cr.value());
        return cr.value() + "demo";
    }
}
  • service类

    import org.apache.kafka.clients.producer.ProducerRecord;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.kafka.core.KafkaTemplate;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    /**
     * 通过KafkaTemplate发送消息
     * Transactional可以通过这个配置使用kafka的相关事务
     */
    @Transactional
    @Service
    public class OrderService {
    
        @Autowired
        private KafkaTemplate kafkaTemplate;
    
        public void saveOrder(String id, Object message) {
            kafkaTemplate.send(new ProducerRecord("topic04", id, message));
        }
    }
    
  • 测试类

    import com.baizhi.KafkaSpringBootApplication;
    import com.baizhi.OrderService;
    import org.apache.kafka.clients.producer.ProducerRecord;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBoottest;
    import org.springframework.kafka.core.KafkaOperations;
    import org.springframework.kafka.core.KafkaTemplate;
    import org.springframework.test.context.junit4.springrunner;
    
    /**
     * 通过单元测试类来发送消息
     */
    @SpringBoottest(classes = {KafkaSpringBootApplication.class})
    @RunWith(springrunner.class)
    public class KafkaTempolateTests {
    
        @Autowired
        private KafkaTemplate kafkaTemplate;
    
        @Autowired
        private OrderService orderService;
    
        @Test
        public void testOrderService() {
            orderService.saveOrder("001", "诗和远方");
        }
    
        @Test
        public void testKafkaTemplate() {
            kafkaTemplate.executeInTransaction(new KafkaOperations.OperationsCallback() {
                @Override
                public Object doInoperations(KafkaOperations kafkaOperations) {
                    return kafkaOperations.send(new ProducerRecord("topic04", "002", "this is a demo"));
                }
            });
        }
    }
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐