关于Jedis连接ECS

环境

Java代码

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

Maven


    
      redis.clients
      jedis
      2.9.0
    
  

服务器使用的是阿里云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)