Spring integrated redis cluster

目录

1, description

6, use

1, explain

The client used in this article is jedisCluster. It has encapsulated some common operations. Therefore, you only need to package according to your needs.

2, jar version

The version of the jar package used is as follows

jedis-2.9.0.jar (According to online feedback, only this version of the client supports access to the redis cluster with password)

spring-data-commons-1.8.6.RELEASE.jar

spring-data-redis-1.8.6.RELEASE.jar

Because the project is relatively old, so the spring main version is still used in 3.2.6.

jar Download address: Portal

3, clean up jar

After downloading and importing the above package, pay attention to delete the jar corresponding to the old version to avoid conflicts.

4, redis.properties file

Create a new folder under conf, name redis. Create a new redis.properties file, enter the following content:

redisCluster.maxIdle=10000
redisCluster.maxWaitMillis=50000
redisCluster.testOnBorrow=true

redisCluster.host1Ip=ip1
redisCluster.host2Ip=ip2
redisCluster.port1=port1
redisCluster.port2=port2
redisCluster.port3=port3

redisCluster.connectionTimeout=60000
redisCluster.soTimeout=30000
redisCluster.maxAttempts=3
redisCluster.password=pwd123

5, applicationContext.xml configuration

In the applicationContext.xml, enter the following information

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--Maximum number of idles -->
<property name="maxIdle" value="${redisCluster.maxIdle}" />
<!--Maximum connection waiting time -->
<property name="maxWaitMillis" value="${redisCluster.maxWaitMillis}" />
<!--Check if the connection is taken out of the pool, if the test fails, remove the connection from the pool and try to take the other -->
<property name="testOnBorrow" value="${redisCluster.testOnBorrow}" />
</bean>
    
    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
    <constructor-arg>
    <set>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg name="host" value="${redisCluster.host1Ip}"/>
<constructor-arg name="port" value="${redisCluster.port1}"/>
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg name="host" value="${redisCluster.host1Ip}"/>
<constructor-arg name="port" value="${redisCluster.port2}"/>
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg name="host" value="${redisCluster.host1Ip}"/>
<constructor-arg name="port" value="${redisCluster.port3}"/>
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg name="host" value="${redisCluster.host2Ip}"/>
<constructor-arg name="port" value="${redisCluster.port1}"/>
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redisCluster.host2Ip}"/>
<constructor-arg name="port" value="${redisCluster.port2}"/>
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg name="host" value="${redisCluster.host2Ip}"/>
<constructor-arg name="port" value="${redisCluster.port3}"/>
    </bean>
    </set>
    </constructor-arg>
    <constructor-arg name="connectionTimeout" value="${redisCluster.connectionTimeout}" />
<constructor-arg name="soTimeout" value="${redisCluster.soTimeout}" />
<constructor-arg name="maxAttempts" value="${redisCluster.maxAttempts}" />
<constructor-arg name="password" value="${redisCluster.password}" />
<constructor-arg name="poolConfig">
<ref bean="jedisPoolConfig" />
</constructor-arg>
</bean>

At this point, the configuration section ends.

注意: The properties file is loaded by configuration. Refer to the loading method of other properties files in applicationContext.xml and load them.

Since spring management is a singleton by default, so the same class can only generate one instance object. So, look at the loading class of the properties. Or you can use the set tag to bulk load the properties file

as shown below

<bean id="propertyConfigurer"
Class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:conf/jdbc.properties</value>
<value>classpath:conf/redis/redis.properties</value>
</list>
</property>
<!-- Add the following properties to avoid reading multiple properties file error -->
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

6, use

to automatically inject objects using

@Autowired protected JedisCluster jedisCluster;

.

7, write and read

Note that you want to serialize keys and values ​​. In this way, redis can store the object

to write the sample code:

public void save(final String key, final Object value, final long expirTime) {
Try {
String keySerialize = serialization (key);
String valueSerialize = serialization (value);

jedisCluster.lpush(keySerialize.getBytes(), valueSerialize.getBytes());
jedisCluster.expireAt(keySerialize.getBytes(),(expirTime+(new Date()).getTime()));

} catch (IOException e) {
e.printStackTrace();
Throw new RuntimeException("serialization failed!", e);
}
}

Read sample code is as follows

Public Object get(final String id) {
//1, serialized key
Try {
Byte[] key = serialization (id).getBytes();
Byte[] lindex = jedisCluster.lindex(key, 0L);
If(null != lindex){
String valueSerialize =new String(lindex,"UTF-8");
Object result = deserialization (valueSerialize);
Return result;
}else{
Return null;
}
} catch (Exception e1) {
e1.printStackTrace();
// TODO Auto-generated catch block
Return null;
}
}

jedisCluster.expireAt(keySerialize.getBytes(),(expirTime+(new Date()).getTime()));

的标识 The expiration time is identified by the unix timestamp. In other words, is at a specific point in time, the key value is invalid .

The author has delayed a day here. Ugh. . . .