Redis installation deployment application [part]

Posted by Bob_PHP_Builder on Sat, 09 May 2020 19:12:10 +0200

Linux Installation of Redis

1. Preparing the installation package

2. Unzip the installation package
tar zxvf redis-5.0.5.tar.gz

3. Enter the folder you just unzipped and enter the make command

4. After waiting for the above process to complete, enter the make install command

5. Run. / Redis server under the redis/src / folder to start Redis

6. Set Redis background operation

7. Restart after modification.

Spring boot integrates Redis

1. Add Redis related dependency
We are still based on the previous spring boot project

                 <!-- Redis Related dependence -->
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-redis</artifactId>
                </dependency>

2. Add redis related configuration to application.properties
See also Configuration files of Spring Boot, Duboo, Zookeeper and redis [how to use]

# Redis database index (0 by default)
spring.redis.database=0
# Redis server address
spring.redis.host=192.168.0.24
# Redis server connection port
spring.redis.port=6379
# Redis server connection password (empty by default)
spring.redis.password=
# Maximum number of connections in the connection pool (use a negative value to indicate no limit)
spring.redis.pool.max-active=200
# Connection pool maximum block wait time (use a negative value to indicate no limit)
spring.redis.pool.max-wait=-1
# Maximum free connections in connection pool
spring.redis.pool.max-idle=10
# Minimum free connections in connection pool
spring.redis.pool.min-idle=0
# Connection timeout (MS)
spring.redis.timeout=1000 

3. redis configuration class

    package com.example.demo;

    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.support.collections.RedisProperties;
    import java.net.UnknownHostException;
    /**FrozenConnection RedAntctionConfiguration and RedAntctionConfiguration are two clients that I created casually
    *
    /
    @Configuration
    @ConditionalOnClass(RedisOperations.class)
    @EnableConfigurationProperties(RedisProperties.class)
    @Import({FrozenConnection.class, RedAntctionConfiguration.class})
    public class RedisAutoConfiguration {
       /**@ConditionalOnMissingBean
 * If there is a RedisTemplate object in the Spring container,
 * This auto configured RedisTemplate will not be instantiated.
 * So we can write a configuration class and configure RedisTemplate directly.
 */
            @Bean
            @ConditionalOnMissingBean(name = "redisTemplate")
            public RedisTemplate<Object, Object> redisTemplate(
                            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
                    RedisTemplate<Object, Object> template = new RedisTemplate<>();
                    template.setConnectionFactory(redisConnectionFactory);
                    return template;
            }

            @Bean
            @ConditionalOnMissingBean
            public StringRedisTemplate stringRedisTemplate(
                            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
                    StringRedisTemplate template = new StringRedisTemplate();
                    template.setConnectionFactory(redisConnectionFactory);
                    return template;
            }

    }

package com.example.demo;

import io.lettuce.core.RedisClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;

/**
 * SpringBoot The default is lettuce
 */
@Configuration
@ConditionalOnClass(RedisClient.class)
public class FrozenConnection extends RedisAutoConfiguration{

}

package com.example.demo;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnection;

@Configuration
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, RedisProperties.Jedis.class })
public class RedAntctionConfiguration extends RedisAutoConfiguration{

}

Topics: Big Data Redis Spring Database Jedis