1如果将全部数据都保存到一台redis中,如果该服务器损坏,则影响全部的服务
2使用单台redis内存设定一般不超过1G,但是有些业务数据量很大。如果不修改内存则无法存储。
改进方案:参用Redis分片技术实现。
优点:
1使用redis分片可以实现内存数据的动态扩容。
2使用分片,每台redis节点中尽可能保存1/n的数据量。防止数据的丢失。
3对于用户而言,整个redis的分片就是一个服务。
分片搭建
[root@localhost redis-3.2.8]# redis-cli shutdown
[root@localhost redis-3.2.8]# ls
00-RELEASENOTES COPYING INSTALL README.md runtest sentinel.conf utils
BUGS deps Makefile redis.conf runtest-cluster src
CONTRIBUTING dump.rdb MANIFESTO redis.conf~ runtest-sentinel tests
[root@localhost redis-3.2.8]# mkdir shards
[root@localhost redis-3.2.8]# ls
00-RELEASENOTES COPYING INSTALL README.md runtest sentinel.conf tests
BUGS deps Makefile redis.conf runtest-cluster shards utils
CONTRIBUTING dump.rdb MANIFESTO redis.conf~ runtest-sentinel src
复制配置文件
将redis.conf文件复制到shards中,并且复制3份名称为
redis-6379.conf/redis-6380.conf/redis-6381.conf
[root@localhost redis-3.2.8]# cp redis.conf shards/redis-6379.conf
[root@localhost redis-3.2.8]# cp redis.conf shards/redis-6380.conf
[root@localhost redis-3.2.8]# cp redis.conf shards/redis-6381.conf
[root@localhost redis-3.2.8]# cd shards
[root@localhost shards]# ls
redis-6379.conf redis-6380.conf # Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
修改端口号,启动redis
说明:分别修改6380和6381的配置文件为指定端口
[root@localhost shards]# vim redis-6380.conf
port 6380
[root@localhost shards]# vim redis-6381.conf
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6381
[root@localhost shards]# redis-server redis-6379.conf
[root@localhost shards]# redis-server redis-6380.conf
[root@localhost shards]# redis-server redis-6381.conf
[root@localhost shards]#
[root@localhost shards]# ps -ef |grep redis
root 4395 1 0 20:01 ? 00:00:02 gedit /usr/local/src/redis-3.2.8/redis.conf
root 4628 1 0 20:45 ? 00:00:00 redis-server *:6379
root 4632 1 0 20:45 ? 00:00:00 redis-server *:6380
root 4636 1 0 20:45 ? 00:00:00 redis-server *:6381
root 4641 3425 0 20:45 pts/1 00:00:00 grep redis
package test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class TestShardRedis {
@Test
public void testShard(){
//创建分片的对象1.poolConfig标识池的大小,2.shards redis分片的节点信息
JedisPoolConfig poolConfig=new JedisPoolConfig();
poolConfig.setMaxTotal(1000);
poolConfig.setTestOnBorrow(true);//获取链接是先检测,如果不行换一个
List<JedisShardInfo> shards=new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("192.168.161.135",6379));
shards.add(new JedisShardInfo("192.168.161.135",6380));
shards.add(new JedisShardInfo("192.168.161.135",6381));
ShardedJedisPool pool=new ShardedJedisPool(poolConfig, shards);
//获取redis连接
ShardedJedis jedis = pool.getResource();
jedis.set("shards", "保存分片的数据");
System.out.println(jedis.get("shards"));
//将连接还回池中
pool.returnResource(jedis);
pool.close();
}
}