SpringBoot整合Redis
- 1、SpringBoot整合redis
- 1.1 pom.xml
- 1.2 application.yml
- 1.3 配置类RedisConfig,实现RedisTemplate序列化
- 1.4 代码测试
- 2、SpringBoot整合redis几个疑问?
- 2.1、Redis 连接池讲解
- 2.2、RedisTemplate和StringRedisTemplate
- 3、RedisTemplate
1、SpringBoot整合redis
1.1 pom.xml
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--redis相关依赖--><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></dependencies>
1.2 application.yml
server:port: 8080spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: rooturl: jdbc:mysql://127.0.0.1:3306/alarm?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULLredis:host: 127.0.0.1port: 6379password:database: 0timeout: 1000mslettuce:pool:max-active: 8 # 连接池最大连接数max-idle: 8 # 连接池最大空闲连接数min-idle: 0 # 连接池最小空闲连接数max-wait: -1ms # 连接池最大阻塞等待时间,负值表示没有限制mybatis:type-aliases-package: cn.yx.zg.pojomapperLocations: classpath:mappers/*.xml
1.3 配置类RedisConfig,实现RedisTemplate序列化
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/*** redisConfig** @author zhanggang* @since 2023/11/18 19:15*/
@Configuration
public class RedisConfig {@Beanpublic 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);//key hashmap序列化template.setHashKeySerializer (redisSerializer);//value hashmap序列化template.setHashValueSerializer(jackson2JsonRedisSerializer);return template;}
}
1.4 代码测试
经过上面三个步骤的配置,已经把Redis和SpringBoot整合好了,使用下面代码,既可以操作Redis了。import cn.yx.zg.SpringBootMybatisApplication;
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;/*** @author zhanggang* @since 2023/11/21 10:29*/
@SpringBootTest(classes = SpringBootMybatisApplication.class)
@RunWith(SpringRunner.class)
public class RedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void testRedis01() {redisTemplate.opsForValue().set("name", "zg");}
}
2、SpringBoot整合redis几个疑问?
2.1、Redis 连接池讲解
Redis 的客户端,常用的有两种:Jedis 和 Lettuce。Spring Boot 1.5.x 版本,默认的 Redis 客户端,实现上是直接连接 Redis Server,如果在多线程环境下是非线程安全的,这时候要使用连接池为每个 jedis 实例增加物理连接;
Lettuce:Spring Boot 2.x 版本后默认的 Redis 客户端,基于 Netty 实现,连接实例可以在多个线程间并发访问,一个连接实例不够的情况下也可以按需要增加连接实例。
上面代码是实现了Lettuce连接池,直接哪来用就行。
如下图,通过debug可以看出是使用了Lettuce连接池。
2.2、RedisTemplate和StringRedisTemplate
RedisTemplate实际就是类似java的jdbc,封装了对redis操作的一些方法。在1.3 新建配置类RedisConfig中,我们给RedisTemplate进行了序列化,为什么要序列化呢?如果不序列化,我们通过RedisTemplate存到redis的数据,都是是二进制存储的。你只用把我那个配置直接哪来用就可以了。
StringRedisTemplate一般只用来存储key和value都是String类型,当存入对象时,会报错 :can not cast into String。
RedisTemplate则是既可以存字符串又可以存对象,一般我们都使用RedisTemplate就够用了。