java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelIm

1、前言

idea中spring boot 项目在使用了redis之后,不用redis后,启动正常,登录到首页报错。

2、报错原因

我之前用redis时监控了登录信息,把登录页面刷新次数记录到缓存中,但是我没有使用redis后,忘记禁用相应的redis调用代码,导致找不到相应信息报错。

3、报错产生的controller.LoginController.java文件

@GetMapping("/index")
    public String toIndex(HttpSession session,Model model){
    
    
        Object loginUser = session.getAttribute("loginUser");
        //将访问次数放到了缓存中
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String s = operations.get("/index");
        model.addAttribute("indexCount",s);
        System.out.println("index页面访问次数:"+s);
        return "index";
    }

4、解决办法
禁用(注释掉)调用了redis的代码就好了,如果这里注释了还不行,可能其他地方还有redis调用没有注释掉的,要全找到,把不用的都禁用了。

@GetMapping("/index")
    public String toIndex(HttpSession session,Model model){
    
    
        Object loginUser = session.getAttribute("loginUser");
    //    如果项目没有使用redis的时候,下面的几行调用必须要禁用掉,不然一直报错 
        //将访问次数放到了缓存中
   //     ValueOperations<String, String> operations = redisTemplate.opsForValue();
   //     String s = operations.get("/index");
   //     model.addAttribute("indexCount",s);
   //     System.out.println("index页面访问次数:"+s);
        return "index";
    }

4、idea报错信息

2021-11-16 15:39:31.761 ERROR 1404 --- [nio-8087-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379] with root cause

java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_41]
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_41]

Guess you like

Origin blog.csdn.net/qq_43987149/article/details/121357668