redis补充
Redis集群搭建过程
一、环境
(1)centos 7.3虚拟机
(2)Redis 3.0.0 源码
(3)apt-get install ruby apt-get install rubygems
(4)redis-3.0.0.gem
二、安装过程
(1)上传redis.并解压。
Alt + p
Tar -zxvf redis.3.0.0.tar.gz
(2)编译源码
Make && make install,并安装
(3)开始搭建集群
① 复制redis.conf到多个目录【集群中节点的个数】
② 修改配置文件
Port XXX
Pidfile /var/run/redis-XXX.pid
Deamonize yes 将daemonize的值改为yes (这是后台运行)
Cluster-enabled yes
Appenonly yes
③ 然后分别启动集群中的节点
Redis-server redis.conf
④ ./redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385【我的是伪分布式】
没有报错基本就对了。
踩过的坑:
(1)修改配置忘记修改其中的一个Deamonize yes
(2)集群搭建6个以上才可以搭建成功,2个搭建不起来,三个会报错。
(3)集群搭建的过程中,不可以有数据。
bind 127.0.0.1 (只能本机访问)
改为
bind 0.0.0.0 (非本机亦可访问(或者可以绑定指定IP))
远程访问 :将bind 127.0.0.1 改成了 bind 0.0.0.0
性能测试:
(1)请求数据的测试
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -t set,lpush -n 10000 -q
SET: 76335.88 requests per second
LPUSH: 66666.66 requests per second
集群中超过半数以上的节点死掉就会down,我试过6个,死掉3个可以使用,死掉4个的时候就会不会再提供使用了。
试过7个节点,死掉四个就不可以使用了。
添加节点的时候,节点中不可以有数据,必须是一个空的目录。
(2)并发测试
1000
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -t set,lpush -n 100000000 -q
^CT: 65701.260
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -c set,lpush -n 1000 -q
PING_INLINE: 22727.27 requests per second
PING_BULK: 25641.03 requests per second
SET: 23809.52 requests per second
GET: 22222.22 requests per second
INCR: 22222.22 requests per second
LPUSH: 13513.51 requests per second
LPOP: 14705.88 requests per second
SADD: 23255.81 requests per second
SPOP: 22727.27 requests per second
LPUSH (needed to benchmark LRANGE): 14285.71 requests per second
LRANGE_100 (first 100 elements): 15384.62 requests per second
LRANGE_300 (first 300 elements): 7812.50 requests per second
LRANGE_500 (first 450 elements): 5681.82 requests per second
LRANGE_600 (first 600 elements): 4484.30 requests per second
MSET (10 keys): 22727.27 requests per second
10000
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -c set,lpush -n 10000 -q
PING_INLINE: 49751.24 requests per second
PING_BULK: 53191.49 requests per second
SET: 49019.61 requests per second
GET: 48309.18 requests per second
INCR: 48543.69 requests per second
LPUSH: 25906.73 requests per second
LPOP: 24937.66 requests per second
SADD: 50000.00 requests per second
SPOP: 50251.26 requests per second
LPUSH (needed to benchmark LRANGE): 25839.79 requests per second
LRANGE_100 (first 100 elements): 31446.54 requests per second
LRANGE_300 (first 300 elements): 18248.18 requests per second
LRANGE_500 (first 450 elements): 13812.16 requests per second
LRANGE_600 (first 600 elements): 11454.75 requests per second
MSET (10 keys): 45045.04 requests per second
100000
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -c set,lpush -n 100000 -q
PING_INLINE: 51150.89 requests per second
PING_BULK: 51203.28 requests per second
SET: 48076.93 requests per second
GET: 47573.74 requests per second
INCR: 48309.18 requests per second
LPUSH: 25380.71 requests per second
LPOP: 25667.35 requests per second
SADD: 49751.24 requests per second
SPOP: 50556.12 requests per second
LPUSH (needed to benchmark LRANGE): 25157.23 requests per second
LRANGE_100 (first 100 elements): 31065.55 requests per second
LRANGE_300 (first 300 elements): 17531.56 requests per second
LRANGE_500 (first 450 elements): 13433.64 requests per second
LRANGE_600 (first 600 elements): 10833.06 requests per second
MSET (10 keys): 43535.05 requests per second
使用默认的方式
(1)不必每次都运行 redis-benchmark 默认的所有测试。 使用 -t 参数可以选择你需要运行的测试用例,比如下面的范例:
① [root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -t set,lpush -n 100000 -q
SET: 89126.56 requests per second
LPUSH: 70323.48 requests per second
(2)测试所有
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -n 100000 -q
PING_INLINE: 93720.71 requests per second
PING_BULK: 96899.23 requests per second
SET: 86880.97 requests per second
GET: 90171.33 requests per second
INCR: 90744.10 requests per second
LPUSH: 69541.03 requests per second
LPOP: 72621.64 requests per second
SADD: 92592.59 requests per second
SPOP: 94073.38 requests per second
LPUSH (needed to benchmark LRANGE): 68681.32 requests per second
LRANGE_100 (first 100 elements): 44984.25 requests per second
LRANGE_300 (first 300 elements): 21137.18 requests per second
LRANGE_500 (first 450 elements): 15368.07 requests per second
LRANGE_600 (first 600 elements): 12072.92 requests per second
MSET (10 keys): 76452.60 requests per second
(3)测试set方法
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -n 100000 -q script load "redis.call('set','foo','bar')"
script load redis.call('set','foo','bar'): 62189.05 requests per second
(4)10 万随机 key 连续 SET 100 万次,可以使用r
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -t set -r 100000 -n 1000000
====== SET ======
1000000 requests completed in 14.23 seconds
50 parallel clients
3 bytes payload
keep alive: 1
98.61% <= 1 milliseconds
99.78% <= 2 milliseconds
99.79% <= 3 milliseconds
99.81% <= 4 milliseconds
99.83% <= 6 milliseconds
99.84% <= 7 milliseconds
99.85% <= 8 milliseconds
99.85% <= 9 milliseconds
99.86% <= 10 milliseconds
99.87% <= 11 milliseconds
99.88% <= 12 milliseconds
99.92% <= 13 milliseconds
99.93% <= 14 milliseconds
99.96% <= 15 milliseconds
99.98% <= 16 milliseconds
99.99% <= 18 milliseconds
100.00% <= 32 milliseconds
100.00% <= 32 milliseconds
70259.26 requests per second
每个客户端都是在一个请求完成之后才发送下一个请求 (benchmark 会模拟 50 个客户端除非使用 -c 指定特别的数量), 这意味着服务器几乎是按顺序读取每个客户端的命令。Also RTT is payed as well.
[root@192 6382]# redis-benchmark -h 127.0.0.1 -p 6380 -n 1000000 -t set,get -P 16 -q
SET: 559910.44 requests per second
GET: 590667.44 requests per second
Java代码连接redis
单点:
public static void jedisClient() { // Jedis Jedis jedis = new Jedis("192.168.1.118", 6379); // 通过redis赋值 jedis.set("s2", "222"); // 通过redis取值 String result = jedis.get("s2"); System.out.println(result); // 关闭jedis jedis.close(); } |
集群:
public static void jedisCluster() { // 创建jedisCluster Set<HostAndPort> nodes = new HashSet<>(); nodes.add(new HostAndPort("192.168.1.118", 6365)); nodes.add(new HostAndPort("192.168.1.118", 6366)); nodes.add(new HostAndPort("192.168.1.118", 6367)); nodes.add(new HostAndPort("192.168.1.118", 6368)); nodes.add(new HostAndPort("192.168.1.118", 6369)); nodes.add(new HostAndPort("192.168.1.118", 6370)); //nodes.add(new HostAndPort("192.168.1.118", 7007)); JedisCluster cluster = new JedisCluster(nodes); cluster.set("s4", "444"); String result = cluster.get("s4"); System.out.println(result); }
|
坑:集群连接不使用cluster.close(),否则会有错。
原因:我们使用的是redis3.0的集群,用jedis的JedisCluster.close()方法造成的集群连接关闭的情况。 jedisCluster内部使用了池化技术,每次使用完毕都会自动释放Jedis因此不需要关闭。如果调用close方法后再调用jedisCluster的api进行操作时就会出现如上错误。
Spring连接的使用
<!-- 连接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大连接数 --> <property name="maxTotal" value="30" /> <!-- 最大空闲连接数 --> <property name="maxIdle" value="10" /> <!-- 每次释放连接的最大数目 --> <property name="numTestsPerEvictionRun" value="1024" /> <!-- 释放连接的扫描间隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <!-- 连接最小空闲时间 --> <property name="minEvictableIdleTimeMillis" value="1800000" /> <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在获取连接的时候检查有效性, 默认false --> <property name="testOnBorrow" value="false" /> <!-- 在空闲时检查有效性, 默认false --> <property name="testWhileIdle" value="true" /> <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="false" /> </bean>
<!-- redis单机 通过连接池 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close"> <constructor-arg name="poolConfig" ref="jedisPoolConfig" /> <constructor-arg name="host" value="ip地址" /> <constructor-arg name="port" value="端口号" /> </bean>
|