个人网站和企业网站,山西建站便宜,一个主机 多个网站,温州做网站哪里好接下来我要实现的webscoket即时聊天中需要使用到redis,我先在项目中配置一下redis。 我这里再windows中做测试,关于redis的安装请移步《Redis(三)Windows系统安装redis》 一:在pom.xml中添加依赖
!-- springboot redis start --dependencygrou…接下来我要实现的webscoket即时聊天中需要使用到redis,我先在项目中配置一下redis。 我这里再windows中做测试,关于redis的安装请移步《Redis(三)Windows系统安装redis》 一:在pom.xml中添加依赖
!--springbootredisstart--
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-data-redis/artifactId
/dependency
!--springbootredisend-- 二:在yml配置文件中添加redis参数配置
spring:
#redis
redis:
#链接
host:127.0.0.1
#端口
port:6379
#使用的数据库索引,默认是0
database:0
#连接超时时间
timeout:1800000
#设置密码
#password:"123456"
lettuce:
pool:
#最大阻塞等待时间,负数表示没有限制
max-wait:-1
#连接池中的最大空闲连接
max-idle:5
#连接池中的最小空闲连接
min-idle:0
#连接池中最大连接数,负数表示没有限制
max-active:20 我本地的redis没有设置密码,因此密码哪一行的配置被我注释掉了。 三:在项目的config目录中添加RedisConfig.java文件
packagecom.springbootblog.config;importcom.fasterxml.jackson.annotation.JsonAutoDetect;
importcom.fasterxml.jackson.annotation.PropertyAccessor;
importcom.fasterxml.jackson.databind.ObjectMapper;
importorg.springframework.cache.annotation.CachingConfigurerSupport;
importorg.springframework.cache.annotation.EnableCaching;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.data.redis.connection.RedisConnectionFactory;
importorg.springframework.data.redis.core.*;
importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
importorg.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@EnableCaching//开启注解
publicclassRedisConfigextendsCachingConfigurerSupport{/**
*retemplate相关配置
*/
@Bean
publicRedisTemplateString,ObjectredisTemplate(RedisConnectionFactoryfactory){RedisTemplateString,Objecttemplate=newRedisTemplate();
//配置连接工厂
template.setConnectionFactory(factory);//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializerjacksonSeial=newJackson2JsonRedisSerializer(Object.class);ObjectMapperom=newObjectMapper();
//指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
//指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);//值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(newStringRedisSerializer());//设置hashkey和value序列化模式
template.setHashKeySerializer(newStringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();returntemplate;
}/**
*对hash类型的数据操作
*/
@Bean
publicHashOperationsString,String,ObjecthashOperations(RedisTemplateString,ObjectredisTemplate){
returnredisTemplate.opsForHash();
}/**
*对redis字符串类型数据操作
*/
@Bean
publicValueOperationsString,ObjectvalueOperations(RedisTemplateString,ObjectredisTemplate){
returnredisTemplate.opsForValue();
}/**
*对链表类型的数据操作
*/
@Bean
publicListOperationsString,ObjectlistOperations(RedisTemplateString,ObjectredisTemplate){
returnredisTemplate.opsForList();
}/**
*对无序集合类型的数据操作
*/
@Bean
publicSetOperationsString,ObjectsetOperations(RedisTemplateString,ObjectredisTemplate)