Redis读取超时异常

时间:2019-02-26 11:59:48

标签: spring-boot redis spring-data-redis

我正试图在Redis服务器(已安装在另一台服务器计算机上)中保存价值。当我尝试保存上千条记录时,它给我超时错误。第一记录保存非常顺利。

这是我的Redis配置文件

package com.nsdlegov.springredis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.PoolConfig;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.nsdlegov.springredis.model.IfscMaster;
import com.nsdlegov.springredis.model.PincodeMaster;
import com.nsdlegov.springredis.model.SchemeMaster;

@Configuration
public class RedisNewConfig {

    @Autowired
    private Environment environment;


    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("172.18.8.224", 6379);
        JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration);
        factory.getPoolConfig().setMaxIdle(1800000);
        factory.getPoolConfig().setMinIdle(1800000);
        return factory;
    }

    @Bean
    public RedisTemplate<String,SchemeMaster> redisTemplateSchemeMaster(){

        RedisTemplate<String,SchemeMaster> template= new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setEnableTransactionSupport(true);
        template.setHashKeySerializer(new StringRedisSerializer());  
        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(SchemeMaster.class)); 
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(SchemeMaster.class));

        template.setEnableTransactionSupport(true);
        return template;
    }

    @Bean
    public RedisTemplate<String,PincodeMaster> redisTemplatePincodeMaster() {
        RedisTemplate<String, PincodeMaster> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setEnableTransactionSupport(true);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(PincodeMaster.class));
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(PincodeMaster.class));
        return template;
    }

    @Bean
    public RedisTemplate<String,IfscMaster> redisTemplateIfscMaster() {
        RedisTemplate<String, IfscMaster> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setEnableTransactionSupport(true);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(IfscMaster.class));
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(IfscMaster.class));
        return template;
    }
}

这是Redis控制器文件

package com.nsdlegov.springredis.controller;

import java.util.ArrayList;
import java.util.List;

import com.nsdlegov.springredis.model.SchemeMaster;
import com.nsdlegov.springredis.repository.IfscRedisOperationsRepository;
import com.nsdlegov.springredis.repository.PincodeMasterRedisOperationsRepository;
import com.nsdlegov.springredis.repository.SchemeMasterRepository;
import com.nsdlegov.springredis.service.RedisService;

@RestController
@RequestMapping(value={"/Spring-New-Redis"})
public class RedisCacheController {

    @Autowired
    RedisService redisService;

    @Autowired 
    private IfscRedisOperationsRepository ifscRedisOperationsRepository;


    @GetMapping("/setIfscDetails")
    public String setIfscDetails() {
        String result = null;
        try {

            result = ifscRedisOperationsRepository.setIfscDetailsValue();
            System.out.println("Caching IFSC code completed.");

        } catch (Exception e) {
            e.printStackTrace();
            result = "Unable to add Ifsc Details to cache!";
        }

        return result;
    }
}

这是存储库文件

package com.nsdlegov.springredis.repository;


import java.util.List;
import java.util.Map;
import java.util.Set;

@Repository
public class IfscRedisOperationsRepository {

    @Autowired
    RedisTemplate<String, IfscMaster> redisTemplateIfscMaster;

    @Autowired
    private IfscMasterRepository ifscMasterRepository;


    public String setIfscDetailsValue() {

        String result = null;
        try {
            Map<String, IfscMaster> map = new HashMap<>();
            List<IfscMaster> ifscList = ifscMasterRepository.findAll();

            for (IfscMaster ifsc : ifscList) {

                map.put(CommonConstantsUtilsRedis.IFSC_KEY + ":" + ifsc.getImId().toString() + "_" + ifsc.getImIfscCode(), ifsc);

                redisTemplateIfscMaster.opsForValue().multiSetIfAbsent(map);

                result = "IfscDetails added to cache successfully!";
            }
            System.out.println("Successfully cache IFSC code.");

        } catch (Exception e) {
            e.printStackTrace();
            result = "Unable to add IfscDetails to cache!";
        }

        return result;
    }
}

我已经正确配置了服务器地址和数据源属性,因为它的第一个条目已正确插入。

在这里,当我在控制器中调用“ setIfscDetails”方法时,出现以下错误堆栈。

org.springframework.data.redis.RedisConnectionFailureException: java.net.SocketTimeoutException: Read timed out; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:67)
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41)
    at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
    at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
    at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:142)
    at org.springframework.data.redis.connection.jedis.JedisStringCommands.convertJedisAccessException(JedisStringCommands.java:815)
    at org.springframework.data.redis.connection.jedis.JedisStringCommands.mSetNX(JedisStringCommands.java:378)
    at org.springframework.data.redis.connection.DefaultedRedisConnection.mSetNX(DefaultedRedisConnection.java:316)
    at org.springframework.data.redis.core.DefaultValueOperations.lambda$multiSetIfAbsent$9(DefaultValueOperations.java:225)
    at org.springframework.data.redis.core.DefaultValueOperations$$Lambda$782/449667204.doInRedis(Unknown Source)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
    at org.springframework.data.redis.core.DefaultValueOperations.multiSetIfAbsent(DefaultValueOperations.java:225)
    at com.nsdlegov.springredis.repository.IfscRedisOperationsRepository.setIfscDetailsValue(IfscRedisOperationsRepository.java:40)
    at com.nsdlegov.springredis.repository.IfscRedisOperationsRepository$$FastClassBySpringCGLIB$$417eb00b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.nsdlegov.springredis.repository.IfscRedisOperationsRepository$$EnhancerBySpringCGLIB$$c7d67782.setIfscDetailsValue(<generated>)
    at com.nsdlegov.springredis.controller.RedisCacheController.setIfscDetails(RedisCacheController.java:146)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

请帮助我找出可能的解决方案。更重要的是,我已经在Spring Boot Application中实现了该项目。预先感谢

0 个答案:

没有答案