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

2018-10-24 12:34:28 +08:00
 shayang888

我的 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 是空的

3176 次点击
所在节点    Java
13 条回复
leriou
2018-10-24 12:51:16 +08:00
跟 pom 没关系吧, 先 select db,每个 db 写两条数据看看, 再测试, redis 里面如果本来其他 db 就没有数据有啥用, redis 集群的话也用不了 db
shayang888
2018-10-24 12:58:28 +08:00
@leriou application.properties 里的配置我都没变过 就 pom 里 redis 改成了 reactive 的,boot starter 改成了 webflux 的 之前的旧的我改成几数据就在几里 这个新特性就变成这样了
wowo243
2018-10-24 13:00:50 +08:00
看源码呗,这里问有啥用
xgfan
2018-10-24 13:43:59 +08:00
看下那几个 autoconfig 的源码就好了
artist
2018-10-24 15:12:02 +08:00
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
2018-10-24 15:45:15 +08:00
@artist 我按你说的,专门创建了个 config 类来设置 database 也没用 我 redis 里 select 1 keys "*"返回的还是空的,数据都在 0 里面
shayang888
2018-10-24 16:02:47 +08:00
@artist 解决了 谢谢
2yug
2019-02-28 10:05:21 +08:00
你好,能说下怎么解决的嘛,我也遇到这个问题了~
shayang888
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
2019-02-28 18:39:21 +08:00
@shayang888
按你的配置了还是不行,哎!
2yug
2019-03-01 11:21:45 +08:00
@shayang888 我把 springboot 版本升级成 2.1.3 (之前是 2.0.0 )就可以了!!!没有加其他配置,只在 properties 里面指定了 datebase。
shayang888
2019-03-01 12:55:58 +08:00
@2yug properties 里要指定啊 @Value("${spring.redis.database}")这个就是读取里面的配置
2yug
2019-03-01 14:05:49 +08:00
@shayang888 嗯,这个我知道,但是我现在没加你发的配置也可以了~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/500612

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX