首頁 > Java > java教程 > 主體

Java如何連接Redis

WBOY
發布: 2023-05-19 11:25:05
轉載
2158 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!