Continuing from the previous article: Spring Boot project integration Swagger example document
The difference between StringRedisTemplate and RedisTemplate:
- The data of the two are not common ; that is to say, StringRedisTemplate can only manage data in StringRedisTemplate, and RedisTemplate can only manage data in RedisTemplate
The serialization method is different. RedisTemplate uses JdkSerializationRedisSerializer. When storing data, the data will be serialized into a byte array and then stored in the Redis database. StringRedisTemplate uses StringRedisSerializer.
StringRedisTemplate is mainly used to store strings, and the generic type of StringRedisSerializer specifies String. When saving the object, it will report an error: can not cast into String. Visibility is strong and easier to maintain. If it is all string storage, consider using StringRedisTemplate
RedisTemplate can be used to store objects, but it needs to implement the Serializable interface and store it in a binary array. The content is not readable. If you want to have readability, then Solution 1: Manually convert it into a json string and store it again. Retrieving the data requires deserialization solution 2. : Use other serialization methods
Understand Redis and use Redis in Spring Boot projects
Use RedisTemplate to operate Redis in Spring
Summary of common methods of RedisTemplate
RedisTemplate map collection instructions-opsForHash (3)
An exception in Redis: Cannot get Jedis connection
Redis password modification does not take effect
redis.conf parameter configuration
redisConfig类:
@Configuration public class RedisConfig { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<String, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(redisConnectionFactory); //template.setKeySerializer(jackson2JsonRedisSerializer); template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(jackson2JsonRedisSerializer); //template.setHashKeySerializer(jackson2JsonRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }
XML configuration version:
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnFactory"/> <property name="keySerializer"><!--处理KEY乱码问题--> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashKeySerializer"><!--处理KEY乱码问题--> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> </bean>
redis dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
Modification of redis.conf configuration file in linux environment:
Commands used:
编辑文件 vim redis.conf 关键字搜索 /关键字 键入 n 查找下一出 修改 键入 i 退出 键入 esc 保存 :wq 不保存 :q!
Configure the connection password:
requirepass root
Enable the daemon:
daemonize yes
Cancel protection mode:
protected-mode no
Comment out
bind 127.0.0.1 关闭只能本机访问的限制:
#bind 127.0.0.1
Firewall configuration Open port 6379:
查看6379防火墙状态 firewall-cmd --zone=public --query-port=6379/tcp 开放端口 firewall-cmd --zone=public --add-port=6379/tcp --permanent 重载 ==>执行此命令 开放端口才能生效 firewall-cmd --reload 再次查看 返回为yes 即表示配置成功 firewall-cmd --zone=public --query-port=6379/tcp
After modifying the configuration file, remember to restart the service :! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
./redis-server ./redis.conf
Redis configuration example:
There is a problem: the default client in springboot 2.x version is
lettuce
implemented by using, if you replace jedis in the configuration file withlettuce则项目启动报错
Springboot series articles integrated redis service (Lettuce & Jedis)
- Configure Redis connection information in application.properties
# Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=192.168.200.129 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password=root # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-idle=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-active=5 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=1 # 连接超时时间(毫秒) spring.redis.timeout=5000
Test redis storage objects
Print result: People(name=ww, address=ee)----------------------{name=jack, class=1, age=27}
// redisTemplate.setKeySerializer(new StringRedisSerializer()); 设置key的编码 //方式一: redisTemplate.opsForValue().set("p", new People("ww", "ee")); People p = (People) redisTemplate.opsForValue().get("p"); //方式二: Map<String, Object> testMap = new HashMap(); testMap.put("name", "jack"); testMap.put("age", 27); testMap.put("class", "1"); redisTemplate.opsForHash().putAll("hash1", testMap); // System.out.println(p + "----------------------" + redisTemplate.opsForHash().entries("hash1"));
About redis cluster:
Redis cluster tutorial