有没有一种方法可以在Java中使用Redis reactive设置超时?
我正在使用有没有一种方法可以在Java中使用Redis reactive设置超时?,java,spring-webflux,spring-data-redis-reactive,Java,Spring Webflux,Spring Data Redis Reactive,我正在使用reactivedisconnection配置与本地redis容器的连接 但在未来,应用程序将托管在Web服务器上,而redis将托管在不同的服务器上 有没有为请求设置超时的选项?可以在反应式连接实现中配置超时。如果您使用莴苣进行Redis连接,则可以执行以下操作 @Bean public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() { return new LettuceConnectionF
reactivedisconnection
配置与本地redis容器的连接
但在未来,应用程序将托管在Web服务器上,而redis将托管在不同的服务器上
有没有为请求设置超时的选项?可以在反应式连接实现中配置超时。如果您使用莴苣进行Redis连接,则可以执行以下操作
@Bean
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
return new LettuceConnectionFactory(new RedisStandaloneConfiguration(), LettuceClientConfiguration.builder().commandTimeout(Duration.ofSeconds(2)).build());
}
然后使用连接工厂
创建反应器模板
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString
(ReactiveRedisConnectionFactory connectionFactory) {
return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
}
@Bean
公共ReactiveDistemplate ReactiveDistemplate字符串
(反应器连接工厂连接工厂){
返回新的ReactiveEdistemplate(connectionFactory,RedisSerializationContext.string());
}
经过一些研究和测试,我发现必须在请求查询上设置超时
因此,在config类上:
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString
(ReactiveRedisConnectionFactory connectionFactory) {
return new ReactiveRedisTemplate<>
(connectionFactory, RedisSerializationContext.string());
}
@Bean
公共ReactiveDistemplate ReactiveDistemplate字符串
(反应器连接工厂连接工厂){
返回新的反应器模板
(connectionFactory,RedisSerializationContext.string());
}
在服务中:
@Autowired
private ReactiveRedisTemplate<String, Response> repository;
@Autowired
私有反应器模板库;
public Mono-execute(字符串值){
返回repository.opsForHash().entries(“键到搜索”)
.timeout(百万持续时间(超时))
.collect(Collectors.toMap(“这里的代码”);
编辑:感谢所有在此提供帮助的人。无论我设置了什么,都需要10秒钟。
public Mono<String> execute(String value){
return repository.opsForHash().entries("KEY_TO_SEARCH")
.timeout(Duration.ofMillis(TIMEOUT))
.collect(Collectors.toMap("CODE_HERE");