阻塞队列
. BlockingQueue(是一个接口)
-解决线程通信的问题。
-阻塞方法: put、take。
在两个线程中起到缓冲的作用,避免资源被浪费,提升性能
生产者消费者模式
-生产者:产生数据的线程。-消费者:使用数据的线程。
·实现类
- ArrayBlockingQueue
- LinkedBlockingQueue
- PriorityBlockingQueue、SynchronousQueue、DelayQueue等。
演示代码:
package com.nowcoder.community;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueTest {
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();
}
}
}
部分运行结果截图
kafka
Kafka简介
- Kafka是一个分布式的流媒体平台。
-应用:消息系统、日志收集、用户行为追踪、流式处理。
Kafka特点
高吞吐量(可以处理TB级的数据)
消息持久化(将数据存到硬盘里)
(对硬盘的顺序读取速度可能高于对内存的随机读取)
高可靠性(分布式部署)
高扩展性
Kafka术语
消息队列有两种实现方式,一种是点对点方式,即每个数据只被一个消费者消费,另一种是发布订阅模式,生产者将数据发布到某个位置,可以有很多消费者同时订阅这个位置,然后读取消息。kafka采用的是第二种方式。
- Broker(kafka集群中服务器)
- Zookeeper(用于管理kafka集群)
- Topic(存放消息的位置)
- Partition(分区)
- Offset(消息在分区内存放的索引)
- Leader Replica (主副本)
- .Follower Replica(随从副本,不负责读写)
安装
https://kafka.apache.org/downloads
安装包不区分操作系统
(使用kafka内置的zookeeper)
解压
配置:
修改zookeeper.properties
设置 dataDir=d:/work/data/zookeeper(路径为自己设置);
添加 audit.enable=true
修改server.properties
设置 log.dirs=d:/work/data/kafka-logs (路径为自己设置);
启动zookeeper 和kafka
命令行,进入kafka安装路径,执行命令
bin\windows\zookeeper-server-start.bat config\zookeeper.properties
再打开一个命令窗口,进入目录,执行命令
bin\windows\kafka-server-start.bat config\server.properties
检查配置的文件目录下新出现两个文件夹
使用
再打开一个命令行,进入目录
新建主题
kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
查看
kafka-topics.bat --list --bootstrap-server localhost:9092
生产者发送消息
kafka-console-producer.bat --broker-list localhost:9092 --topic test
再打开一个命令窗口
消费者读消息,进入目录,执行命令
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
两端实现通信
Spring整合kafka
配置
public class KafkaTest {
@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"}) //不需要注入kafkaTemplate,自动处理消息,引用该注解后,服务启动,Spring会自动监听该主题
public void handleMessage(ConsumerRecord record){
System.out.println(record.value());
}
}
文章内容来自牛客网java项目课程