一、连接单机版的 redis

/**
* 直接连接 redis
* @throws Exception
*/
@Test
public void test1() throws Exception {
//创建一个 jedis 对象,参数:host、post
Jedis jedis = new Jedis("192.168.25.128", 6379);
//直接通过 jedis 操作 redis,每个 redis 命令都对应一个方法
jedis.set("a", "hello");
String str = jedis.get("a");
System.out.println(str);
//关闭连接
jedis.close();
} /**
* 通过连接池连接 redis
* @throws Exception
*/
@Test
public void test2() throws Exception{
//创建一个连接池对象,两个参数 host、post
JedisPool pool = new JedisPool("192.168.25.128", 6379);
//从连接池获得一个连接,就是一个 jedis 对象
Jedis jedis = pool.getResource();
//使用 jedis 操作 redis
String str = jedis.get("a");
System.out.println(str);
//关闭连接(每次使用完毕后都要关闭连接)
jedis.close();
//关闭连接池
pool.close();
}

二、连接 redis 集群

/**
* jedis 连接 redis 集群
* @throws Exception
*/
@Test
public void test3() throws Exception{
//创建一个 JedisCluster 对象(单例)。有一个参数 nodes,是一个 set 类型。set 中包含若干个 HostAndPort 对象
Set<HostAndPort> nodes = new HashSet();
nodes.add(new HostAndPort("192.168.25.128", 7001));
nodes.add(new HostAndPort("192.168.25.128", 7002));
nodes.add(new HostAndPort("192.168.25.128", 7003));
nodes.add(new HostAndPort("192.168.25.128", 7004));
nodes.add(new HostAndPort("192.168.25.128", 7005));
nodes.add(new HostAndPort("192.168.25.128", 7006));
JedisCluster jedisCluster = new JedisCluster(nodes);
//直接使用 JedisCluster对象操作 redis。(JedisCluster 自带连接池,不用关闭)
jedisCluster.set("b", "123");
String str = jedisCluster.get("b");
System.out.println(str);
//系统关闭之前,关闭 JedisCluster
jedisCluster.close();
}

三、开发过程中用单机版,实际才用集群版

最新文章

  1. HDU 5673 Robot ——(卡特兰数)
  2. oracle 同时更新(update)多个字段多个值
  3. FQ技术
  4. 【leetcode】Letter Combinations of a Phone Number
  5. Python 中文Key 报错问题
  6. Linux下统计出现次数最多的指定字段值
  7. R语言中strptime返回值永远为NA的问题
  8. web前端炫酷实用的HTML5应用和jQuery插件
  9. python——异常处理
  10. IOS 项目名称修改(XCODE4.6)
  11. [转载][HTML] 普通的DIV分层以及版透明效果
  12. vc2015 编译libcurl带openssl
  13. EMC题
  14. Vuex初识
  15. DirectX11 With Windows SDK--23 立方体映射:动态天空盒的实现
  16. win7下利用ftp实现华为路由器的配置文件上传和下载
  17. I/O 流
  18. Django分页(一)
  19. 【Java】 剑指offer(65) 不用加减乘除做加法
  20. B - Pie

热门文章

  1. Unable to read the project file &amp;#39;client.csproj&amp;#39;. Could not load file or assembly &amp;#39;Microsoft.Build.En
  2. C++实现顺序栈的基本功能
  3. SVNserver搭建和使用(二)
  4. Oracle 11gR2 List-Range分区实验
  5. POJ1837 Balance 背包
  6. 修改linux内核的启动logo和禁用启动光标【转】
  7. 分布式系统CAP原则与BASE思想
  8. 原生JS通过勾股定理计算苹果菜单效果
  9. Oracle数据库学习1------数据库安装及客户端配置
  10. node.js date-utils