本地安装redis
1.安装redisbrew install redis2.终端执行命令:redis-server:启动 redis 服务器,默认端口 6379redis-cli:启动 redis 客户端启动成功:3.安装Redis Desktop Manager地址:Download free Redis Desktop Manager for macOS4.使用application.yml配置文件:sprin
·
1.安装redis
brew install redis
redis.config文件在/usr/local/etc目录下
2.终端执行命令:
- redis-server:启动 redis 服务器,默认端口 6379
- redis-cli:启动 redis 客户端
启动成功:
3.安装Redis Desktop Manager
地址:Download free Redis Desktop Manager for macOS
4.使用
application.yml配置文件:
spring
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
timeout: 3000
jedis:
pool:
max-idle: 500
min-idle: 50
max-active: 2000
max-wait: 1000
testOnBorrow: true
4.1第一种方法
pom文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
提示:当对对象进行存储时,记得要对对象进行序列化
第一种方法:实体类要实现序列化如下图所示
model:
@Data
public class AutoDO implements Serializable {
private static final long serialVersionUID = 2280759125960251969L;
private Integer id;
private String key;
private String value;
private Date createTime;
}
第二种:配置类
package com.example.logic.common.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching//开启注解
public class RedisConfig {
// @Bean
// public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
// CacheManager cacheManager = new RedisCacheManager(redisTemplate);
// return cacheManager;
// /*RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// // 多个缓存的名称,目前只定义了一个
// rcm.setCacheNames(Arrays.asList("thisredis"));
// //设置缓存默认过期时间(秒)
// rcm.setDefaultExpiration(600);
// return rcm;*/
// }
// 以下两种redisTemplate自由根据场景选择
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(factory);
return stringRedisTemplate;
}
}
test1:
package com.example.logic;
import com.example.logic.model.AutoDO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)//SpringBoot 2.X 默认使用Junit4
@SpringBootTest(classes = LogicApplication.class)
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;//自带的字符串模板类,用于存储字符串
@Autowired
private RedisTemplate redisTemplate;//自带的对象模板类,用于存储对象
@Test
public void test() throws Exception
{
AutoDO autoDO=new AutoDO();
autoDO.setKey("1222");
autoDO.setValue("2222");
// 保存字符串
//stringRedisTemplate.opsForValue().set("test", "test");
redisTemplate.opsForValue().set("3455",autoDO);
//Logger logger = LoggerFactory.getLogger(RedisTest.class);
String str = stringRedisTemplate.opsForValue().get("username");
System.out.println(str);
//logger.warn(str);
}
}
4.2第二种方法:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
package com.example.logic.common.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
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 JedisConfig {
private Logger logger = LoggerFactory.getLogger(JedisConfig.class);
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password:#{null}}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Bean
public JedisPool jedisPool(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMinIdle(minIdle);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password);
logger.info("JedisPoll连接成功:"+host+"\t"+port);
return jedisPool;
}
}
test2:
package com.example.logic;
import com.example.logic.model.AutoDO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@RunWith(SpringRunner.class)//SpringBoot 2.X 默认使用Junit4
@SpringBootTest(classes = LogicApplication.class)
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;//自带的字符串模板类,用于存储字符串
@Autowired
private RedisTemplate redisTemplate;//自带的对象模板类,用于存储对象
@Autowired
private JedisPool jedisPool;
@Test
public void test() throws Exception
{
Jedis jedis = jedisPool.getResource();
jedis.set("jedis","jedistest");
}
}
遇到的问题
Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?
解决方案:
在此处代码中不要设置password:JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password);
修改如下:
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
更多推荐
已为社区贡献7条内容
所有评论(0)