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集群 ...
随机推荐
- C++模版的用法
模板是实现代码重用机制的一种工具,实质就是实现类型参数化,即把类型定义为参数. C++提供两种模板:函数模板,类模板 函数模板 template <typename T> T myMax( ...
- I/O:OutputStream
OutputStream: void close() :关闭此输出流并释放与此流有关的所有系统资源. void flush() :刷新此输出流并强制写出所有缓冲的输出字节. void write(by ...
- 【题解】P1892 [BOI2003]团伙-C++
原题传送门 前置知识:并查集,不会的补了再来. 这道题只是在并查集的基础上多了一个操作而已. 这种操作,叫做反集(就先这么叫着) 题目里有一种关系是互为朋友,这很好理解,把互为朋友的两个点合并就可以了 ...
- 数据库中 ’’ 和 NULL的区别
null不是对象,''是对象 从'',你就可以知道这是一个字符串类型的数据,是一个长度为零的字符串. 从NULL,你只能知道这里没有赋过值,是空的,他不属于任何数据类型. 我们在数据库实际使用中,一般 ...
- who are you?
不多说,直接使用脚本跑 # -*- coding:utf-8 -*- import requests import string url = "http://ctf5.shiyanbar.c ...
- cesium 学习(四) Hello World
一.前言 之前的文章都是基础,搭建环境.部署Cesium.学习资料等等.现在简单入手,一个Hello World页面开发. 二.Hello World 感觉Hello World没有什么特别需要讲的, ...
- python基础之list列表的增删改查以及循环、嵌套
Python的列表在JS中又叫做数组,是基础数据类型之一,以[]括起来,以逗号隔开,可以存放各种数据类型.嵌套的列表.对象.列表是有序的,即有索引值,可切片,方便取值.列表的操作和对字符串的操作是一样 ...
- PHP 跨域处理
PHP 跨域处理 跨域访问失败是会出现 No 'Access-Control-Allow-Origin' header is present on the requested resource. Or ...
- HashTable源码解读
一:总述 底层实现原理是用数组+链表,与HashMap一样,但HashTable是线程安全的,HashMap是非线程安全的 下面是其结构图(与hashMap类似) 二:属性说明 /** * The h ...
- GitLab-CI 来自动创建 Docker 镜像
1.what is gitlab-ci docker image CI/CD 自动化集成,自动化部署.简单的说就是把代码提交到gitlab管理的同时部署到指定的server,打成docker imag ...