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集群 ...
随机推荐
- Mybatis__模糊查询
在一个Web工程中,查询功能几乎都要用到姓名模糊查询,,虽然学号,工号等可以最准确最快的定位,但如果清楚信息到连学号,工号都一个数不差,应该也没必要去查询了. 故需要用到一下语句实现模糊查询: sel ...
- 剑指offer第二版-5.替换空格
面试题5:替换空格 题目要求: 实现一个函数,把字符串中的每个空格都替换成“%20”,已知原位置后面有足够的空余位置,要求改替换过程发生在原来的位置上. 思路: 首先遍历字符串求出串中空格的数量,求出 ...
- WinForm控件之【CheckBox】
基本介绍 复选框顾名思义常用作选择用途,常见的便是多选项的使用: 常设置属性.事件 Checked:指示组件是否处于选中状态,true为选中处于勾选状态,false为未选中空白显示: Enabled: ...
- 手机web app开发笔记
各位朋友好,最近自学开发了一个手机Web APP,“编程之路”,主要功能包括文章的展示,留言,注册登录,音乐播放等.为了记录学习心得,提高自己的编程水平,也许对其他朋友有点启发,特整理开发笔记如下. ...
- SpringBoot开发案例Nacos配置管理中心
前言 在开发过程中,通常我们会配置一些参数来实现某些功能,比如是否开启某项服务,告警邮件配置等等.一般会通过硬编码.配置文件或者数据库的形式实现. 那么问题来了,如何更加优雅的实现?欢迎来到 Naco ...
- 解决 mysql多表联合查询时出现的分页问题
mysql一对多分页问题 部门表:tbl_dept 员工表:tbl_emp 数据库sql文件 CREATE DATABASE /*!32312 IF NOT EXISTS*/`ssm-crud` /* ...
- MapReduce之WordCount
用户统计文件中的单词出现的个数 注意各个文件的导包,job的封装步骤 WordCountMapper.java package top.wintp.mapreduce.wordcount; impor ...
- [小米OJ] 10. 爬楼梯
dp 另: 小米oj上的测试样例是错的 ; ) function solution(line) { if (line == 0) return 0; if (line == 1) return 1; ...
- 成为高级 React 开发你需要知道的知识点
简评:除了常见的 HOC 和 RenderProp 技巧,作者介绍了 7 个有用的知识点. 使用 Fragment 而不是 div 很多时候我们想要处理多个 component,但是 render 只 ...
- php上传excle文件,csv文件解析为二维数组
解析上传的CSV文件不是什么难事,直接读取转成你想要的数组样子就OK了. public function putStoreStockIn ($filePath = '') { $file = fope ...