springboot 配置redis

记录一些基础但必须的配置---redis

springboot 中的redis 配置也是十分简单的,直接上代码:

首先,引入jar包文件:

        <!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<exclusions>
				<exclusion>
					<groupId>redis.clients</groupId>
					<artifactId>jedis</artifactId>
				</exclusion>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>

然后编写配置类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisPoolFactory {

    //自动注入redis配置属性文件
    @Autowired
    private RedisProperties properties;
    @Bean
    public JedisPool getJedisPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(properties.getJedis().getPool().getMaxIdle());
        config.setMaxTotal(properties.getJedis().getPool().getMaxActive());
        config.setMaxWaitMillis(properties.getJedis().getPool().getMaxWait().toMillis());
        JedisPool pool = new JedisPool(config,properties.getHost(),properties.getPort(),100,properties.getPassword(),properties.getDatabase(),null);
        return pool;
    }
}

查看源码可以看到  RedisProperties 会自动去获取配置文件中以spring.redis开头的数据

然后我们往配置文件中加入参数:

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=2
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=1234qwer
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

然后再application.java  类上加上扫码bean的注解

简单的写个方法测试一下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.edgecdj.utils.UUIDUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 测试redis
 * @author cdj
 * @date 2018年8月7日 上午10:04:19
 */
@Component
public class RedistestUtil {
	
	@Autowired
	JedisPool jedisPool;
	@Autowired
	StringRedisTemplate stringRedisTemplate;
	
	public String saveJedis (String val) {
		Jedis jedis = jedisPool.getResource();
		String uuid = UUIDUtils.getUUID();
		String setex = jedis.setex(uuid, 20000, val);
		return setex;
	}
	public String saveJedis2 (String val) {
		stringRedisTemplate.opsForValue().set(val, val);
		String xx = stringRedisTemplate.opsForValue().get(val);
		return xx;
	}
}

简单的检测一下就好了,配置还是蛮简单的,应用就很有学问了。

猜你喜欢

转载自blog.csdn.net/atmknight/article/details/81477802
今日推荐