当前位置:网站首页>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空转
边栏推荐
- Go语言 | 03 数组、指针、切片用法
- 5年经验Android程序员面试27天,2022程序员进阶宝典
- Reflection and imagination on the notation like tool
- ELK分布式日志分析系统部署(华为云)
- word如何转换成pdf?word转pdf简单的方法分享!
- IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
- 开源 SPL 消灭数以万计的数据库中间表
- You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
- Blue sky drawing bed Apple quick instructions
猜你喜欢

出海十年:新旧接力,黑马崛起

5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic

为什么 BI 软件都搞不定关联分析?带你分析分析

How to realize the Online timer and offline timer in the game

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

Why can't Bi software do correlation analysis? Take you to analyze

Fuzor 2020軟件安裝包下載及安裝教程

Ultrasonic ranging based on FPGA

数据库 逻辑处理功能

cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
随机推荐
Use file and directory properties and properties
Postman核心功能解析-参数化和测试报告
Hiengine: comparable to the local cloud native memory database engine
Word finds red text word finds color font word finds highlighted formatted text
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
The basic grammatical structure of C language
618“低调”谢幕,百秋尚美如何携手品牌跨越“不确定时代”?
MySQL中字段类型为longtext的值导出后显示二进制串方式
Talking about fake demand from takeout order
PHP利用ueditor实现上传图片添加水印
JAD installation, configuration and integration idea
5年经验Android程序员面试27天,2022程序员进阶宝典
中国银河证券开户安全吗 证券开户
golang通过指针for...range实现切片中元素的值的更改
微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
C# 语言的高级应用
关于 Notion-Like 工具的反思和畅想
Tupu software digital twin smart wind power system
How to realize the Online timer and offline timer in the game
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】