维持婚姻springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败提⽰:
springboot提供了⼀套链接redis的api,也就是个jar包,⽤到的连接类叫做LettuceConnectionConfiguration,所以我们引⼊pom时是这样的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
如果你想使⽤其他链接redis的api,最常⽤的基本上就是Jedis的JedisConnectionFactory那么你就需要引⼊jedis,然后exclusions掉springboot提供做Lettuce: <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>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
1.配置实现SpringBoot集成Redis(只需两步)
1.最简单的配置就是在application.propertity中配置
#cache指定缓存类型
pe=REDIS
#data-redis
于明加图片
药学是干什么的
2.启动类开启缓存
@SpringBootApplication
@EnableCaching
public class EvaluationApplication {}
好了,配置好了!!
2.你有没有想过为啥SpringBoot集成Redis的配置那么简单?
原因:
1.SB之所以好⽤就是因为SB替我们做了很多以前我们需要⼿动在Spring中做的⼯作。
2.SB中默认是带了⼀个jar包:org.springframework.boot.autoconfigure这个包包含了⼤部分SpringBoot默认实现的功能,感兴趣的童鞋可以⾃⼰看下
3.我们可以在这个package org.springframework.boot.dis;下看到SB为我们默认实现的集成Redis的代码:
4.我们可以看到有个JedisConnectionConfiguration类
@Configuration //启动加载
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })
class JedisConnectionConfiguration extends RedisConnectionConfiguration {
private final RedisProperties properties;
private final List<JedisClientConfigurationBuilderCustomizer> builderCustomizers;
JedisConnectionConfiguration(RedisProperties properties,
ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration,
ObjectProvider<RedisClusterConfiguration> clusterConfiguration,
ObjectProvider<List<JedisClientConfigurationBuilderCustomizer>> builderCustomizers) {
super(properties, sentinelConfiguration, clusterConfiguration);
this.properties = properties;
this.builderCustomizers = builderCustomizers
.getIfAvailable(Collections::emptyList);
}
适合发朋友圈的奶茶图片
@Bean  //启动加载JedisConnectionFactory放⼊spring的容器中
@ConditionalOnMissingBean(RedisConnectionFactory.class)
public JedisConnectionFactory redisConnectionFactory() throws UnknownHostException {
return createJedisConnectionFactory();
}
  //我们可以看到SB为我们默认提供了三种加载⽅式
private JedisConnectionFactory createJedisConnectionFactory() {
JedisClientConfiguration clientConfiguration = getJedisClientConfiguration();
if (getSentinelConfig() != null) {//如果配置⽂件中配置了多个节点,则以多节点⽅式加载此⽅式上⾯配置部分⽆效,详情看源码
return new JedisConnectionFactory(getSentinelConfig(), clientConfiguration);
}
if (getClusterConfiguration() != null) {//如果配置了集则以集⽅式加载此⽅式上⾯配置部分⽆效,详情看源码
return new JedisConnectionFactory(getClusterConfiguration(),
clientConfiguration);
}
return new JedisConnectionFactory(getStandaloneConfig(), clientConfiguration);//以单节点的⽅式加载redis配置,上⾯配置⽣效
}
5.附上最后⼀种⽅式加载配置的源码
protected final RedisStandaloneConfiguration getStandaloneConfig() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
if (StringUtils.hasText(Url())) {
谭松韵的现任男朋友
ConnectionInfo connectionInfo = parseUrl(Url());
config.HostName());
config.Port());
config.setPassword(RedisPassword.Password()));
}
else {
config.setHostName(Host()); //这个propertity加载的就是上⾯我们贴出来的配置,源码可以看到上⾯图⽚中有个RedisPropertity类,配置⽂件中的配置就是根据这个类配置的            config.setPort(Port());
config.setPassword(RedisPassword.of(Password()));
}
config.setDatabase(Database());
return config;
}
3.那么问题来了,为什么我⼀直择库失败呢?
如果解决了你的问题,帮忙点个赞