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指定缓存类型
spring.cache.type=REDIS

#data-redis
spring.redis.database=15
spring.redis.password=
spring.redis.host=192.168.**.**
spring.redis.port=6379
spring.redis.timeout=2000
spring.redis.jedis.pool.max-active=8 
spring.redis.jedis.pool.max-idle=8 
spring.redis.jedis.pool.max-wait=-1 
spring.redis.jedis.pool.min-idle=0 

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.autoconfigure.data.redis;下看到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(this.properties.getUrl())) {
            ConnectionInfo connectionInfo = parseUrl(this.properties.getUrl());
            config.setHostName(connectionInfo.getHostName());
            config.setPort(connectionInfo.getPort());
            config.setPassword(RedisPassword.of(connectionInfo.getPassword()));
        }
        else {
            config.setHostName(this.properties.getHost()); //这个propertity加载的就是上面我们贴出来的配置,源码可以看到上面图片中有个RedisPropertity类,配置文件中的配置就是根据这个类配置的
            config.setPort(this.properties.getPort());
            config.setPassword(RedisPassword.of(this.properties.getPassword()));
        }
        config.setDatabase(this.properties.getDatabase());
        return config;
    }

3.那么问题来了,为什么我一直择库失败呢?

spring.redis.database=15  //为什么的我这个配置一直不生效呢?  我想你心里应该有答案了

 如果解决了你的问题,帮忙点个赞

posted @ 2018-11-15 17:15  隔壁w王叔叔  阅读(2940)  评论(0编辑  收藏