当前位置:网站首页>redis集群模拟消息队列
redis集群模拟消息队列
2022-07-05 19:11:00 【玄妙之门】
- 准备工作:安装redis并搭建好集群
- IDEA新建项目编写测试类RedisTest,添加集群环境配置代码
private Jedis jedis;
private JedisCluster jedisCluster;
@Before
public void connectionRedis(){
jedis = new Jedis("192.168.1.118",6379);
//jedis = new Jedis("192.168.1.20",6382);
//jedis.auth("123456");
HostAndPort hp1 = new HostAndPort("192.168.1.20", 6381);
HostAndPort hp2 = new HostAndPort("192.168.1.20", 6382);
HostAndPort hp3 = new HostAndPort("192.168.1.20", 6383);
HostAndPort hp4 = new HostAndPort("192.168.1.20", 6384);
HostAndPort hp5 = new HostAndPort("192.168.1.20", 6385);
HostAndPort hp6 = new HostAndPort("192.168.1.20", 6386);
Set<HostAndPort> hostAndPortSet = new HashSet<>();
hostAndPortSet.add(hp1);
hostAndPortSet.add(hp2);
hostAndPortSet.add(hp3);
hostAndPortSet.add(hp4);
hostAndPortSet.add(hp5);
hostAndPortSet.add(hp6);
jedisCluster = new JedisCluster(hostAndPortSet);
}
- 添加入队测试方法
//集群模式测试入队操作
@Test
public void testEnterQueue(){
jedisCluster.lpush("queue-1","感谢您对本店的支持");
}
- 添加出队测试方法
//集群模式测试出队操作
@Test
public void testOutQueue() throws InterruptedException{
Thread t = new Thread("thread-getmsg"){
@Override
public void run() {
while(true){
String s = jedisCluster.rpop("queue-1");
if(null==s){
try {
Thread.sleep(3*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(s);
}
}
};
t.start();
t.join();//保证子线程结束后主线程才结束
}

可以看到消息队列中如果没有消息,则线程休眠3秒执行,这样可有效防止CPU空转
边栏推荐
- Decision tree and random forest
- 微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
- 2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
- Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
- 在线协作产品哪家强?微软 Loop 、Notion、FlowUs
- 手把手教你处理 JS 逆向之图片伪装
- 数学分析_笔记_第9章:曲线积分与曲面积分
- MMO project learning 1: preheating
- 数据库 逻辑处理功能
- Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?
猜你喜欢

不愧是大佬,字节大牛耗时八个月又一力作

块编辑器如何选择?印象笔记 Verse、Notion、FlowUs

Summer Challenge database Xueba notes, quick review of exams / interviews~

软件测试是干什么的?学习有啥要求?

Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?

【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
![[performance test] jmeter+grafana+influxdb deployment practice](/img/32/f07792734d040829398a90a2040146.png)
[performance test] jmeter+grafana+influxdb deployment practice

Blue sky drawing bed Apple quick instructions

Ultrasonic ranging based on FPGA

IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
随机推荐
How to convert word into PDF? Word to PDF simple way to share!
shell编程基础(第9篇:循环)
android中常见的面试题,2022金九银十Android大厂面试题来袭
Go语言 | 03 数组、指针、切片用法
Use file and directory properties and properties
Debezium系列之:解析默认值字符集
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
信息/数据
Benefits of automated testing
Android interview, Android audio and video development
5年经验Android程序员面试27天,2022程序员进阶宝典
Apprentissage du projet MMO I: préchauffage
14、用户、组和权限(14)
Mariadb root用户及普通用户的密码 重置
uniapp获取微信头像和昵称
[performance test] jmeter+grafana+influxdb deployment practice
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
强化学习-学习笔记4 | Actor-Critic
word如何转换成pdf?word转pdf简单的方法分享!