1. 程式人生 > >使用Jedis操作Redis資料庫

使用Jedis操作Redis資料庫

1、Jedis客戶端出現的異常資訊

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.

2、對於異常資訊的解決方法

       2.1 在redis伺服器,設定訪問密碼

[[email protected] redis-3.2.1]# redis-cli  
127.0.0.1:6379> config set requirepass 123456  
OK  
127.0.0.1:6379>  

       2.2 在Jedis客戶端,加入訪問密碼

// 設定連線密碼
    jedis.auth("123456");

2.3 使用密碼登入redis客戶端

$ ./redis-cli -h 127.0.0.1 -p 6379 -a 123456
3、Jedis連線Redis的簡單操作
import redis.clients.jedis.Jedis;
public class JedisDemo {

	public static void main(String[] args) {
		// 主機地址
		String host = "192.168.2.111";
		// 構造jedis物件
		Jedis jedis = new Jedis(host, 6379);
		// 設定連線密碼
		jedis.auth("123456");
		// 向redis中新增資料
		jedis.set("mytest", "123");
		// 從redis中讀取資料
		String value = jedis.get("mytest");

		System.out.println(value);
		// 關閉連線
		jedis.close();
	}
}

4、JedisPool的配置和使用

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolDemo {

	public static void main(String[] args) {
		// 主機地址
		String host = "192.168.2.105";
		// 構建連線池配置資訊
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		// 設定最大連線數
		jedisPoolConfig.setMaxTotal(50);
		// 超時時間
		int timeout = 10000;
		// 授權密碼
		String password = "123456";
		// 構建連線池
		JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, 6379,
				timeout, password);
		// 從連線池中獲取連線
		Jedis jedis = jedisPool.getResource();
		// 設定訪問密碼
		// 讀取資料
		System.out.println(jedis.get("mytest"));

		// 將連線還回到連線池中
		jedisPool.returnResource(jedis);

		// 釋放連線池
		jedisPool.close();

	}
}

5、ShardedJedisPool的配置和使用

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * 叢集式的連線池
 * 
 */
public class ShardedJedisPoolDemo {

	public static void main(String[] args) {
		// 構建連線池配置資訊
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		// 設定最大連線數
		poolConfig.setMaxTotal(50);
		// 定義叢集資訊
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();

		// 定義A節點資訊
		JedisShardInfo infoA = new JedisShardInfo("192.168.2.111", 6379);
		// 設定A節點的訪問密碼
		infoA.setPassword("123456");
		// 定義B節點資訊
		JedisShardInfo infoB = new JedisShardInfo("192.168.2.116", 6379);
		// 設定B節點的訪問密碼
		infoB.setPassword("123456");

		// 加入叢集節點
		shards.add(infoA);
		shards.add(infoB);
		// 定義叢集連線池
		ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig,
				shards);
		ShardedJedis shardedJedis = null;
		try {
			// 從連線池中獲取到jedis分片物件
			shardedJedis = shardedJedisPool.getResource();
			for (int i = 0; i < 100; i++) {
				shardedJedis.set("key" + i, "value" + i);
			}

			// 從redis中獲取資料
			String value = shardedJedis.get("mytest");
			System.out.println(value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != shardedJedis) {
				// 關閉,檢測連線是否有效,有效則放回到連線池中,無效則重置狀態
				shardedJedis.close();
			}
		}

		// 關閉連線池
		shardedJedisPool.close();

	}
}

6、原始碼下載

相關推薦

使用Jedis操作Redis資料庫

1、Jedis客戶端出現的異常資訊 Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException:

jedis工具類:java操作redis資料庫

