1. 程式人生 > >java連線redis的一堆坑

java連線redis的一堆坑

java中連線虛擬機器中的redis要注意以下幾點:

1)ip和埠號不正確

2)虛擬機器中防火牆的原因(可能性較大)

3)redis.conf 中bind 127.0.0.1 未用#註釋掉

4)未取消保護模式

1、java.net.SocketTimeoutException: connect timed out 連線超時

    at redis.clients.jedis.Connection.connect(Connection.java:154)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:83)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:88)
    at redis.clients.jedis.Jedis.get(Jedis.java:97)
    at com.zyao89.jedis.JedisTest.test1(JedisTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at redis.clients.jedis.Connection.connect(Connection.java:148)
    ... 28 more

這種情況先排除是否開啟了redis,或者ip,埠是否正確。如果都沒問題,那八成就是防火牆了。
可能是redis的6379埠無法訪問,請先在cmd中輸入命令
telnet 127.0.0.1 6379
如果不能訪問,需要在遠端機器關掉防火牆或者新增允許通過

CentOS,root使用者登陸,輸入如下命令開啟埠:

/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

重新整理自動啟動儲存

/etc/rc.d/init.d/iptables save

或者直接關閉防火牆:操作如下:

centos7的一些變化,firewalld替換iptables、systemctl 替換service

1.防火牆命令用firewalld取代了iptables了。

檢視防火牆狀態   systemctl status firewalld

臨時關閉防火牆命令,reboot之後,防火牆自動起來。   systemctl stop firewalld

永久關閉防火牆命令。reboot之後,防火牆不會自動啟動    systemctl disable firewalld

啟動防火牆命令   systemctl enable firewalld

2.用systemctl 代替了service,不過為了向後相容,centos7中,service還是可以用的。

如使用systemctl關閉防火牆

systemctl stop firewalld.service 啟動把stop改為start即可

2、Connection refused 拒絕訪問


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 practice.RedisJava.main(RedisJava.java:13)
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)
    ... 6 more

修改redis.conf配置檔案,使用vim命令開啟,找到bind那行修改後,wq儲存退出,重啟redis-server。

redis.conf編輯後

編輯後如上圖,用#號註釋。

修改完之重啟之後可以通過如下命令檢視:ps aux | grep redis

3、DENIED Redis is running in protected mode (保護模式)


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:117)
    at redis.clients.jedis.Protocol.process(Protocol.java:142)
    at redis.clients.jedis.Protocol.read(Protocol.java:196)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:207)
    at redis.clients.jedis.Connection.getBulkReply(Connection.java:196)
    at redis.clients.jedis.Jedis.get(Jedis.java:98)
    at com.zyao89.jedis.JedisTest.test1(JedisTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

報錯資訊很長,但是主要是說redis開啟了protected mode,這也是Redis3.2加入的新特性,開啟保護模式的redis只允許本機登入,連結redis 時只能通過本地localhost (127.0.0.1)這個來連結,而不能用網路ip(192.168..)這個連結,問題然如果用網路ip 連結會報以上錯誤:

是說處於保護模式,只能本地連結,我們需要修改配置檔案../redis.conf
1)開啟配置檔案把下面對應的註釋掉

# bind 127.0.0.1  

2)Redis預設不是以守護程序的方式執行,可以通過該配置項修改,使用yes啟用守護程序,設定為no

daemonize no

3)保護模式

protected-mode no

4)最後關鍵的是:
沒反應應該是你啟動服務端的時候沒有帶上配置檔案。你可以./redis-server redis.conf
你配置好了,但要重新啟動redis,如果還是報一樣的錯誤,很可能是沒有啟動到配置檔案,所以需要真正的和配置檔案啟動需要:
在redis.conf檔案的當前目錄下:

redis-server redis.conf

或者:

redis-server ./redis.conf 

如果還是所某個埠已在使用,那麼可能是有 後臺程式在佔用該埠,需要kill 掉該程式,重新帶上配置檔案。./redis-server redis.conf啟動。
將含有”redis”關鍵詞的程序殺死:

$ ps -ef | grep redis | awk ‘{print $2}’ | xargs kill -9

在linux 中連線客戶端,設定密碼,config set requirepass "123456"  //123456為密碼