V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shayang888
V2EX  ›  Java

spring boot2.1 为什么切换不了 redis 的 databases

  •  
  •   shayang888 · 2018-10-24 12:34:28 +08:00 · 3143 次点击
    这是一个创建于 1983 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的 pom

    <dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-webflux</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.session</groupId>
    			<artifactId>spring-session-data-redis</artifactId>
    		</dependency>
    

    application.properties

    spring.data.mongodb.uri=mongodb://localhost:27017/apitest
    spring.jackson.time-zone=GMT+8
    spring.redis.host=localhost
    spring.redis.database=1
    spring.redis.port=6379
    spring.session.store-type=redis
    

    我发现我运行后,始终只出现在 database=0 有数据,database=1 是空的

    13 条回复    2019-03-01 14:05:49 +08:00
    leriou
        1
    leriou  
       2018-10-24 12:51:16 +08:00
    跟 pom 没关系吧, 先 select db,每个 db 写两条数据看看, 再测试, redis 里面如果本来其他 db 就没有数据有啥用, redis 集群的话也用不了 db
    shayang888
        2
    shayang888  
    OP
       2018-10-24 12:58:28 +08:00
    @leriou application.properties 里的配置我都没变过 就 pom 里 redis 改成了 reactive 的,boot starter 改成了 webflux 的 之前的旧的我改成几数据就在几里 这个新特性就变成这样了
    wowo243
        3
    wowo243  
       2018-10-24 13:00:50 +08:00 via Android
    看源码呗,这里问有啥用
    xgfan
        4
    xgfan  
       2018-10-24 13:43:59 +08:00 via iPhone
    看下那几个 autoconfig 的源码就好了
    artist
        5
    artist  
       2018-10-24 15:12:02 +08:00   ❤️ 1
    JedisConnectionFactory
    /**
    * Sets the index of the database used by this connection factory. Default is 0.
    *
    * @param index database index.
    * @deprecated since 2.0, configure the client name using {@link RedisSentinelConfiguration} or
    * {@link RedisStandaloneConfiguration}.
    */
    @Deprecated
    public void setDatabase(int index) {

    Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");

    if (isRedisSentinelAware()) {
    sentinelConfig.setDatabase(index);
    return;
    }

    standaloneConfig.setDatabase(index);
    }
    shayang888
        6
    shayang888  
    OP
       2018-10-24 15:45:15 +08:00
    @artist 我按你说的,专门创建了个 config 类来设置 database 也没用 我 redis 里 select 1 keys "*"返回的还是空的,数据都在 0 里面
    shayang888
        7
    shayang888  
    OP
       2018-10-24 16:02:47 +08:00
    @artist 解决了 谢谢
    2yug
        8
    2yug  
       2019-02-28 10:05:21 +08:00
    你好,能说下怎么解决的嘛,我也遇到这个问题了~
    shayang888
        9
    shayang888  
    OP
       2019-02-28 11:09:52 +08:00
    @2yug
    ```
    @Configuration
    @EnableRedisWebSession
    public class RedisSessionConfig {

    @Value("${spring.redis.database}")
    private int database;

    @Bean
    public ReactiveRedisConnectionFactory redisConnectionFactory() {
    RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
    redisSentinelConfiguration.setDatabase(database);
    return new LettuceConnectionFactory(redisSentinelConfiguration);
    }
    }
    ```
    2yug
        10
    2yug  
       2019-02-28 18:39:21 +08:00
    @shayang888
    按你的配置了还是不行,哎!
    2yug
        11
    2yug  
       2019-03-01 11:21:45 +08:00
    @shayang888 我把 springboot 版本升级成 2.1.3 (之前是 2.0.0 )就可以了!!!没有加其他配置,只在 properties 里面指定了 datebase。
    shayang888
        12
    shayang888  
    OP
       2019-03-01 12:55:58 +08:00
    @2yug properties 里要指定啊 @Value("${spring.redis.database}")这个就是读取里面的配置
    2yug
        13
    2yug  
       2019-03-01 14:05:49 +08:00
    @shayang888 嗯,这个我知道,但是我现在没加你发的配置也可以了~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2697 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:12 · PVG 23:12 · LAX 08:12 · JFK 11:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.