Springboot中整合kafka

1.阻塞队列

  • BlockingQueue
    • 解决线程通信的问题
    • 阻塞方法:put、take
  • 生产者消费者模式
    • 生产者:产生数据的线程
    • 消费者:使用数据的线程
  • 实现类:
    • ArrayBlockingQueue
    • LinkedBlockingQueue
    • PriorityBlockingQueue、SynchronousQueue、DelayQueue等

在这里插入图片描述

  • 以ArrayBlockingQueue为例写一个案例
public class BlockingQueueTests {
    public static void main(String[] args) {

        BlockingQueue queue = new ArrayBlockingQueue(10);
        new Thread(new Producer(queue)).start();
        new Thread(new Consumer(queue)).start();
        new Thread(new Consumer(queue)).start();
        new Thread(new Consumer(queue)).start();

    }
}
class Producer implements Runnable{

    private BlockingQueue<Integer> queue;

    public Producer (BlockingQueue<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try{
            for(int i = 0; i < 100; i++){
                Thread.sleep(20);
                queue.put(i);
                System.out.println(Thread.currentThread().getName() + "生产:" + queue.size());
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

class Consumer implements Runnable{

    private BlockingQueue<Integer> queue;

    public Consumer (BlockingQueue<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try{
            while (true) {
                Thread.sleep(new Random().nextInt(1000));
                queue.take();
                System.out.println(Thread.currentThread().getName() + "消费:" + queue.size());
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
  • 运行结果
    在这里插入图片描述

2.Kafka入门

  • Kafka简介

    • Kafka是一个分布式的流媒体平台
    • 应用:消息系统、日志收集、用户行为追踪、流式处理
  • Kafka特点

    • 高吞吐量
    • 消息持久化(放到硬盘中)
    • 高可靠性(利用了分布式)
    • 高扩展性(集群方便再加服务器)
  • Kafka术语

    • Broker(服务器)、Zookeeper(管理集群,Kafka自带)
    • Topic(主题,存放消息的位置,理解为文件夹)、Partition(对主题的分区)、Offset(在消息内存放的索引)
    • Leader Replica(副本)、 Follower Replica
  • 下载kafka

    • 启动zookeeper
      E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0>bin\windows\zookeeper-server-start.bat config\zookeeper.properties
    • 启动server
      E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0>bin\windows\kafka-server-start.bat config\server.properties
    • 启动生产者
      E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0>cd bin\windows

E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0\bin\windows>kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0\bin\windows>kafka-topics.bat --list --bootstrap-server localhost:9092
test

E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0\bin\windows>kafka-console-producer.bat --broker-list localhost:9092 --topic test

hello
world
how are you
haha

- 启动消费者

E:>cd E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0\bin\windows

E:\kafka_2.12-2.2.0\kafka_2.12-2.2.0\bin\windows>kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
hello
world
how are you
haha

3.Spring整合Kafka

  • 引入依赖

    • spring-kafka
  • 配置Kafka

    • 配置server、consumer
  • 访问Kafka

    • 生产者
      • kafkaTemplate.send(topic, data);
    • 消费者
      • @KafkaListener(topics = {“test”})
      • public void handleMessage(ConsumerRecord record)
  • 案例:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class KafkaTests {

    @Autowired
    private KafkaProducer kafkaProducer;

    @Test
    public void testKafka(){
        kafkaProducer.sendMessage("test", "你好");
        kafkaProducer.sendMessage("test", "在吗");

        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

@Component
class KafkaProducer{
    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void sendMessage(String topic, String content){
        kafkaTemplate.send(topic, content);
    }
}


@Component
class KafkaConsumer{
    @KafkaListener(topics = {"test"})
    public void handleMessage(ConsumerRecord record){
        System.out.println(record.value());
    }
}
  • 运行结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值