首頁  >  文章  >  Java  >  Java如何連接Redis

Java如何連接Redis

WBOY
WBOY轉載
2023-05-19 11:25:052318瀏覽

Java連接Redis

Jedis Client是Redis官網推薦的一個面向java客戶端,程式庫檔案實作了對redis各類別API進行封裝呼叫.

引入jar套件

我創建的是maven項目,所以只用在pom檔案中加入

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>3.0.0</version>
		</dependency>

如果不是maven項目,你要確定引入相關依賴


Java如何連接Redis

編寫測試類別

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();
		// 最大连接数
		config.setMaxTotal(10);
		// 最大连接空闲数
		config.setMaxIdle(2);
		JedisPool jedisPool = new JedisPool(config, host, port);
		try{
			
			return jedisPool.getResource();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}

注意:如果出現

錯誤: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的設定檔

Java如何連接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 ptpt onetec the follosect from external computers to Redis you may ptpt onetec the follo formal formal 的版本。 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.D. 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 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 in order for the server to start accepting connections from the side.

錯誤訊息很長,但是主要是說redis開啟了protected mode,這也是Redis3.2加入的新特性,開啟保護模式的redis只允許本機登錄,同樣設定在設定檔redis.conf中

這裡原來是yes代表開啟了保護模式,後面可以填密碼也可以填no代表關閉,我們這裡選擇關閉保護模式,wq保存退出後再重啟redis-server

Java如何連接Redis

#下面再運行就可以了

Jedis常用方法API

前段時間介紹如何在Linux環境下部署和操作redis,今天將為大家介紹如何在我們的Java程式碼中操作redis。接下來按部就班:

一、先把jedis-2.1.0.jar(jedis基礎套件)

匯入到java專案裡

2、建立jedis物件

Java如何連接Redis

三、鍵操作

Java如何連接Redis

Java如何連接Redis

Java如何連接Redis

Java如何連接Redis

Java如何連接Redis

##五、整數與浮點數運算

Java如何連接Redis

六、列表(List)運算

Java如何連接Redis

Java如何連接Redis

#######T Set)操作###############八、哈希(Hash)運算################9、有序集合(Zsort)操作###############第十、排序運算#############

以上是Java如何連接Redis的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除