当前位置:网站首页>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空转
边栏推荐
- 太牛了,看这篇足矣了
- Reflection and imagination on the notation like tool
- Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
- 如何实现游戏中的在线计时器和离线计时器
- cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
- 2022最新大厂Android面试真题解析,Android开发必会技术
- How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
猜你喜欢
Django使用mysqlclient服务连接并写入数据库的操作过程
Go语言 | 02 for循环及常用函数的使用
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
The road of enterprise digital transformation starts from here
JAD installation, configuration and integration idea
Technology sharing | common interface protocol analysis
PHP uses ueditor to upload pictures and add watermarks
1亿单身男女撑起一个IPO,估值130亿
5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
word如何转换成pdf?word转pdf简单的方法分享!
随机推荐
HAC cluster modifying administrator user password
Tupu software digital twin | visual management system based on BIM Technology
基于FPGA的超声波测距
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
1亿单身男女撑起一个IPO,估值130亿
Pandora IOT development board learning (HAL Library) - Experiment 8 timer interrupt experiment (learning notes)
JMeter 常用的几种断言方法,你会了吗?
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
HAC集群修改管理员用户密码
Go语言 | 02 for循环及常用函数的使用
中国银河证券开户安全吗 证券开户
Is the performance evaluation of suppliers in the fastener industry cumbersome? Choose the right tool to easily counter attack!
JS解力扣每日一题(十二)——556. 下一个更大元素 III(2022-7-3)
Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]
2022最新大厂Android面试真题解析,Android开发必会技术
Android面试,android音视频开发
C# 语言的基本语法结构
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
司空见惯 - 英雄扫雷鼠
After the company went bankrupt, the blackstones came