您的位置:首页 > 汽车 > 新车 > 前端开发语言有哪些_2023年6月疫情最新消息_沈阳seo排名优化软件_如何优化网络连接

前端开发语言有哪些_2023年6月疫情最新消息_沈阳seo排名优化软件_如何优化网络连接

2025/6/13 16:44:59 来源:https://blog.csdn.net/weixin_74019079/article/details/146025615  浏览:    关键词:前端开发语言有哪些_2023年6月疫情最新消息_沈阳seo排名优化软件_如何优化网络连接
前端开发语言有哪些_2023年6月疫情最新消息_沈阳seo排名优化软件_如何优化网络连接

一、场景整合

(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*/@Beanpublic 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=8spring.data.redis.client-type=jedis
spring.data.redis.jedis.pool.enabled=true
spring.data.redis.jedis.pool.max-active=8

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com