美文网首页
关于Jedis连接ECS

关于Jedis连接ECS

作者: RalapHao | 来源:发表于2017-07-24 23:58 被阅读0次

环境

Java代码

 public static void main(String[] args) {
        Jedis jedis = new Jedis("**.**.**.**",6379);
        System.out.println(jedis.ping());
    }

Maven

<dependencies>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
  </dependencies>

服务器使用的是阿里云Ecs

问题

jedis访问Ecs的Redis报错

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
    at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
    at com.test.redis.JedisTest.main(JedisTest.java:12)
    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:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 11 more

原因分析

  1. Ecs的防火强拦截了请求

    • 在Ecs终端执行以下命令
    Paste_Image.png

    Active:inactive这表明防火墙没有开启

  2. Redis配置文件有问题(默认只能是本地访问)
    修改redis.conf文件
    将protected-mode改为no( 保护机制默认是开启的,如果非本地访问,需要认证)

Paste_Image.png

同时将

bind 127.0.0.1注释掉

Paste_Image.png

保存重启redis
这时访问正常了

注意

如果以上修改还有错误

DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, 
    no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. 
    If you want to connect from external computers to Redis you may adopt one of the following solutions: 
    1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 
    2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
    3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
    4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

这个问题说的很清楚了,就是你还是没有将protected-mode设置为no导致的,

相关文章

  • 关于Jedis连接ECS

    环境 Java代码 Maven 服务器使用的是阿里云Ecs 问题 jedis访问Ecs的Redis报错 原因分析 ...

  • java客户端连接redis

    一、连接方案 jedis连接实现lettuce连接实现redisson连接 jedis连接 简单实用,但是jedi...

  • jedis

    https://github.com/xetorthio/jedis jedis 使用 连接 key...

  • redis 学习(8)-- redis 客户端 -- Jedis

    redis 客户端 -- Jedis 1. Jedis 直连 本质是 TCP 连接。 执行流程 创建Jedis对象...

  • Redis客户端

    1)区分Jedis直连和连接池的区别,在生产环境中使用连接池 2)Jedis.close()在直连下是关闭连接,在...

  • Jedis连接池的使用方法

      Jedis有直连方式,所谓直连指的是Jedis每次都会新建TCP连接,使用后在断开连接,对于平凡访问Redis...

  • Jedis

    Jedis是Redis的Java客户端,连接池使用commons-pool2。此文记录Jedis的设计,也探讨连接...

  • 5.7-SpringBoot整合Jedis+Lettuce客户端

    SpringBoot整合Jedis+Lettuce客户端连接池配置实战 简介:SpringBoot整合Jedis+...

  • Spring 集成Jedis客户端对Redis进行使用

    1、依赖jar包,commons-pool2,jedis; 2、Spring 整合,配置jedis连接池,bean...

  • 连接 ECS

    买了自己的阿里云服务器 很激动整理遇到的问题本地环境 macOS 1. ssh 连接不成功 使用 ssh 连接 L...

网友评论

      本文标题:关于Jedis连接ECS

      本文链接:https://www.haomeiwen.com/subject/zupikxtx.html