19. Jedis 测试联通及问题解决

原创
2017/03/11 00:39
阅读数 100

依赖包:

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

连接虚拟机上的Redis 

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

第一次连接报错:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
	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.wgy.redis.Redis.PingTest.main(PingTest.java:8)

原因:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

①. redis 默认配置是只能由当前服务器访问,若想要其他ip也能访问,将bind 127.0.0.1 注释掉即可。

②. 检查防火墙是否已开启,若开启,需关掉。

CentOS Linux开启和关闭防火墙命令有两种,

1) 临时生效,重启后复原
开启: service iptables start
关闭: service iptables stop
 
2) 永久性生效,重启后不会复原
开启: chkconfig iptables on
关闭: chkconfig iptables off

修改后重新启动redis服务,然后运行程序,报错

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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.
	at redis.clients.jedis.Protocol.processError(Protocol.java:127)
	at redis.clients.jedis.Protocol.process(Protocol.java:161)
	at redis.clients.jedis.Protocol.read(Protocol.java:215)
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
	at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
	at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196)
	at com.wgy.redis.Redis.PingTest.main(PingTest.java:8)

 提示需关闭保护模式,将protected-mode 修改为no ,重新启动redis服务,实现连接

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

若不关闭protected-mode,也可以通过设置redis 的密码实现联通

127.0.0.1:6379> CONFIG SET requirepass 123456
OK
    public static void main(String[] args) {
		Jedis jedis = new Jedis("192.168.10.130",6379);
		jedis.auth("123456");
		System.out.println(jedis.ping());
	}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部