Java代码操作Redis的sentinel和Redis的集群Cluster操作
来源:互联网 发布:二维码追溯系统源码 编辑:程序博客网 时间:2024/06/11 15:27
Java代码操作Redis的sentinel和Redis的集群Cluster操作
Jedis操作Redis的sentinel示例代码:
总共四台机器,crxy99,crxy98分别是主节点和从节点. crxy97和crxy96是两个监控此主从架构的sentinel节点.
上代码:
1 import org.junit.Test; 2 3 import redis.clients.jedis.HostAndPort; 4 import redis.clients.jedis.Jedis; 5 import redis.clients.jedis.JedisPoolConfig; 6 import redis.clients.jedis.JedisSentinelPool; 7 8 public class TestSentinel { 9 @Test10 public void test1() {11 JedisPoolConfig poolConfig = new JedisPoolConfig();12 String masterName = "mymaster";13 Set<String> sentinels = new HashSet<String>();14 sentinels.add("192.168.1.97:26379");15 sentinels.add("192.168.1.96:26379");16 JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, poolConfig);17 HostAndPort currentHostMaster = jedisSentinelPool.getCurrentHostMaster();18 System.out.println(currentHostMaster.getHost()+"--"+currentHostMaster.getPort());//获取主节点的信息19 Jedis resource = jedisSentinelPool.getResource();20 String value = resource.get("a");21 System.out.println(value);//获得键a对应的value值22 resource.close();23 }24 25 }
运行结果入下:
192.168.1.99--63791
Jedis操作集群示例代码:
模拟的集群环境.在一台机器上启动多个redis..每个redis对应的是不同端口.
在crxy99 192.168.1.99上启动的....总共3主3从 端口号对应的的是7000~7005.....
看代码:
1 import java.util.HashSet; 2 import java.util.Set; 3 import org.junit.Test; 4 import redis.clients.jedis.HostAndPort; 5 import redis.clients.jedis.JedisCluster; 6 import redis.clients.jedis.JedisPoolConfig; 7 8 public class TestCluster { 9 @Test10 public void test1() throws Exception {11 JedisPoolConfig poolConfig = new JedisPoolConfig();12 Set<HostAndPort> nodes = new HashSet<HostAndPort>();13 HostAndPort hostAndPort = new HostAndPort("192.168.1.99", 7000);14 HostAndPort hostAndPort1 = new HostAndPort("192.168.1.99", 7001);15 HostAndPort hostAndPort2 = new HostAndPort("192.168.1.99", 7002);16 HostAndPort hostAndPort3 = new HostAndPort("192.168.1.99", 7003);17 HostAndPort hostAndPort4 = new HostAndPort("192.168.1.99", 7004);18 HostAndPort hostAndPort5 = new HostAndPort("192.168.1.99", 7005);19 nodes.add(hostAndPort);20 nodes.add(hostAndPort1);21 nodes.add(hostAndPort2);22 nodes.add(hostAndPort3);23 nodes.add(hostAndPort4);24 nodes.add(hostAndPort5);25 JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig);//JedisCluster中默认分装好了连接池.26 //redis内部会创建连接池,从连接池中获取连接使用,然后再把连接返回给连接池27 String string = jedisCluster.get("a");28 System.out.println(string); 29 }30 }
阅读全文
0 0
- Java代码操作Redis的sentinel和Redis的集群Cluster操作
- 基于Sentinel的Java客户端操作Redis
- Spring-data-redis操作Redis的Sentinel
- 实现redis(主从,sentinel,cluster)的主从复制集群
- redis cluster和redis sentinel的一些文章
- Redis: sentinel集群的搭建和测试
- Redis集群的操作
- Redis集群的操作
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis操作Redis的Sentinel
- 使用Spring-data-redis操作Redis的Sentinel
- Redis Cluster 集群的实现和管理
- Redis集群:redis主从自动切换Sentinel操作实例
- Redis源码剖析和注释(二十四)--- Redis Sentinel实现(哨兵操作的深入剖析)
- redis cluster模式key的模糊删除-java操作
- php操作redis cluster集群成功实例
- php操作redis cluster集群成功实例
- php操作redis cluster集群成功实例
- Unity3d shader 教程二 最小着色器
- 巨坑!用SlidingMenu导致ListView中点击item失效的原因
- [Hash] BZOJ 4076 [Wf2014]Maze Reduction
- 生命该如何我去走
- google test 环境搭建
- Java代码操作Redis的sentinel和Redis的集群Cluster操作
- Activity的启动模式
- QTextStream读写文件
- tomcat——无限循环的警告
- Java集成redis集群
- 关于iCheck 单选框的事件绑定
- quick cocos2dx 自定义事件管理器
- QLabel中html标签的使用
- SonicOperator之实验2