数据库
首页 > 数据库> > springboot连接docker的redis注意事项

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

结果:自动根据定义的类装配成对象,自己类型强转要注意

 

 

 

 

 

 

 

标签:om,springboot,org,redis,springframework,new,import,docker
来源: https://www.cnblogs.com/zhukaile/p/15918883.html