I use the redis3.0 version. The 3.0 version mainly increased the Redis cluster function.
Prerequisites for Installation: Install Gcc:yum install gcc-c++
1, download the Redis source package.
2, the source package uploaded to the Linux server
3, extract source package TAR-ZXVF redis-3.0.0.tar.gz
4, make
5, make install
[Root@shuang redis-3.0.0]# make install Prefix=/usr/local/redis
1.1 Start Redis
1. Front Start mode
/usr/local/redis/bin/redis-server
Default is front-end boot mode, port is 6379
2, back-end boot
1 copy redis.conf to Redis installation directory from the Redis source directory.
2) Modify the configuration file redis.conf
3) [Root@shuang bin]#./redis-server redis.conf
common commands for 1.2 Redis
127.0.0.1:6379> set a 10
Ok
127.0.0.1:6379> get a
"10" and so on.
1.3 Redis Common data types
String
Hash
List
Set
SortedSet
1.4Redis Cluster Setup
Redis-cluster maps all the physical nodes to [0-16383]slot, cluster is responsible for maintaining Node<->slot<->value
There are 16,384 hash slots built into the Redis cluster, and when a key-value is required to be placed in the Redis cluster, Redis first uses the CRC16 algorithm to calculate a result for the key, and then the result is given to 16384 for the remainder, so that each key will correspond to a number A hash slot between 0-16383, Redis maps a hash slot to a different node based on roughly equal number of nodes, for example:
Key:a
Calculates the hash value of a, for example, a value of 100,100 this slot is on the Server1, so a should be placed in the Server1.
Key:hello
Hash value: 10032, this slot is on the server2. Hell can exist server2. 1.5 redis-cluster vote: Fault tolerance
(1) The voting process is the participation of all master in the cluster, if more than half of the master node communicates with the master node (cluster-node-timeout), the current master node is considered dead.
(2): When is the entire cluster unavailable (Cluster_state:fail)?
A: If the cluster any master hangs, and the current master is not slave. The cluster enters the fail state, and it can be understood that the slot mapping of the cluster [0-16383] enters the fail state when it is not completed.
Ps:redis-3.0.0.rc1 joins the Cluster-require-full-coverage parameter, closes by default, and fails to open the Cluster compatibility section.
B: If the cluster more than half of master hangs out, whether or not a slave cluster enters the fail state.
PS: When the cluster is not available, all operations to the cluster are not available, received ((error) Clusterdown the cluster is down) error
Two Redis cluster
1.1 My cluster structure
There are three nodes in the cluster, and each node has one main standby. Requires 6 virtual machines.
Build a pseudo distributed cluster, using 6 Redis instances to simulate.
1.2 The environment needed to build the cluster
Building a cluster requires the use of the official Ruby script.
A ruby environment needs to be installed.
Install Ruby
Yum Install Ruby
Yum Install RubyGems
Redis Cluster Management tool REDIS-TRIB.RB
[Root@shuang ~]# CD redis-3.0.0
[Root@shuang redis-3.0.0]# cd src
[Root@shuang src]# ll *.RB
-rwxrwxr-x. 1 root 48141 Apr 1 07:01 redis-trib.rb
[Root@shuang src]#
The Ruby package required by the script:
Need to upload to Linux services.
To install Ruby packages:
Gem Install Redis-3.0.0.gem
[Root@bogon ~]# gem install Redis-3.0.0.gem
Successfully installed redis-3.0.0
1 Gem installed
Installing RI documentation for redis-3.0.0 ...
Installing RDOC documentation for redis-3.0.0 ... Construction of 1.3 cluster
Step One: Create 6 Redis instances, the port number from the 7001~7006
Step Two: Modify the Redis configuration file
1, modify the port number
2, open the cluster-enable before the note.
Step three: Copy the Ruby script that created the cluster to the Redis-cluster directory.
Step Fourth: Start 6 Redis instances
Fifth step: Create the cluster.
./redis-trib.rb Create--replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006
[Root@shuang redis-cluster]#./REDIS-TRIB.RB Create--replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006
>>> Creating cluster
Connecting to Node 192.168.25.153:7001:ok
Connecting to Node 192.168.25.153:7002:ok
Connecting to Node 192.168.25.153:7003:ok
Connecting to Node 192.168.25.153:7004:ok
Connecting to Node 192.168.25.153:7005:ok
Connecting to Node 192.168.25.153:7006:ok
>>> performing hash slots allocation on 6 nodes ...
Using 3 Masters:
192.168.25.153:7001
192.168.25.153:7002
192.168.25.153:7003
Adding replica 192.168.25.153:7004 to 192.168.25.153:7001
Adding replica 192.168.25.153:7005 to 192.168.25.153:7002
Adding replica 192.168.25.153:7006 to 192.168.25.153:7003
m:5a8523db7e12ca600dc82901ced06741b3010076 192.168.25.153:7001
slots:0-5460 (5461 slots) Master
M:bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.25.153:7002
slots:5461-10922 (5462 slots) Master
M:c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.25.153:7003
slots:10923-16383 (5461 slots) Master
s:2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.25.153:7004
Replicates 5a8523db7e12ca600dc82901ced06741b3010076
S:14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.25.153:7005
Replicates bf6f0929044db485dea9b565bb51e0c917d20a53
s:3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.25.153:7006
Replicates C5E334DC4A53F655CB98FA3C3BDEF8A808A693CA
Can I Set the above configuration? (Type ' yes ' to accept): Yes
>>> Nodes Configuration Updated
>>> Assign a different config epoch to each node
>>> sending CLUSTER MEET messages to join the CLUSTER
Waiting for the "cluster" to join ...
>>> performing Cluster Check (using node 192.168.25.153:7001)
m:5a8523db7e12ca600dc82901ced06741b3010076 192.168.25.153:7001
slots:0-5460 (5461 slots) Master
M:bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.25.153:7002
slots:5461-10922 (5462 slots) Master
M:c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.25.153:7003
slots:10923-16383 (5461 slots) Master
m:2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.25.153:7004
Slots: (0 slots) Master
Replicates 5a8523db7e12ca600dc82901ced06741b3010076
M:14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.25.153:7005
Slots: (0 slots) Master
Replicates bf6f0929044db485dea9b565bb51e0c917d20a53
m:3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.25.153:7006
Slots: (0 slots) Master
Replicates C5E334DC4A53F655CB98FA3C3BDEF8A808A693CA
[OK] All nodes agree about slots configuration.
>>> Check for open Slots ...
>>> Check Slots Coverage ...
[OK] All 16384 slots covered.
[Root@shuang redis-cluster]#
1.4 Test cluster
[Root@shuang redis-cluster]# redis01/redis-cli-h 192.168.25.153-p 7002- c
[Root@shuang redis-cluster]# redis01/redis-cli-h 192.168.25.153-p 7002
192.168.25.153:7002> set a 100
(Error) Moved 15495 192.168.25.153:7003
192.168.25.153:7002>
[Root@shuang redis-cluster]# redis01/redis-cli-h 192.168.25.153-p 7002-c
192.168.25.153:7002> set a 100
-> redirected to slot [15495] located at 192.168.25.153:7003
Ok
192.168.25.153:7003>
1.5 off Redis
Redis01/redis-cli-p 7001 shutdown
iii. Java use Redis--jedis client
The Jedis jar package needs to be added to the project, and if Maven needs to add the coordinates of the jar package.
Single-Version test class
public class Jedistest {
@Test public
void Testjedissingle () {
//Create a Jedis object.
Jedis Jedis = new Jedis ("192.168.25.153", 6379);
The method that invokes the Jedis object, with the method name consistent with the Redis command.
jedis.set ("Key1", "Jedis test");
String string = Jedis.get ("Key1");
System.out.println (string);
Close Jedis.
jedis.close ();
}
/**
* Using connection pooling
//@Test public
void Testjedispool () {
//creating Jedis Connection pool
jedispool pools = new Jedispool ("192.168.25.153", 6379);
Obtain the Jedis object from the connection pool
Jedis Jedis = Pool.getresource ();
String string = Jedis.get ("Key1");
System.out.println (string);
Closes the Jedis object
jedis.close ();
Pool.close ();
}
Cluster beta test class
@Test public
void Testjediscluster () {
hashset
1.1 Jedis integrated Spring Single-version configuration
<!--connection Pool configuration--> <bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <!--Maximum connection number
; <property name= "Maxtotal" value=/> <!--maximum number of idle connections--> <property name= "Maxidle" value= "ten"/> t;! --Maximum number of connections per release--> <property name= "Numtestsperevictionrun" value= "1024"/> <!--free connection scan interval (milliseconds)--> ;p roperty name= "Timebetweenevictionrunsmillis" value= "30000"/> <!--connection Minimum idle time--> <property name= "minEvict Ableidletimemillis "value=" 1800000/> <!--How long the connection is idle and released after free time > This value and free connections > maximum idle connections--> <property N Ame= "Softminevictableidletimemillis" value= "10000"/> <!--get the maximum number of milliseconds to wait for a connection, less than 0: block indeterminate time, Default-1--> <property Name= "Maxwaitmillis" value= "1500"/> <!--check validity when getting connections, default false--> <property "Name=" Testonborrow " True '/> <!--check validity at idle, default false--> <property name= ' testwhileidle ' value= ' true '/> <!--blocked when connection is exhausted , False reported abnormally, Ture blocked untilTimeout, default true--> <property name= "blockwhenexhausted" value= "false"/> </bean> <!--Jedis Client standalone version--> <bean id= "redisclient" class= "Redis.clients.jedis.JedisPool" > <constructor-arg name= "host" value= " 192.168.25.153 "></constructor-arg> <constructor-arg name=" Port "value=" 6379 "></constructor-arg > <constructor-arg name= "poolconfig" ref= "Jedispoolconfig" ></constructor-arg> </bean>
/**
* Single version Test
* <p>Title:testSpringJedisSingle</p>
* <p>description: </p>/
@Test public
void Testspringjedissingle () {
ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("Classpath:spring/applicationcontext-*.xml");
Jedispool pool = (jedispool) applicationcontext.getbean ("Redisclient");
Jedis Jedis = Pool.getresource ();
String string = Jedis.get ("Key1");
System.out.println (string);
Jedis.close ();
Pool.close ();
}
Cluster configuration
<bean id= "redisclient" class= "Redis.clients.jedis.JedisCluster" > <constructor-arg name= "Nodes" > <set > <bean class= "redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.25.153" & gt;</constructor-arg> <constructor-arg name= "Port" value= "7001" ></constructor-arg> </bean>
; <bean class= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.25.153" >
</constructor-arg> <constructor-arg name= "Port" value= "7002" ></constructor-arg> </bean> <bean class= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.25.153" ><
;/constructor-arg> <constructor-arg name= "Port" value= "7003" ></constructor-arg> </bean> <bean class= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.25.153" > </constructor-arg> <constructor-arg name= "Port" value= "7004" ></constructor-arg> </bean> <bean class= "Redis.clients" . Jedis. Hostandport "> <constructor-arg name=" host "value=" 192.168.25.153 "></constructor-arg> <construc Tor-arg name= "Port" value= "7005" ></constructor-arg> </bean> <bean class= "Redis.clients.jedis.Hos" Tandport "> <constructor-arg name=" host "value=" 192.168.25.153 "></constructor-arg> <constructor -arg name= "Port" value= "7006" ></constructor-arg> </bean> </set> </constructor-arg> & Lt;constructor-arg name= "Poolconfig" ref= "Jedispoolconfig" ></constructor-arg> </bean>
@Test public
void Testspringjediscluster () {
ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("Classpath:spring/applicationcontext-*.xml");
Jediscluster jediscluster = (jediscluster) Applicationcontext.getbean ("Redisclient");
String string = Jediscluster.get ("Key1");
System.out.println (string);
Jediscluster.close ();
}
The Jedis class uses Redis encapsulation (stand-alone version)
public class Jedisclientsingle implements jedisclient{@Autowired private Jedispool jedispool;
@Override public string (string key) {Jedis Jedis = Jedispool.getresource ();
String string = Jedis.get (key);
Jedis.close ();
return string;
@Override public string Set (string key, String value) {Jedis Jedis = Jedispool.getresource ();
String string = Jedis.set (key, value);
Jedis.close ();
return string;
@Override public string Hget (string hkey, String key) {Jedis Jedis = Jedispool.getresource ();
String string = Jedis.hget (hkey, key);
Jedis.close ();
return string;
@Override Public Long Hset (string hkey, String key, String value) {Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.hset (hkey, key, value);
Jedis.close ();
return result;
@Override Public long incr (String key) {Jedis Jedis = Jedispool.getresource ();
Long result = JEDIS.INCR (key);
Jedis.close ();
return result; @Override Public long expire (string key, int second) {Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.expire (key, second);
Jedis.close ();
return result;
@Override public long TTL (String key) {Jedis Jedis = Jedispool.getresource ();
Long result = Jedis.ttl (key);
Jedis.close ();
return result; }
}
Jedis class uses Redis Encapsulation (Cluster edition)
public class Jedisclientcluster implements Jedisclient {
@Autowired
private jediscluster jediscluster;
@Override public
String ' get (string key) {return
jediscluster.get (key);
}
@Override public
string Set (String key, String value) {return
Jediscluster.set (key, value);
}
@Override public
string Hget (String hkey, string key) {return
jediscluster.hget (hkey, key);
}
@Override public
Long Hset (string hkey, String key, String value) {return
jediscluster.hset (hkey, key, value);
}
@Override public
Long incr (String key) {return
jediscluster.incr (key);
}
@Override public
long expire (String key, int second) {return
Jediscluster.expire (key, second);
}
@Override Public
long TTL (String key) {return
Jediscluster.ttl (key);
}
Adding the cache to the business logic example, the idea of using slices here is simpler
@Override public
list<tbcontent> getcontentlist (Long contentcid) {
//fetch content from cache
try {
String result = Jedisclient.hget (Index_content_redis_key, Contentcid + "");
if (! Stringutils.isblank (Result)) {
//convert string to List
list<tbcontent> resultlist = jsonutils.jsontolist ( result, tbcontent.class);
Return resultlist
}
} catch (Exception e) {
e.printstacktrace ();
}
Query the content list based on the content category ID
tbcontentexample example = new Tbcontentexample ();
Criteria criteria = Example.createcriteria ();
Criteria.andcategoryidequalto (contentcid);
Execute Query
list<tbcontent> List = contentmapper.selectbyexample (example);
Add content to the cache
try {
//Convert list to string
cachestring = Jsonutils.objecttojson (list);
Jedisclient.hset (Index_content_redis_key, Contentcid + "", cachestring);
} catch (Exception e) {
e.printstacktrace ();
}
return list;
}