Java連接Redis全過程講解
Java連接Redis
Jedis Client是Redis官網推薦的一個面向java客戶端,庫文件實現(xiàn)了對redis各類API進行封裝調用.
引入jar包
我創(chuàng)建的是maven項目,所以只用在pom文件中加入
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0</version> </dependency>
如果不是maven項目,你要確定引入相關依賴

編寫測試類
package cn.jiangdoc;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
*
* @author jiangdoc
*
*/
public class JedisUtil {
public static void main(String[] args) {
//ip地址,端口號
Jedis jedis = cli_single("192.168.1.103", 6379);
jedis.set("key", "first Java connect!");
String value = jedis.get("key");
System.out.println(value);
}
/**
* 單個連接
*
* @param host
* @param port
* @return
*/
public static Jedis cli_single(String host, int port) {
try {
return new Jedis(host, port);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 連接池
*
* @param host
* @param port
* @return
*/
public static Jedis cli_pool(String host, int port) {
JedisPoolConfig config = new JedisPoolConfig();
// 最大連接數(shù)
config.setMaxTotal(10);
// 最大連接空閑數(shù)
config.setMaxIdle(2);
JedisPool jedisPool = new JedisPool(config, host, port);
try{
return jedisPool.getResource();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}注意:如果出現(xiàn)
報錯:Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException:
檢查端口是否開放
解決方法:
- 1.關閉防火墻:service iptables stop
- 2.開放端口:
(1.修改配置文件:vi /etc/sysconfig/iptabls 追加:-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379-j ACCEPT
(2.包存改變:service iptables save
(3.重啟服務:service iptables restart
查看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.
報錯信息很長,但是主要是說redis開啟了protected mode,這也是Redis3.2加入的新特性,開啟保護模式的redis只允許本機登錄,同樣設置在配置文件redis.conf中
這里原來是yes代表開啟了保護模式,后面可以填密碼也可以填no代表關閉,我們這里選擇關閉保護模式,wq保存退出后再重啟redis-server

下面再運行就可以了
Jedis常用方法API
前段時間給大家介紹了如何在Linux環(huán)境下部署和操作redis,今天將為大家介紹如何在我們的Java代碼中操作redis。接下來 按部就班:
一、首先把 jedis-2.1.0.jar(jedis基礎包)
導入到 java項目里
二、創(chuàng)建 jedis對象
![]()
三、鍵操作

四、字符串操作

五、整數(shù)和浮點數(shù)操作

六、列表(List)操作

七、集合(Set)操作

八、哈希(Hash)操作

九、有序集合(Zsort)操作

十、排序操作

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springboot關閉druid監(jiān)控 druid2改配置文件無效的解決
這篇文章主要介紹了springboot關閉druid監(jiān)控 druid2改配置文件無效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
java中SynchronizedList和Vector的區(qū)別詳解
這篇文章主要介紹了java中SynchronizedList和Vector的區(qū)別詳解,Vector是java.util包中的一個類。 SynchronizedList是java.util.Collections中的一個靜態(tài)內部類。,需要的朋友可以參考下2019-06-06
解決spring 處理request.getInputStream()輸入流只能讀取一次問題
這篇文章主要介紹了解決spring 處理request.getInputStream()輸入流只能讀取一次問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java Swing實現(xiàn)窗體添加背景圖片的2種方法詳解
這篇文章主要介紹了Java Swing實現(xiàn)窗體添加背景圖片的2種方法,結合實例形式較為詳細的分析了Swing實現(xiàn)窗體添加背景圖片的方法,并總結分析了Swing重繪中repaint與updateUI的區(qū)別,需要的朋友可以參考下2017-11-11

