11、使用SpringBoot集成Redis

Spring-Boot整合

  • Spring-Boot操作数据:Spring-data jpa jdbc MongoDB redis
  • SpringData也是SpringBoot齐名的项目、
  • 说明:在SpringBoot2之后,原来使用的jedis被替换为lettuce
  • jedis:采用的直连,多个线程操作的话。是不安全的,如果想要避免不安全。使用jedis pool连接池!更像BIO模式

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

  • 源码分析:
  •     @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) {
            return new StringRedisTemplate(redisConnectionFactory);
        }
    
  • 整合测试

    • 引入依赖

              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-redis</artifactId>
              </dependency>
      
    • 写入配置

      spring:
        redis:
          host: 127.0.0.1
          port: 6379
      
      
    • 简单测试

          @Test
          void contextLoads() {
              //操作基本数据类型
      //        redisTemplate.opsForValue();
      //        redisTemplate.opsForList();
      //        redisTemplate.opsForHash();
              //获取数据库连接
      //        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
      //        connection.flushAll();
              //将一个值插入到数据库,并获取
              redisTemplate.opsForValue().set("key1","dxz");
              System.out.println(redisTemplate.opsForValue().get("key1"));
      
          }
      
  • 注意:在数据库中传入对象的时候
    1. 一定要进行对象序列化!!

    2. public class User implements Serializable { //实现serializable接口
          private String name;
          private int age;
      }
      
    3. 使用自己的redis配置,进行对象序列化

    4. @Configuration
      public class RedisConfig {
          @Bean
          @SuppressWarnings("all")
          public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
              //我们为了方便直接使用String和Object
              RedisTemplate<String, Object> template = new RedisTemplate();
              template.setConnectionFactory(redisConnectionFactory);
              //Json序列化
              Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
              ObjectMapper om = new ObjectMapper();
              om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
              om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
              jackson2JsonRedisSerializer.setObjectMapper(om);
              //String类型序列化
              StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
      
              //key采用String序列化方式
              template.setKeySerializer(stringRedisSerializer);
              //hash的key也采用String序列化
              template.setHashKeySerializer(stringRedisSerializer);
              //value采用jackson
              template.setValueSerializer(jackson2JsonRedisSerializer);
              //hash的value也采用jackson
              template.setHashValueSerializer(jackson2JsonRedisSerializer);
              template.afterPropertiesSet();
              return template;
          }
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Windows 10上使用Spring Boot集成Redis,你可以按照以下步骤进行操作: 步骤1:安装和配置Redis 1. 下载Redis:在Windows上,你可以从Redis官方网站(https://redis.io/download)下载最新的Redis版本。 2. 解压Redis:将下载的Redis压缩包解压到一个目录中,例如 `C:\redis`。 3. 配置Redis:在Redis目录中,找到并打开`redis.windows.conf`文件。根据你的需求,可以修改配置文件中的一些参数,比如端口号和密码。保存并关闭文件。 步骤2:启动Redis服务器 1. 打开命令提示符(CMD)或PowerShell,并导航到Redis目录。 2. 在命令提示符中输入 `redis-server.exe redis.windows.conf`,然后按回车键启动Redis服务器。 步骤3:在Spring Boot项目中集成Redis 1. 在你的Spring Boot项目的`pom.xml`文件中添加Redis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在`application.properties`(或`application.yml`)文件中配置Redis连接信息: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=your_password (如果设置了密码的话) ``` 3. 创建一个Java类作为Redis配置类,使用`@Configuration`注解标记,并使用`@EnableCaching`启用缓存支持: ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class RedisConfig { // 配置其他Redis相关的bean或缓存配置 } ``` 4. 在需要使用Redis的地方,使用`@Cacheable`、`@CachePut`、`@CacheEvict`等注解来实现缓存操作。 现在你已经成功集成Redis到你的Spring Boot项目中。你可以使用Redis来实现缓存、分布式锁等功能。记得启动Redis服务器后,确保你的Spring Boot应用程序能够连接到Redis服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺平崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值