Jedis连接Redis三种模式
这里说的三种工作模式是指:
1、单机模式
2、分片模式
3、集群模式(since 3.0)
说明图详见以下:
使用单机模式连接:
private String addr="192.168.1.1";
private String port="6236";
private String key="key";
private Jedis jedis=new Jedis(addr,port);//Jedis获取到的Redis数据在jedis里,
jedis.set("a","b");//更改key为a的值
jedis.hmset(key,hash);
System.out.println(jedis.get(key));
使用分片模式连接:
GenericObjectPoolConfig config=new GenericObjectPoolConfig();
config.setMaxIdle(32);
config.setMinIdle(12);
config.setTestOnBorrow(true);
config.setTestOnReturn(rtrue);
config.setTestWhileIdle(true);
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
for (int i = 0; i < shareds.size(); i++) {
shards.add(new JedisShardInfo("192.168.0.100", 6379, 200));
}
// 构造池
ShardedJedisPool shardedJedisPool= new ShardedJedisPool(config, shards);
ShardedJedis jedis=shardedJedisPool.getResource();
jedis.set("a","b");
jedis.hmset(key, hash);
使用集群模式:
String[] ADDRs = conf.getString("redisIP", "10.244.84.33").split(",");//conf是读取的配置
String[] PORTs = conf.getString("redisPort", "6379").split(",");
for (int i = 0; i < length; i++) {
HostAndPort hostAndPort = new HostAndPort(ADDRs[i], Integer.parseInt(PORTs[i]));
haps.add(hostAndPort);
}
JedisCluster myJedisCluster = new JedisCluster(haps, TIMEOUT);
Map<String, String> gather = new HashMap<>(); //Redis中的数据是<key,value>形式,也可以是其他类型的数据
gather =myJedisCluster.hgetAll(key) ;
补充:使用JedisPool连接Redis:
public class RedisUtils {
static {
String configurationFileName = "xxx.properties";
Configuration conf = Configuration.getConfiguration(configurationFileName);
if (conf == null) {
System.out.println("reading " + configurationFileName + " is failed.");
System.exit(-1);
}
String[] ADDRs = conf.getString("redisIP", "10.100.56.33").split(",");
String[] PORTs = conf.getString("redisPort", "6379").split(",");
if (ADDRs.length == 0 || PORTs.length == 0) {
System.out.println("definition redisIP is not found in " + configurationFileName);
System.exit(-1);
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_IDLE);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDRs[0], PORT, TIMEOUT);
}
public synchronized static Jedis getJedis() {
if (jedisPool != null) {
//获取资源
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
}
public static void returnBrokenResource(Jedis jedis) {
if (jedis != null) {
//释放资源
jedisPool.returnBrokenResource(jedis);
}
}
public static void returnResource(Jedis jedis) {
if (jedis != null) {
//释放资源
jedisPool.returnResource(jedis);
}
}
}
尚有需要改进之处,暂时记录一下
Jedis连接Redis三种模式的更多相关文章
- [转]VMware Workstation网络连接的三种模式
经常要使用VMWare Workstation来在本地测试不同的操作系统,以前也搞不清楚网络连接三种模式,最近看了几篇文章才算明白.现总结如下: 1. VMware Workstation的虚拟网络组 ...
- Redis三种模式——主从复制,哨兵模式,集群
一.Redis主从复制作用 数据冗余:主从复制实现了数据的热备份,是持久化之外的一种数据冗余方式. 故障恢复:当主节点出现问题时,可以由从节点提供服务,实现快速的故障恢复:实际上是一种服务的冗余. 负 ...
- redis三种模式对比
模式类型 主从模式(redis2.8版本之前的模式).哨兵sentinel模式(redis2.8及之后的模式).redis cluster模式(redis3.0版本之后) 主从模式原理 同Mysql主 ...
- 理解VMware虚拟机下网络连接的三种模式(如何配置虚拟机上网)
很多朋友都用vmware来测试不同的系统,我结合自己的经验谈一下对网络设置的理解,不对的地方请指正. bridge:这种方式最简单,直接将虚拟网卡桥接到一个物理网卡上面,和linux下一个网卡 绑定两 ...
- Linux初学之vmware Workstation 网络连接三种模式
简介: VM(VMware Workstation简称VM,后面都将用VM代替阐述)是一款功能强大的虚拟化软件.VM支持在 单一的桌面上同时运行多款不同的操作系统,能够模拟完整的网络环境,支持pxe功 ...
- VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
VMware虚拟机上网络连接(network type)的三种模式--bridged.host-only.NAT VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换 ...
- centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB 注意down掉网卡的方法 nginx效率没有LVS高 ipvsadm命令集 测试LVS方法 第三十三节课
centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB ...
- hadoop学习;自己定义Input/OutputFormat;类引用mapreduce.mapper;三种模式
hadoop切割与读取输入文件的方式被定义在InputFormat接口的一个实现中.TextInputFormat是默认的实现,当你想要一次获取一行内容作为输入数据时又没有确定的键.从TextInpu ...
- 用Jedis连接Redis
jedis中的方法名,和Redis的命令几乎一样 1.jar包,作为测试只需要一个jar 2.代码 package com; import java.util.HashMap; import java ...
随机推荐
- Metaweblog博客分发体验
在8月份OpenLiveWriter 这篇文章使用博客客户端撰写做了metaweblog的个人服务,支持通过OpenLiveWriter发博客到本站(OurJS),然后再分发到其他博客平台(目前就os ...
- line 3: /usr/local/arm/4.3.2/bin/arm-none-linux-gnueabi-gcc: No such file or directory
sudo apt-get install lib32ncurses5(网上下载的很多arm-linux-gcc都是32位的,64位的ubuntu需要按此包)
- 【BZOJ】1823: [JSOI2010]满汉全席(2-sat)
题目 传送门:QWQ 分析 2-sat模板(然而辣鸡如我还是调了好久) 代码 //bzoj 1823 2-sat #include <bits/stdc++.h> using namesp ...
- Druid.io系列(五):查询过程
原文链接: https://blog.csdn.net/njpjsoftdev/article/details/52956194 Druid使用JSON over HTTP 作为底层的查询语言,不过强 ...
- fir 窗口设计法
加窗的原因.对于理想的低通滤波器H(exp(jw)),其h(n)是无限长序列.这是可以证明的.因此为了得到有限长的h(n)就需要截断,而这个过程就是加窗.由于h(n)截断即其频率响应就和理想的低通滤波 ...
- java并发的基本概念和级别
并发的概念: 并发(Concurrency)和并行(Parallelism) 并发偏重于多个任务交替执行,而多个任务之间有可能还是串行的.而并行是真正意义上的“同时执行”.严格意义上来说,并行的多个任 ...
- martin/docker-cleanup-volumes
https://hub.docker.com/r/martin/docker-cleanup-volumes/
- 22_java之File对象
01IO技术概述 * A:IO技术概述 * a: Output * 把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作 * b: Input * 把持久设备上的数据读取到内存中的这 ...
- nginx代理socket tcp/udp
准备一台linux服务器.nginx官网:http://nginx.org/ .在网上搜到大致用的是 ngx_stream_core_module 这个模块,这里你也可以关注一下官方文档中的其他模块都 ...
- [Z] 用GDB调试程序
原文:http://blog.csdn.net/haoel/article/details/2879 用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工 ...