Requirements:
If all data is saved to a redis instance, all services are affected if the server is damaged;
The memory size of a single apsaradb for redis instance should not exceed 1 GB. However, if the memory size is not modified, the data cannot be stored;
Solution:
Adopt redis sharding technology;
Advantages:
1. Using redis sharding can achieve dynamic expansion of memory data;
2. Use shards to store 1/N of data in each redis node as much as possible to prevent data loss;
3. for users, the entire redis sharding is a service; n servers serve users together as a whole server;
1. Build parts:
1.1 copy the configuration file: Create a New shards folder, copy the redis. conf file to the shards file, copy the file for 3 minutes, change the name, and change the port number;
[[Email protected] redis] # ls00-RELEASENOTES deps manifesto runtest-cluster srcbugs dump. RDB readme. MD runtest-sentinel testscontributing install redis. conf Sentinel. conf utilscopying makefile runtest shards [[email protected] redis] # cp redis. conf shards/redis-6379.conf [[email protected] redis] # cp redis. conf shards/redis-6380.conf [[email protected] redis] # cp redis. conf shards/redis-6381.conf [[email protected] redis] # ll
Result:
[[Email protected] redis] # cd shards/
[[Email protected] shards] # ll
Total 144
-RW-r --. 1 Root 46696 Nov 2 redis-6379.conf
-RW-r --. 1 Root 46696 Nov 2 redis-6380.conf
-RW-r --. 1 Root 46696 Nov 2 redis-6381.conf
1.2 check whether the startup is successful:
[[email protected]alhost shards]# redis-server redis-6379.conf [[email protected] shards]# redis-server redis-6380.conf [[email protected] shards]# redis-server redis-6381.conf [[email protected] shards]# ps -ef |grep redisroot 2845 1 0 22:58 ? 00:00:00 redis-server *:6379 root 2849 1 0 22:58 ? 00:00:00 redis-server *:6380 root 2853 1 0 22:59 ? 00:00:00 redis-server *:6381 root 2857 2585 0 22:59 pts/0 00:00:00 grep redis
1.3 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 () {/*** create a sharding object * 1. poolconfig indicates the size of the pool * 2. shards redis shard node information */jedispoolconfig poolconfig = new jedispoolconfig (); poolconfig. setmaxtotal (1000); poolconfig. settestonborrow (true); // check the connection first. If not, change the list to <jedisshardinfo> shards = new arraylist <> (); shards. add (New jedisshardinfo ("192.168.25.132", 6379); shards. add (New jedisshardinfo ("192.168.25.132", 6380); shards. add (New jedisshardinfo ("192.168.25.132", 6381); shardedjedispool pool = new shardedjedispool (poolconfig, shards); // obtain redis connection shardedjedis Jedis = pool. getresource (); Jedis. set ("Shards", "Save sharded data"); system. out. println (Jedis. get ("Shards"); // connect back to the pool. returnresource (Jedis );}}
Output: Save the partition data.
Redis sharding Technology