This is the 19th day of my participation in the Redis CurD Challenge. The curD operation and cluster setup of Redis are described in detail in the previous article

Curd operation and cluster construction of Redis are described in detail in the previous article. Now we start to integrate it into our actual projects. My project adopts the standard SSM framework. The SSM framework is not mentioned here, but directly integrated.

  • Start by introducing our JAR package in Maven administration
<! <dependency> <groupId>org.springframework.data</groupId> < artifactId > spring - data - redis < / artifactId > < version > 1.7.2. RELEASE < / version > < / dependency > <! Clients </groupId> <artifactId>jedis</artifactId> <version>2.9.0</version>  </dependency> <! Maven coordinates spring maven coordinates spring Maven coordinatesCopy the code
  • We can configure Redis in the Spring configuration file, but to keep Redis separate and easy to modify I have uninstalled it in another XML file, just import the Redis configuration file in the Spring configuration file.

  • All of the following records are operated in the Redis configuration file.

Redis connection pool configuration

  • Here you will only set some basic properties of the connection pool, such as the maximum number of connections, pre-connection attribute determination, and so on
<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxTotal}" />
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>
Copy the code
  • MaxIdle: Controls the maximum number of jedis instances in idle state in a pool.

  • Whether to advance the AlIDATE operation when borrowing a Jedis instance; If true, the resulting Jedis instances are all available

  • MaxWaitMillis: Indicates the maximum wait time when borrow a Jedis instance. If the wait time is exceeded, JedisConnectionException is thrown directly;

Redis cluster configuration

Here we are introducing the Redis service we started in the last article into the project. ClusterNodes is our Redis service one by one.

<! -- Redis clusterConfig --> <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="3"></property> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" Value ="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7000"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" Value ="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" Value ="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> </set> </property> </bean>Copy the code

Redis connection factory

We imported the above Redis service node and connection pool into the factory, there is a project to produce a usable Jedis to provide us with CURD operations for caching!!

<! - ReDis connection factory - > < bean id = "redis4CacheConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="clusterConfig" ref="redisClusterConfig" /> <property name="timeout" value="${redis.timeout}" /> <property name="poolConfig" ref="redisPoolConfig" /> </bean>Copy the code

Redis template

Provided with jedis to operate we are going to put a template inside for us to call! .

<! - store serialized - > < bean name = "stringRedisSerializer" class = "org. Springframework. Data. Redis. Serializer. StringRedisSerializer" / > <! Cluster Resis - use the template - > < bean id = "clusterRedisTemplate" class = "org. Springframework. Data. Redis. Core. RedisTemplate" > < property name="connectionFactory" ref="redis4CacheConnectionFactory" /> <property name="keySerializer" ref="stringRedisSerializer" /> <property name="hashKeySerializer" ref="stringRedisSerializer" /> <property name="valueSerializer" ref="stringRedisSerializer" /> <property name="hashValueSerializer" ref="stringRedisSerializer" /> </bean>Copy the code

Project called

Jedis is already configured in the configuration file, so we only need this template to CURD Redis. Here’s a simple example

clusterRedisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { byte[] keyb = key.getBytes(); byte[] valueb = toByteArray(value); If (connection.exists(keyb)) {// Delete the original data connection.del(keyb); } connection.set(keyb, valueb); return 1L; }});Copy the code

Source code exploration

RedisClusterConfiguration class we passed in the Redis service node, we set the property and then began to do the following code in the class

And then we pass in these nodes in the factory,

The factory gave me an AfterProperties method, which means a method to execute after these parameters are set.

Here we can see a creatPool method that creates a connection in the connection pool

Bug to solve

The deployment will be a problem, it is in the project, each initialization heavy in the pool when they choose a Redis service connections are available, and when the Redis service outage our project will continue to connect the Redis service, we have to start new projects, projects will anew from the connection pool to choose new Redis service.

  • I refactor the connection pool every time I use Redis to ensure that I find a valid Redis service every time I go to the connection pool.
  • The solution is to inject RedisNode into the project, each time executing the creatPool method we saw last time
Redis template into * * * / / @ Resource private RedisClusterConfiguration redisClusterConfig; private JedisConnectionFactory redis4CacheConnectionFactory; @Resource private RedisTemplate<String, Object> clusterRedisTemplate; / / refactoring connection pool private void init () {redis4CacheConnectionFactory = new JedisConnectionFactory (redisClusterConfig); redis4CacheConnectionFactory.afterPropertiesSet(); clusterRedisTemplate.setConnectionFactory(redis4CacheConnectionFactory); }Copy the code

There is no solution to this problem, but I initially want to use Redis Sentinel to detect the Redis services in the Redis cluster. When Redis goes down, the sentinel API is used to inform the project to rebuild the connection pool and reconnect to the new Redis service