Jedis連線 HelloWorld實現
直接進入正題...... 建立一個maven工程,在pom里加下jedis依賴,
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
注意:有時候會報錯,原因就是專案jdk對jedis版本的支援有要求,根據錯誤提示修改上面<version>2.4.2</version>這個配置即可.
java連線redis的方式有兩種:
一種是jedis直接連線redis
另一種是通過連線池的方式連線redis
這裡,我們依次介紹一下.
class JedisTest {
|
測試程式碼,
執行 報錯了
連線超時,
我們配置下防火牆 開一個6379埠許可權
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
繼續執行 還是報錯 連線超時 錯誤;
我們配置下 redis配置檔案
[[email protected] redis]# vi /usr/local/redis/redis.conf
這裡綁定了本機,我們把這個備註掉;
# bind 127.0.0.1
配置完後
[[email protected] redis]# ./bin/redis-cli shutdown
[[email protected] redis]# ./bin/redis-server ./redis.conf
要重啟下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.Jedis.set(Jedis.java:121)
at com.java1234.redis.JedisTest.main(JedisTest.java:14)
這個是因為遠端連線redis redis自我保護 拒絕訪問;
有兩種方法 解決
第一種 直接去掉自我保護功能(不推薦)
[[email protected] redis]# vi /usr/local/redis/redis.conf
進入配置
找到 protected-mode yes
改成 no即可
編輯後 重啟redis服務,然後執行 ,結果出來了
第二種 設定redis連線密碼
進入客戶端
[[email protected] redis]# ./bin/redis-cli
127.0.0.1:6379> config set requirepass 123456
設定密碼 123456
127.0.0.1:6379> quit
[[email protected] redis]# ./bin/redis-cli
127.0.0.1:6379> auth 123456
OK
說明設定成功
1 package com.lxz; import redis.clients.jedis.Jedis; /** * java連線redis * @author admin * */ public class JedisTest { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.19.128", 6379); jedis.auth("[email protected]"); jedis.set("hello", "你好"); String value = jedis.get("hello"); System.out.println(value); jedis.close(); } } |
這樣就OK了.
=======================================================
接下來,講一下redis連線池例項:
|
執行:
這就大功告成了.......