SpringBoot通过RedisTemplate使用redis(无须手动序列化)

  • 2018-05-23
  • 791
  • 0

导入redis依赖
[xml]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
[/xml]
Jackson依赖
[xml]
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.2</version>
</dependency>
[/xml]

配置application.yml配置文件
[xml]
# REDIS (RedisProperties)
spring:
redis:
database: 0 # Redis数据库索引(默认为0)
host: localhost # Redis服务器地址
port: 6379 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
timeout: 0 # 连接超时时间(毫秒)
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
[/xml]


使redisTemplate无须手动序列化的类,并用jackson替换默认的序列化工具
[java]
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* 功能描述: redis配置
* Date:2018-05-23 > Author:Chaiyibo
**/
@Configuration
public class RedisConfig {

/**
* redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
[/java]
开始使用
[java]
import com.example.entity.User;
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.test.context.junit4.SpringRunner;

/**
* Date:2018-05-23 > Author:Chaiyibo
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {

@Autowired
private RedisTemplate<Object, Object> template;

@Test
public void contextLoads() {
User user = new User(1,"象拔蚌");
template.opsForValue().set(user.getId()+"",user);
//原本opsForValue()是只能操作字符串的.现在就可以操作对象了
User result = (User) template.opsForValue().get(user.getId()+"");
System.out.println(result.toString());
}
// @RabbitListener(queues = "${avclog.queue.name.demand}")
// public void getDemandMessage(String message) {
// try {
// UserActionLog log = JSONObject.parseObject(message, UserActionLog.class);
// redisTemplate.opsForList().leftPush("logstash-demand", log);
// } catch (Exception e) {
// e.printStackTrace();
// log.info("rabbitmq-demand消费消息失败,消息为:{}", message);
// }
// }
}
[/java]

评论

还没有任何评论,你来说两句吧