學完redis,需要在java客戶端中使用Jedis,作為連線redis的工具: JedisUtils工具類: public class JedisUtils{ //定義一個連線池物件: private final static JedisPool POOL; static {

Java中使用Jedis操作Redis

移除 keys pre java max value con print 處的 轉載:http://www.cnblogs.com/liuling/p/2014-4-19-04.html 使用Java操作Redis需要jedis-2.1.0.jar,下載地址:http://

使用jedisPool管理jedis,使用jedis操作redis

syn als love setup com 變參 可變參 != row ps:jedis是redis在java中的客戶端操作工具 package com.test; 2 3 import java.util.HashMap; 4 import jav

使用Jedis操作redis

www. 操作 app tp5 com asn dpx www userinfo 7詼6WEU1AQGWhttp://jz.docin.com/yok0967 S1及粵列5TJZ趕http://www.docin.com/rpfua246 493U怪o俁97DTJ1ht

Redis學習-5 Jedis操作Redis

lunch sta bsp redis style org 線程池 java應用 獲取 1.Jedis Jedis在java應用中操作Redis; 2.Jedis訪問Redis 下載Jedis http://search.maven.org和Commons-Poolx [關

【JAVA】使用 jedis操作redis——連線、儲存資料、切庫等

本篇運用Java呼叫jedis包(jedis線上文件API ),做簡單操作例項。 安裝jedis 1. 2.9.0 jar 版本下載: jedis-2.9.0.jar 2. 新建專案,新增該驅動包    連線到 redis 服務

使用Jedis操作Redis-使用Java語言在客戶端操作---set型別

原文地址:http://www.cnblogs.com/lixianyuan-org/p/9509696.html 1 //測試set資料型別 2 /** 3 * 在Redis中,我們可以將Set型別看作為沒有排序的字元集合,和List型別一樣,我們也可以在該型別

使用RedisTemplate操作Redis資料庫

目錄 一.什麼是Redis 二.RedisTemplate及其相關方法 三.RedisTemplate操作Redis資料庫的具體例子 四.RedisTemplate和StringRedisTemplate的區別 一.什麼是Redis Redis是一個非關係型資料庫,具有很高的

redis常見資料型別操作命令,Java中使用Jedis操作Redis

redis常見資料型別操作命令 可參考地址:Http://redisdoc.com/ Java中使用Jedis操作Redis: https://www.cnblogs.com/liuling/p/2014-4-19-04.html redis鍵(key)

7.jedis操作redis

jedis操作redis Author:SimpleWu Jedis介紹 Jedis是Redis官方推薦的的Java客戶端開發包。 官方:https://github.com/xetorthio/jedis 使用 匯入依賴: <dependencies> <!--

操作redis資料庫的常用命令

1.啟動和訪問 啟動redis資料庫 sudo redis-server /etc/redis/redis.conf 終端訪問redis資料庫 sudo redis-cli -h 127.0.0.1 -p 6379 2.String (字串型別) se

PHP操作Redis資料庫常用方法

redis Redis支援的資料型別有 Stirng(字串), List(列表), Hash(字典), Set(集合), Sorted Set(有序集合); redis版本是Redis 2.6.12 系統是在Windows+Apache2.4+php5.6 連線: //例項化

Java——Jedis操作Redis的方法總彙!

在開發中少不了Redis的應用,做快取、高併發、購物車等等都需要用到redis.... 再開發過程中我們都會使用Jedis來操作Redis,下面整理了一些Jedis操作Redis的方法,在需要的時候用來做參考~ ​ package com.wujintao.redis; i

Jedis操作Redis--SortedSet型別

/**  * SortedSet(有序集合)  * ZADD,ZCARD,ZCOUNT,ZINCRBY,ZRANGE,ZRANGEBYSCORE,ZRANK,ZREM,ZREMRANGEBYRANK,ZREMRANGEBYSCORE,ZREVRANGE  * ZREVR

Jedis操作Redis--Hash型別

/**  * Hash(雜湊表)  * HDEL,HEXISTS,HGET,HGETALL,HINCRBY,HINCRBYFLOAT,HKEYS,HLEN,HMGET,HMSET, HSET,HSETNX,HVALS,HSCAN,HSTRLEN  */ public c

Jedis操作Redis--List型別

/**  * List(列表)  * BLPOP,BRPOP,BRPOPLPUSH,LINDEX,LINSERT,LLEN,LPOP,LPUSH,LPUSHX,LRANGE,LREM,LSET,LTRIM, RPOP,RPOPLPUSH,RPUSH,RPUSHX  */

Jedis操作Redis--Set型別

/**  * Set(集合)  * SADD,SCARD,SDIFF,SDIFFSTORE,SINTER,SINTERSTORE,SISMEMBER,SMEMBERS,SMOVE,SPOP,SRANDMEMBER,SREM,SUNION,SUNIONSTORE,SSCAN &nb

java通過jedis操作redis(從JedisPool到JedisCluster)

redis作為一個快取資料庫,在絕大多數java專案開發中是必須使用的,在web專案中,直接配合spring-redis,各種配置都直接在spring配置檔案中做了,一般都是使用redis連線池。在非web專案中,通常也是使用的redis連線池。 根據redis的機器數量和叢集方式,又分為以

jedis操作redis事務與管道

Jedis對管道、事務以及Watch的操作詳細解析 1、Pipeline 利用pipeline的方式從client打包多條命令一起發出,不需要等待單條命令的響應返回,而redis服務端會處理完多條命令後會將多條命令的處理結果打包到一起返回給客戶端。所以p