总共四台机器,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 @Test
10 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--6379
1

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 @Test
10 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 }

【转】Java代码操作Redis的sentinel和Redis的集群Cluster操作的更多相关文章

  1. Java代码操作Redis的sentinel和Redis的集群Cluster操作

    总共四台机器,crxy99,crxy98分别是主节点和从节点.   crxy97和crxy96是两个监控此主从架构的sentinel节点. 看代码: import org.junit.Test; im ...

  2. Linux--6 redis订阅发布、持久化、集群cluster、nginx入门

    一.redis发布订阅 Redis 通过 PUBLISH .SUBSCRIBE 等命令实现了订阅与发布模式. 其实从Pub/Sub的机制来看,它更像是一个广播系统,多个Subscriber可以订阅多个 ...

  3. redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用

    目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...

  4. redis与集群实用操作笔记

    redis哨兵 部署方式 redis配置 首先需要区分的是主从redis,主机也就是用来写的机器,从机是从来读的,为主机分担压力,与集群不同的是redis哨兵不可通过从机写入数据同步到主机,但是也可以 ...

  5. Java接口对Hadoop集群的操作

    Java接口对Hadoop集群的操作 首先要有一个配置好的Hadoop集群 这里是我在SSM框架搭建的项目的测试类中实现的 一.windows下配置环境变量 下载文件并解压到C盘或者其他目录. 链接: ...

  6. redis 5.0.3 讲解、集群搭建

    REDIS 一 .redis 介绍 不管你是从事Python.Java.Go.PHP.Ruby等等... Redis都应该是一个比较熟悉的中间件.而大部分经常写业务代码的程序员,实际工作中或许只用到了 ...

  7. Redis安装(单机及各类集群,阿里云)

    Redis安装(单机及各类集群,阿里云) 前言 上周,我朋友突然悄悄咪咪地指着手机上的一篇博客说,这是你的博客吧.我看了一眼,是之前发布的<Rabbit安装(单机及集群,阿里云>.我朋友很 ...

  8. 2017最新技术java高级架构、千万高并发、分布式集群、架构师入门到精通视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩展. ...

  9. redis基础之redis-sentinel(哨兵集群)(六)

    前言 redis简单的主从复制在生产的环境下可能是不行的,因为从服务器只能读不能写,如果主服务器挂掉,那么整个缓存系统不能写入了:redis自带了sentinel(哨兵)机制可以实现高可用. redi ...

随机推荐

  1. 【Java】使用BigDecimal类进行精确小数计算

    在商业计算中(尤其是计算价格)需要使用BigDecimal类来进行精确小数计算,因为用其他类型计算(如double)得到的结果不是精确的! 写个测试类. import org.junit.Test; ...

  2. [uart]设置linux 串口的block方式

    1. 如果设置为非block模式: open(device, O_RDWR | O_NDELAY | O_NONBLOCK); 2. 如果设置为block模式,且读固定字节数返回则termios.c_ ...

  3. AndroidA——背景选择器selector用法汇总(一)

    一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...

  4. dapper支持操作函数和事物

    dapper除了支持基础的CURD.存储过程以外,还支持操作函数和事物. dapper操作函数的代码如下: using Dapper; using System; using System.Colle ...

  5. mybatis动态sql中的两个内置参数(_parameter和_databaseId)

    mybatis动态sql中的两个内置参数(_parameter和_databaseId)   <!-- mybatis动态sql的两个内置参数           不只是方法传递过来的参数可以被 ...

  6. im ui框架调研,对比

    1, https://github.com/jpush/aurora-imui 极光的 支持 Android/iOS/RN 提供了消息列表.输入视图 功能不多, 2,https://github.co ...

  7. js学习(一)-对象和函数概念

    //-----------------------js代码-------------------- function class1(){       //类成员的定义及构造函数 this.name = ...

  8. lr11.0负载测试 real-world schedule 与basic schedule的区别是什么

    real-world schedule 是真实场景模式  可以通过增加ACTION来增加多个用户 basic schedule 是我们以前用的 经典模式  只能设置一次负载的上升和下降

  9. Android O seLinux 编译错误

    编译android O源码,遇到错误 FAILED: out/target/product/hon450/obj/ETC/sepolicy_intermediates/sepolicy /bin/ba ...

  10. 第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理

    第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理 1.映射(mapping)介绍 映射:创建索引的时候,可以预先定义字 ...