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
- 启动zookeeper
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());
}
}
- 运行结果