本文介绍了无法连接到Redis;嵌套异常是 io.lettuce.core.RedisConnectionException 使用 ReactiveRedisTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是响应式编程的新手.我需要连接到 Redis 以保存和获取一些数据.redis 实例存在于云中.正在使用 Lettuce Connection factory 建立连接.与redis建立连接时,请求失败.这是我的 Redis 配置类:package com.sap.slh.tax.attributes.determination.springwebfluxdemo.config;导入 org.slf4j.Logger;导入 org.slf4j.LoggerFactory;导入 org.springframework.beans.factory.annotation.Value;导入 org.springframework.context.annotation.Bean;导入 org.springframework.context.annotation.Configuration;导入 org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;导入 org.springframework.data.redis.connection.RedisPassword;导入 org.springframework.data.redis.connection.RedisStandaloneConfiguration;导入 org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;导入 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;导入 org.springframework.data.redis.core.ReactiveRedisOperations;导入 org.springframework.data.redis.core.ReactiveRedisTemplate;导入 org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;导入 org.springframework.data.redis.serializer.RedisSerializationContext;导入 org.springframework.data.redis.serializer.StringRedisSerializer;导入 org.springframework.scheduling.annotation.EnableAsync;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxDetails;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxLine;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.util.JsonUtil;@配置@EnableAsync公共类RedisConfig {私有静态最终记录器日志 = LoggerFactory.getLogger(RedisConfig.class);@Value("${vcap.services.redis.credentials.hostname:10.11.241.101}")私人字符串主机;@Value("${vcap.services.redis.credentials.port:36516}")私有int端口;@Value("$vcap.services.redis.credentials.password:123456788")私人字符串密码;@豆角,扁豆public ReactiveRedisConnectionFactory reactorRedisConnectionFactory() {RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port);redisStandaloneConfiguration.setPassword(RedisPassword.of(password));redisStandaloneConfiguration.setDatabase(0);log.error("Redis 独立配置{}",JsonUtil.toJsonString(redisStandaloneConfiguration));LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().build();LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);lettuceConnectionFactory.afterPropertiesSet();返回生菜连接工厂;}@豆角,扁豆ReactiveRedisOperations重新操作(ReactiveRedisConnectionFactory 反应性RedisConnectionFactory) {Jackson2JsonRedisSerializerserializer = new Jackson2JsonRedisSerializer(TaxDetails.class);Jackson2JsonRedisSerializerserializer1 = new Jackson2JsonRedisSerializer(TaxLine.class);RedisSerializationContext.RedisSerializationContextBuilderbuilder = RedisSerializationContext.newSerializationContext(new StringRedisSerializer());RedisSerializationContext上下文 = builder.key(serializer).value(serializer1).build();;返回新的 ReactiveRedisTemplate(反应性RedisConnectionFactory,上下文);}}这是我的查找服务类,它在请求期间实际上与 redis 通信导入 org.slf4j.Logger;导入 org.slf4j.LoggerFactory;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.data.redis.core.ReactiveRedisOperations;导入 org.springframework.stereotype.Service;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.RedisRepo;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxDetails;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxLine;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.util.JsonUtil;进口reactor.core.publisher.Flux;进口reactor.core.publisher.Mono;@服务公共类RedisTaxLineLookUpService {私有静态最终记录器日志 = LoggerFactory.getLogger(RedisTaxLineLookUpService.class);@自动连线私有 ReactiveRedisOperations重新操作;公共 Fluxget(TaxDetails taxDetails) {log.info("要调用redis来获取税行{}", JsonUtil.toJsonString(taxDetails));返回 redisOperations.keys(taxDetails).flatMap(redisOperations.opsForValue()::get);}公共 Mono<RedisRepo>设置(RedisRepo redisRepo){log.info("要调用redis来节省税款{}", JsonUtil.toJsonString(redisRepo.getTaxDetails()));返回 redisOperations.opsForValue().set(redisRepo.getTaxDetails(), redisRepo.getTaxLine()).map(__ -> redisRepo);}}堆栈跟踪:2020-03-26T16:27:54.513+0000 [APP/PROC/WEB/0] OUT org.springframework.data.redis.RedisConnectionFailureException:无法连接到Redis;嵌套异常是 io.lettuce.core.RedisConnectionException:无法连接到 10.11.241.101:36516 |在 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1199) |抑制:reactor.core.publisher.FluxOnAssembly$OnAssemblyException:|在以下站点发现错误: ||_ 检查站?处理程序 com.sap.slh.tax.attributes.determination.springwebfluxdemo.controller.TaxLinesDeterminationController#saveTaxLines(RedisRepo) [DispatcherHandler] ||_ 检查站?HTTP POST "/tax/lines/save/" [ExceptionHandlingWebHandler] |堆栈跟踪: |在 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1199) |在 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1178) |在 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedReactiveConnection(LettuceConnectionFactory.java:952) |在 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:429) |在 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:94) |在 org.springframework.data.redis.core.ReactiveRedisTemplate.lambda$doInConnection$0(ReactiveRedisTemplate.java:198) |在reactor.core.publisher.MonoSupplier.call(MonoSupplier.java:85) |在 reactor.core.publisher.FluxUsingWhen.subscribe(FluxUsingWhen.java:80) |在reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:55) |在reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150) |在reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) |在reactor.core.publisher.MonoFlatMap$FlatMapInner.onNext(MonoFlatMap.java:241) |在 reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:73) |在 reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:203) |在 reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:203) |在reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) |在reactor.core.publisher.MonoIgnoreThen$ThenAcceptInner.onNext(MonoIgnoreThen.java:296) |在reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) |在reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:144) |在reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) |在reactor.core.publisher.MonoZip$ZipCoordinator.signal(MonoZip.java:247) |在reactor.core.publisher.MonoZip$ZipInner.onNext(MonoZip.java:329) |在reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onNext(MonoPeekTerminal.java:173) |在 reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:92) |在reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67) |在 reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:73) |在reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) |在reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:144) |在 reactor.core.publisher.FluxContextStart$ContextStartSubscriber.onNext(FluxContextStart.java:103) |在 reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:287) |在reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onNext(FluxFilterFuseable.java:330) |在reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) |在reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:160) |在 reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:136) |在 reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:252) |在 reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:136) |在 reactor.netty.channel.FluxReceive.terminateReceiver(FluxReceive.java:419) |在 reactor.netty.channel.FluxReceive.drainReceiver(FluxReceive.java:209) |在 reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:367) |在reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:363) |在reactor.netty.http.server.HttpServerOperations.onInboundNext(HttpServerOperations.java:489) |在reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:90) |在 io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)任何建议或答案都会非常有帮助!提前致谢 ! 解决方案 我更新了我的 RedisConfig 类如下:导入 java.time.Duration;导入 java.util.List;导入 java.util.stream.Collectors;导入 org.springframework.context.annotation.Bean;导入 org.springframework.context.annotation.Configuration;导入 org.springframework.context.annotation.Primary;导入 org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;导入 org.springframework.data.redis.connection.RedisConfiguration;导入 org.springframework.data.redis.connection.RedisNode;导入 org.springframework.data.redis.connection.RedisPassword;导入 org.springframework.data.redis.connection.RedisSentinelConfiguration;导入 org.springframework.data.redis.connection.RedisStandaloneConfiguration;导入 org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;导入 org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;导入 org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;导入 org.springframework.data.redis.core.ReactiveRedisOperations;导入 org.springframework.data.redis.core.ReactiveRedisTemplate;导入 org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;导入 org.springframework.data.redis.serializer.RedisSerializationContext;导入 org.springframework.data.redis.serializer.StringRedisSerializer;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxDetails;导入 com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxLine;导入 io.lettuce.core.RedisURI;导入 io.pivotal.cfenv.core.CfEnv;@配置公共类RedisConfig {CfEnv cfEnv = new CfEnv();String tag = "redis";String redisHost = cfEnv.findCredentialsByTag(tag).getHost();@豆角,扁豆@基本的public ReactiveRedisConnectionFactory reactorRedisConnectionFactory(RedisConfiguration defaultRedisConfig) {LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(60000)).build();return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);}@豆角,扁豆公共RedisConfiguration defaultRedisConfig() {如果(redisHost != null){//RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("127.0.0.1", 6379);RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();String redisPort = cfEnv.findCredentialsByTag(tag).getPort();String redisPassword = cfEnv.findCredentialsByTag(tag).getPassword();config.setHostName(redisHost);config.setPassword(RedisPassword.of(redisPassword));config.setPort(Integer.parseInt(redisPort));config.setDatabase(2);返回配置;} 别的 {RedisSentinelConfiguration config = new RedisSentinelConfiguration();String uri = cfEnv.findCredentialsByTag(tag).getUri();RedisURI redisURI = RedisURI.create(uri);config.master(redisURI.getSentinelMasterId());列表节点 = redisURI.getSentinels().stream().map(redisUri -> populateNode(redisUri.getHost(), redisUri.getPort())).collect(Collectors.toList());nodes.forEach(node -> config.addSentinel(node));config.setPassword(RedisPassword.of(redisURI.getPassword()));config.setDatabase(2);返回配置;}}@豆角,扁豆public ReactiveRedisOperations反应式RedisTemplate(ReactiveRedisConnectionFactory 工厂) {StringRedisSerializer keySerializer = new StringRedisSerializer();Jackson2JsonRedisSerializervalueSerializer = new Jackson2JsonRedisSerializer(TaxLine.class);Jackson2JsonRedisSerializervalueSerializer1 = 新的 Jackson2JsonRedisSerializer(TaxDetails.class);RedisSerializationContext.RedisSerializationContextBuilderbuilder = RedisSerializationContext.newSerializationContext(keySerializer);RedisSerializationContext上下文 = builder.key(valueSerializer1).value(valueSerializer).build();返回新的 ReactiveRedisTemplate<>(工厂,上下文);}私有RedisNode populateNode(字符串主机,整数端口){返回新的RedisNode(主机,端口);}}cfEnv 的依赖项: io.pivotal.cfenv<artifactId>java-cfenv-boot</artifactId><version>2.1.1.RELEASE</version></依赖>I am new to Reactive Programming. i need to connect to Redis to save and get some data. The redis instance is present in cloud.Am using Lettuce Connection factory to establish the connection.when establishing the connection to redis, the request fails.Here is my Redis configuration class :package com.sap.slh.tax.attributes.determination.springwebfluxdemo.config;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;import org.springframework.data.redis.connection.RedisPassword;import org.springframework.data.redis.connection.RedisStandaloneConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.ReactiveRedisOperations;import org.springframework.data.redis.core.ReactiveRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.scheduling.annotation.EnableAsync;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxDetails;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxLine;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.util.JsonUtil;@Configuration@EnableAsyncpublic class RedisConfig { private static final Logger log = LoggerFactory.getLogger(RedisConfig.class); @Value("${vcap.services.redis.credentials.hostname:10.11.241.101}") private String host; @Value("${vcap.services.redis.credentials.port:36516}") private int port; @Value("$vcap.services.redis.credentials.password:123456788") private String password; @Bean public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port); redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); redisStandaloneConfiguration.setDatabase(0); log.error("Redis standalone configuration{}",JsonUtil.toJsonString(redisStandaloneConfiguration)); LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().build(); LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig); lettuceConnectionFactory.afterPropertiesSet(); return lettuceConnectionFactory; } @Bean ReactiveRedisOperations<TaxDetails, TaxLine> redisOperations( ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) { Jackson2JsonRedisSerializer<TaxDetails> serializer = new Jackson2JsonRedisSerializer<>(TaxDetails.class); Jackson2JsonRedisSerializer<TaxLine> serializer1 = new Jackson2JsonRedisSerializer<>(TaxLine.class); RedisSerializationContext.RedisSerializationContextBuilder<TaxDetails, TaxLine> builder = RedisSerializationContext .newSerializationContext(new StringRedisSerializer()); RedisSerializationContext<TaxDetails, TaxLine> context = builder.key(serializer).value(serializer1).build(); ; return new ReactiveRedisTemplate<>( reactiveRedisConnectionFactory, context); }}and here is my look up service class which actually communicates with redis during the requestimport org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.ReactiveRedisOperations;import org.springframework.stereotype.Service;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.RedisRepo;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxDetails;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxLine;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.util.JsonUtil;import reactor.core.publisher.Flux;import reactor.core.publisher.Mono;@Servicepublic class RedisTaxLineLookUpService { private static final Logger log = LoggerFactory.getLogger(RedisTaxLineLookUpService.class); @Autowired private ReactiveRedisOperations<TaxDetails, TaxLine> redisOperations; public Flux<TaxLine> get(TaxDetails taxDetails) { log.info("going to call redis to fetch tax lines{}", JsonUtil.toJsonString(taxDetails)); return redisOperations.keys(taxDetails).flatMap(redisOperations.opsForValue()::get); } public Mono<RedisRepo> set(RedisRepo redisRepo) { log.info("going to call redis to save tax lines{}", JsonUtil.toJsonString(redisRepo.getTaxDetails())); return redisOperations.opsForValue().set(redisRepo.getTaxDetails(), redisRepo.getTaxLine()) .map(__ -> redisRepo); }}Stack trace :2020-03-26T16:27:54.513+0000 [APP/PROC/WEB/0] OUT org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 10.11.241.101:36516 | at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1199) | Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: | Error has been observed at the following site(s): | |_ checkpoint ? Handler com.sap.slh.tax.attributes.determination.springwebfluxdemo.controller.TaxLinesDeterminationController#saveTaxLines(RedisRepo) [DispatcherHandler] | |_ checkpoint ? HTTP POST "/tax/lines/save/" [ExceptionHandlingWebHandler] | Stack trace: | at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1199) | at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1178) | at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedReactiveConnection(LettuceConnectionFactory.java:952) | at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:429) | at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:94) | at org.springframework.data.redis.core.ReactiveRedisTemplate.lambda$doInConnection$0(ReactiveRedisTemplate.java:198) | at reactor.core.publisher.MonoSupplier.call(MonoSupplier.java:85) | at reactor.core.publisher.FluxUsingWhen.subscribe(FluxUsingWhen.java:80) | at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:55) | at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150) | at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) | at reactor.core.publisher.MonoFlatMap$FlatMapInner.onNext(MonoFlatMap.java:241) | at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:73) | at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:203) | at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:203) | at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) | at reactor.core.publisher.MonoIgnoreThen$ThenAcceptInner.onNext(MonoIgnoreThen.java:296) | at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) | at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:144) | at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) | at reactor.core.publisher.MonoZip$ZipCoordinator.signal(MonoZip.java:247) | at reactor.core.publisher.MonoZip$ZipInner.onNext(MonoZip.java:329) | at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onNext(MonoPeekTerminal.java:173) | at reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:92) | at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67) | at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:73) | at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) | at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:144) | at reactor.core.publisher.FluxContextStart$ContextStartSubscriber.onNext(FluxContextStart.java:103) | at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:287) | at reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onNext(FluxFilterFuseable.java:330) | at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1705) | at reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:160) | at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:136) | at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:252) | at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:136) | at reactor.netty.channel.FluxReceive.terminateReceiver(FluxReceive.java:419) | at reactor.netty.channel.FluxReceive.drainReceiver(FluxReceive.java:209) | at reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:367) | at reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:363) | at reactor.netty.http.server.HttpServerOperations.onInboundNext(HttpServerOperations.java:489) | at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:90) | at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)Any suggestions or answers would be highly helpful ! Thanks in Advance ! 解决方案 i updated my RedisConfig class as follows :import java.time.Duration;import java.util.List;import java.util.stream.Collectors;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;import org.springframework.data.redis.connection.RedisConfiguration;import org.springframework.data.redis.connection.RedisNode;import org.springframework.data.redis.connection.RedisPassword;import org.springframework.data.redis.connection.RedisSentinelConfiguration;import org.springframework.data.redis.connection.RedisStandaloneConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;import org.springframework.data.redis.core.ReactiveRedisOperations;import org.springframework.data.redis.core.ReactiveRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxDetails;import com.sap.slh.tax.attributes.determination.springwebfluxdemo.model.TaxLine;import io.lettuce.core.RedisURI;import io.pivotal.cfenv.core.CfEnv;@Configurationpublic class RedisConfig { CfEnv cfEnv = new CfEnv(); String tag = "redis"; String redisHost = cfEnv.findCredentialsByTag(tag).getHost(); @Bean @Primary public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(RedisConfiguration defaultRedisConfig) { LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder() .commandTimeout(Duration.ofMillis(60000)).build(); return new LettuceConnectionFactory(defaultRedisConfig, clientConfig); } @Bean public RedisConfiguration defaultRedisConfig() { if (redisHost != null) {// RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("127.0.0.1", 6379); RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); String redisPort = cfEnv.findCredentialsByTag(tag).getPort(); String redisPassword = cfEnv.findCredentialsByTag(tag).getPassword(); config.setHostName(redisHost); config.setPassword(RedisPassword.of(redisPassword)); config.setPort(Integer.parseInt(redisPort)); config.setDatabase(2); return config; } else { RedisSentinelConfiguration config = new RedisSentinelConfiguration(); String uri = cfEnv.findCredentialsByTag(tag).getUri(); RedisURI redisURI = RedisURI.create(uri); config.master(redisURI.getSentinelMasterId()); List<RedisNode> nodes = redisURI.getSentinels().stream() .map(redisUri -> populateNode(redisUri.getHost(), redisUri.getPort())).collect(Collectors.toList()); nodes.forEach(node -> config.addSentinel(node)); config.setPassword(RedisPassword.of(redisURI.getPassword())); config.setDatabase(2); return config; } } @Bean public ReactiveRedisOperations<TaxDetails, TaxLine> reactiveRedisTemplate( ReactiveRedisConnectionFactory factory) { StringRedisSerializer keySerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer<TaxLine> valueSerializer = new Jackson2JsonRedisSerializer<>( TaxLine.class); Jackson2JsonRedisSerializer<TaxDetails> valueSerializer1 = new Jackson2JsonRedisSerializer<>( TaxDetails.class); RedisSerializationContext.RedisSerializationContextBuilder<TaxDetails, TaxLine> builder = RedisSerializationContext .newSerializationContext(keySerializer); RedisSerializationContext<TaxDetails, TaxLine> context = builder.key(valueSerializer1).value(valueSerializer).build(); return new ReactiveRedisTemplate<>(factory, context); } private RedisNode populateNode(String host, Integer port) { return new RedisNode(host, port); }}dependencies for cfEnv: <groupId>io.pivotal.cfenv</groupId> <artifactId>java-cfenv-boot</artifactId> <version>2.1.1.RELEASE</version> </dependency> 这篇关于无法连接到Redis;嵌套异常是 io.lettuce.core.RedisConnectionException 使用 ReactiveRedisTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-07 03:39