当前位置: 首页 > article >正文

SpringBoot3—场景整合:NoSQL

一、场景整合

(1)依赖导入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

(2)配置

spring.data.redis.host=192.168.200.100
spring.data.redis.password=Lfy123!@!

(3)测试

@Autowired
StringRedisTemplate redisTemplate;

@Test
void redisTest(){
    redisTemplate.opsForValue().set("a","1234");
    Assertions.assertEquals("1234",redisTemplate.opsForValue().get("a"));
}

二、自动配置原理

  1. META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports中导入了RedisAutoConfigurationRedisReactiveAutoConfigurationRedisRepositoriesAutoConfiguration。所有属性绑定在RedisProperties中
  2. RedisReactiveAutoConfiguration属于响应式编程,不用管。RedisRepositoriesAutoConfiguration属于 JPA 操作,也不用管
  3. RedisAutoConfiguration 配置了以下组件
    1. LettuceConnectionConfiguration: 给容器中注入了连接工厂LettuceConnectionFactory,和操作 redis 的客户端DefaultClientResources
    2. RedisTemplate<Object, Object>: 可给 redis 中存储任意对象,会使用 jdk 默认序列化方式
    3. StringRedisTemplate: 给 redis 中存储字符串,如果要存对象,需要开发人员自己进行序列化。key-value都是字符串进行操作

三、定制化

(1)序列化机制

@Configuration
public class AppRedisConfiguration {


    /**
     * 允许Object类型的key-value,都可以被转为json进行存储。
     * @param redisConnectionFactory 自动配置好了连接工厂
     * @return
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //把对象转为json字符串的序列化工具
        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}
  1. 因为在RedisAutoConfiguration中有这段代码,也就是说只要容器中有名为redisTemplatebean,那么这个本来要自动提供给我们的redisTemplate就不会生效了
  2.  关于@Bean注解

(2)redis客户端

  1. RedisTemplateStringRedisTemplate: 操作redis的工具类
  2. 要从redis的连接工厂获取连接才能操作redis
  3. Redis客户端
    1. Lettuce: 默认
    2. Jedis:可以使用以下切换
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-redis</artifactId>
                  <exclusions>
                      <exclusion>
                          <groupId>io.lettuce</groupId>
                          <artifactId>lettuce-core</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
      
      <!--        切换 jedis 作为操作redis的底层客户端-->
              <dependency>
                  <groupId>redis.clients</groupId>
                  <artifactId>jedis</artifactId>
              </dependency>

(3)配置参考

spring.data.redis.host=8.130.74.183
spring.data.redis.port=6379
#spring.data.redis.client-type=lettuce

#设置lettuce的底层参数
#spring.data.redis.lettuce.pool.enabled=true
#spring.data.redis.lettuce.pool.max-active=8

spring.data.redis.client-type=jedis
spring.data.redis.jedis.pool.enabled=true
spring.data.redis.jedis.pool.max-active=8

看这两张图就能理解,为什么这么配置就能改变redis的客户端: 


http://www.kler.cn/a/573742.html

相关文章:

  • 开源模型应用落地-LangGraph101-探索 LangGraph人机交互-添加断点(一)
  • 准确---Liunx查看出口ip的命令
  • Leetcode 57: 插入区间
  • 快速熟悉JavaScript
  • Linux的一些配置(网络建设与运维)
  • 【软考-架构】9.2、摘要-签名-PKI-访问控制-DOS-欺骗技术
  • 计算机网络(1) 网络通信基础,协议介绍,通信框架
  • 基于微信小程序的小区服务管理系统+论文源码调试讲解
  • 基于SSM+MySQL的二手书籍交易系统
  • wheel_legged_genesis 开源项目复现与问题记录
  • llama.cpp: GGUF格式及模型量化参数介绍
  • 突破传统:用Polars解锁ICU医疗数据分析新范式
  • C语言【指针篇】(四)
  • vueDraggable插件拖拽过程中节点文本不允许选中方案
  • 最新技术趋势与应用解析:未来科技走向
  • 【每日学点HarmonyOS Next知识】动图循环播放、监听tab切换、富文本上下滚动、tab默认居中、a标签唤起拨号
  • AIGC(生成式AI)试用 26 -- 跟着清华教程学习 - 个人理解
  • TCP、UDP、WebSocket 和 HTTP 教程
  • 玩转大模型——Trae AI IDE国内版使用教程
  • 学网络安全报班可靠吗?