redis学习七、SpringBoot整合redis

七、SpringBoot整合

  1. 导入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

springboot 2.x后 ,原来使用的 Jedis 被 lettuce 替换。

jedis:采用的直连,多个线程操作的话,是不安全的。如果要避免不安全,使用jedis pool连接池!更像BIO模式,阻塞

lettuce:采用netty,异步请求,实例可以在多个线程中共享,不存在线程不安全的情况!可以减少线程数据了,更像NIO模式

我们在学习SpringBoot自动配置的原理时,整合一个组件并进行配置一定会有一个自动配置类xxxAutoConfiguration,并且在spring.factories中也一定能找到这个类的完全限定名。Redis也不例外。

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\

那么就一定还存在一个RedisProperties类

@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })

之前我们说SpringBoot2.x后默认使用Lettuce来替换Jedis,现在我们就能来验证了。

先看Jedis:

在这里插入图片描述

@ConditionalOnClass注解中有两个类是默认不存在的,所以Jedis是无法生效的

然后再看Lettuce:

在这里插入图片描述

完美生效。

现在我们回到RedisAutoConfiguration

@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

只有两个简单的Bean

  • RedisTemplate
  • StringRedisTemplate

当看到xxTemplate时可以对比RestTemplat、SqlSessionTemplate,通过使用这些Template来间接操作组件。那么这俩也不会例外。分别用于操作Redis和Redis中的String数据类型。

在RedisTemplate上也有一个条件注解,说明我们是可以对其进行定制化的

说完这些,我们需要知道如何编写配置文件然后连接Redis,就需要阅读RedisProperties

在这里插入图片描述

这是一些基本的配置属性。

在这里插入图片描述

还有一些连接池相关的配置。注意使用时一定使用Lettuce的连接池。

在这里插入图片描述

  1. 编写配置文件

    # 配置redis
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    
  2. 使用RedisTemplate

    @SpringBootTest
    class SpringbootRedisApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
    
        @Test
        void contextLoads() {
    
            // redisTemplate 操作不同的数据类型,api和我们的指令是一样的
            // opsForValue 操作字符串 类似String
            // opsForList 操作List 类似List
            // opsForSet
            // opsForHash
            // opsForZSet
            // opsForGeo
            // opsForHyperLogLog
    
            // 除了基本的操作,我们常用的方法都是可以直接通过redisTemplate操作,比如事务,和基本的crud
    
            // 获取redis的连接对象
            // RedisConnection connection =
            //redisTemplate.getConnectionFactory().getConnection();
            // connection.flushDb();
            // connection.flushAll();
    
            redisTemplate.opsForValue().set("mykey","中文");
            System.out.println(redisTemplate.opsForValue().get("mykey"));
    
    
        }
    
    }
    
  3. 测试结果

    此时我们回到Redis查看数据时候,惊奇发现全是乱码,可是程序中可以正常输出:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v0jcVjro-1609320295496)(C:\Users\汪航\AppData\Roaming\Typora\typora-user-images\image-20201227205243485.png)]

这时候就关系到存储对象的序列化问题,在网络中传输的对象也是一样需要序列化,否者就全是乱码。

我们转到看那个默认的RedisTemplate内部什么样子:

private @Nullable RedisSerializer<?> defaultSerializer;
	private @Nullable ClassLoader classLoader;
	@SuppressWarnings("rawtypes") private @Nullable RedisSerializer keySerializer = null;
	@SuppressWarnings("rawtypes") private @Nullable RedisSerializer valueSerializer = null;
	@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashKeySerializer = null;
	@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashValueSerializer = null;

在最开始就能看到几个关于序列化的参数。

默认的序列化器是采用JDK序列化器

在这里插入图片描述

而默认的RedisTemplate中的所有序列化器都是使用这个序列化器:

if (defaultSerializer == null) {//默认的序列化器是采用JDK序列化器

			defaultSerializer = new JdkSerializationRedisSerializer(
					classLoader != null ? classLoader : this.getClass().getClassLoader());
		}
if (enableDefaultSerializer) {

			if (keySerializer == null) {
				keySerializer = defaultSerializer;
				defaultUsed = true;
			}
			if (valueSerializer == null) {
				valueSerializer = defaultSerializer;
				defaultUsed = true;
			}
			if (hashKeySerializer == null) {
				hashKeySerializer = defaultSerializer;
				defaultUsed = true;
			}
			if (hashValueSerializer == null) {
				hashValueSerializer = defaultSerializer;
				defaultUsed = true;
			}
		}

后续我们定制RedisTemplate就可以对其进行修改。

RedisSerializer提供了多种序列化方案:

  • 直接调用RedisSerializer的静态方法来返回序列化器,然后set

    在这里插入图片描述

  • 自己new 相应的实现类,然后set

    在这里插入图片描述

  1. 定制RedisTemplate的模板:

    我们创建一个Bean加入容器,就会触发RedisTemplate上的条件注解使默认的RedisTemplate失效。

    @Configuration
    public class RedisConfig {
    
       @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
            // 将template 泛型设置为 <String, Object>
            RedisTemplate<String, Object> template = new RedisTemplate();
            // 连接工厂,不必修改
            template.setConnectionFactory(redisConnectionFactory);
            /*
             * 序列化设置
             */
            // key、hash的key 采用 String序列化方式
            template.setKeySerializer(RedisSerializer.string());
            template.setHashKeySerializer(RedisSerializer.string());
            // value、hash的value 采用 Jackson 序列化方式
            template.setValueSerializer(RedisSerializer.json());
            template.setHashValueSerializer(RedisSerializer.json());
            template.afterPropertiesSet();
            
            return template;
        }
    }
    

    这样一来,只要实体类进行了序列化,我们存什么都不会有乱码的担忧了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值