springboot连接docker的redis注意事项
2022/2/21 19:28:22
本文主要是介绍springboot连接docker的redis注意事项,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、虚拟机配置
redis配置保护模式关闭:no
配置bind:0.0.0.0 , 代表ip可以访问,如果是默认的127.0.0.1,代表仅自己本机
注意:将daemonize yes注释起来或者 daemonize no设置,因为该配置和docker run中-d参数冲突,会导致容器一直启动失败
2、springboot配置
基本属于默认配置
redis: #服务器地址 host: 192.168.10.100 #redis的ip #端口 port: 6379 #数据库 database: 0 #超时时间 timeout: 1800000 #密码 password: lettuce: pool: #最大连接数,默认8 max-active: 8 #最大连接阻塞等待时间,默认-1 max-wait: 10000ms #最大空闲连接,默认8 max-idle: 200 #最小空闲连接,默认0 min-idle: 0
项目添加配置类:
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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.data.redis.cache.RedisCacheConfiguration; 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.script.DefaultRedisScript; import org.springframework.data.redis.serializer.*; import java.time.Duration; /** * Redis配置类 * <p> * 乐字节:专注线上IT培训 * 答疑老师微信:lezijie * * @author zhoubin * @since 1.0.0 */ @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解决查询缓存转换异常的问题 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解决乱码的问题),过期时间600秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } // @Bean // public DefaultRedisScript<Boolean> script() { // DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(); // //lock.lua脚本位置和application.yml同级目录 // redisScript.setLocation(new ClassPathResource("lock.lua")); // redisScript.setResultType(Boolean.class); // return redisScript; // } // // @Bean // public DefaultRedisScript<Long> script() { // DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(); // //放在和application.yml 同层目录下 // redisScript.setLocation(new ClassPathResource("stock.lua")); // redisScript.setResultType(Long.class); // return redisScript; // } }
使用
redisTemplate.opsForValue().set("name","lucy"); //从redis获取值 String name = (String)redisTemplate.opsForValue().get("name");
注意:获取数据时的类型,因为配置类的相关配置,返回的类型会自动的装换如:bank1的数据类型是json
结果:自动根据定义的类装配成对象,自己类型强转要注意
这篇关于springboot连接docker的redis注意事项的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南