Redis的分布式和主备配置调研
package com.jd.redis.client;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;
publicclass RedisShardPoolTest {
static ShardedJedisPoolpool;
static{
JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
config.setMaxActive(500);//最大活动的对象个数
config.setMaxIdle(1000 * 60);//对象最大空闲时间
config.setMaxWait(1000 * 10);//获取对象时最大等待时间
config.setTestOnBorrow(true);
String hostA = "10.10.224.44";
int portA = 6379;
String hostB = "10.10.224.48";
int portB = 6379;
List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2);
JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
infoA.setPassword("redis.360buy");
JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
infoB.setPassword("redis.360buy");
jdsInfoList.add(infoA);
jdsInfoList.add(infoB);
pool =new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH,
Sharded.DEFAULT_KEY_TAG_PATTERN);
}
/**
* @param args
*/
publicstaticvoid main(String[] args) {
for(int i=0; i<100; i++){
String key = generateKey();
//key += "{aaa}";
ShardedJedis jds = null;
try {
jds = pool.getResource();
System.out.println(key+":"+jds.getShard(key).getClient().getHost());
System.out.println(jds.set(key,"1111111111111111111111111111111"));
} catch (Exception e) {
e.printStackTrace();
}
finally{
pool.returnResource(jds);
}
}
}
privatestaticintindex = 1;
publicstatic String generateKey(){
return String.valueOf(Thread.currentThread().getId())+"_"+(index++);
}
}
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class Test { /**
* 测试transfer的应用
* @param args
*/
public static void main(String[] args) {
testString(); }
public static void testString() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost",6379); Jedis jedis = pool.getResource();
try {
// 清空数据
System.out.println(jedis.flushDB());
String date = "20131225154209";
String date1 = "20131225154210";
String date2 = "20131225154211";
String date3 = "20131225154212"; HashMap msgMap1=new HashMap();
msgMap1.put("ID", "magid1");
msgMap1.put("Application", "hotelbe");
msgMap1.put("Data", "<OTrequest>reuqeustXML</OTrequest>");
msgMap1.put("DataLength", "200"); Person person = new Person();
person.setAge(BigDecimal.valueOf(23));
person.setName("haijun");
person.setSex("1"); Person person1 = new Person();
person1.setAge(BigDecimal.valueOf(23));
person1.setName("haijun1");
person1.setSex("1"); // 添加数据
byte[] str = SerializeUtil.serialize(person);
byte[] str1 = SerializeUtil.serialize(person1);
jedis.zadd("hotelBE".getBytes(), Double.valueOf(date), str);
jedis.zadd("hotelBE".getBytes(), Double.valueOf(date1), str1);
// jedis.zadd("hotelCE", Double.valueOf(date2), "zset");
// jedis.zadd("hotelCE", Double.valueOf(date3), "zset!");
// 元素个数
System.out.println(jedis.zcard("hotelBE"));
// 获取指定时间的元素
Set<byte[]> set = jedis.zrangeByScore("hotelBE".getBytes(), date.getBytes(), date1.getBytes());
int i=0;
for( Iterator it = set.iterator(); it.hasNext(); )
{
i++;
byte[] persons1 = (byte[]) it.next();
Person person2 = (Person)SerializeUtil.unserialize(persons1);
System.out.println(person2.getName());
}
System.out.println("此时间段内的消息个数为:"+i+"个"); } finally {
// 这里很重要,一旦拿到的jedis实例使用完毕,必须要返还给池中
pool.returnResource(jedis);
}
// 程序关闭时,需要调用关闭方法
pool.destroy(); } public static String getCurrentDateAndTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new java.util.Date(System.
currentTimeMillis()));
} }
附件是一份windows下redis服务端程序和客户端需要的jar包,附件下载后后缀改为.rar后解压缩
可以自己配置分布式和主从测试,
配置文件着重关注以下配置
服务端口和 绑定网卡IP
# Accept connections on the specified port, default is 6379
port 6178 # If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for connections.
#
# bind 127.0.0.1 是否为备机(IP:端口)
################################# REPLICATION ################################# # Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
slaveof 127.0.0.1 6378 认证密码
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
Redis的分布式和主备配置调研的更多相关文章
- Ubuntu16.04双网卡主备配置
前几日写了一篇Ubuntu14.04双网卡主备配置,没成想变化总是这么快,今日安装某软件,提示最匹配的ubuntu版本是16.04,作为一个码农能有什么办法,只能不断去适应变化.拥抱变化. 首先16. ...
- keepalived nginx 主备配置
keepalived nginx 主备配置(多主多备同理) 1.Nginx服务安装 nginx 不区分主备,在两台服务上安装两个即可. 安装参考:https://www.cnblogs.com/zw ...
- MySQL备份与主备配置
MySQL备份与主备配置 数据备份类型 全量备份:备份整个数据库 增量备份:备份自上一次备份以来(增量或完全)以来变化的数据 差异备份:备份自上一次完全备份以来变化的数据 全量备份 全量备份的方法有 ...
- Ubuntu14.04双网卡主备配置
近日有个需求,交换机有两台,做了堆叠,服务器双网卡,每个分别连到一台交换机上.这样就需要将服务器的网卡做成主备模式,以增加安全性,使得当其中一个交换机不通的时候网卡能够自动切换. 整体配置不难,网上也 ...
- mysql主备配置方法
1. 选择两台机器(这里选的centos6.5 final),安装相同版本的mysql yum install mysql ; yum install mysql-server; 2. 启动mysql ...
- mysql主备配置
目录 mysql主备2 一.master配置:2 1. 修改配置文件 2 2. 登录添加账号并赋权限 2 3. 查看master信息 2 二.slave配置:2 1. 修改配置文件 2 2. 重启登录 ...
- Keepalived 主备配置
keepalived主备或多主多备,配置都是一样配置方法,只是搭建多少的问题. 1.keepalived安装 参考:https://www.cnblogs.com/zwcry/p/9542867.ht ...
- Mysql 主备配置
来自:http://blog.csdn.net/u013256816/article/details/52536283 1. 了解主备配置过程原理. http://blog.csdn.net/u013 ...
- Redis(1.13)Redis cluster 分布式集群手动配置
[1]试验环境 结构图如下: (这里试验没有那么多机器,就用3台机器搭建试验) redis1是redis集群的一个节点A,上面运行了两个redis实例,7001 7004 redis2是redis集群 ...
随机推荐
- redis module 学习—官网文档整理
前言 redis在4.0版本中,推出了一个非常吸引的特性,可以通过编写插件的模式,来动态扩展redis的能力.在4.0之前,如果用户想拥有一个带TTL的INCRBY 命令,那么用户只能自己去改代码,重 ...
- Excel中vlookup函数使用
https://baijiahao.baidu.com/s?id=1594684818733205984&wfr=spider&for=pc
- Socket网络编程系列教程序
C语言的用途相当多,可以用在数据结构.数据库.网络.嵌入式等方面,历经40多年不衰,真是厉害!最近一直想从某一应用方面写一个系列教程,好好地把某一方面讲深讲透. 正好博主对网络方面的编 ...
- LiteIDE TARGETARGS -conf_path=E:/PokerServer/src/GameServer/conf/texas.xml -log_dir=E:/PokerServer/src/GameServer/main/log
LiteIDE TARGETARGS -conf_path=E:/PokerServer/src/GameServer/conf/texas.xml -log_dir=E:/PokerServer/s ...
- TestNG的静态方法mock的步骤
最近团队内部对程序中使用大量的`静态方法`,而公司要求要有sonar扫描覆盖率的,因为在大量使用静态方法的地方若不mock,则覆盖率达不到.于是网上很少的文章讲解对静态方法的mock,大多都是如何使用 ...
- 个人永久性免费-Excel催化剂功能第70波-工作薄外部链接维护管理
Excel在数据领域万物互联的特性,其中一个使用场景是连接非本工作薄的外部性文件内容,如其他Excel工作薄文件里的内容或直接用OLE对象的方式嵌入一个文件链接,使其在不离开Excel环境,也可提供类 ...
- pyqt QT设计师制作关于对话框(软件版权申明)
一.实验环境 1.anaconda2 2.5.0 + python2.7 2.pyinstaller3.0 二.操作步骤 2.1 启动designer.exe 2.2 单击“文件” -> “新建 ...
- mysql查询表所有列名,并用逗号分隔
SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ",") FROM information_schema.COLUMNS WHERE TABLE ...
- backtracing
5月10日 1 37 Sudoku Slover public void solveSudoku(char[][] board) { if(board == null || board.length ...
- 知识图谱学习与实践(4)——Protégé使用入门
1 Protégé简介 Protégé是一个本体建模工具软件,由斯坦福大学基于java语言开发的,属于开放源代码软件.软件主要用于语义网中本体的构建和基于本体的知识应用,是本体构建的核心开发工具,最新 ...