從JAVA客戶端訪問Redis示例(入門)

轉載地址

本文記錄了安裝Redis和從JAVA端訪問Redis的步驟

從http://download.csdn.net/detail/kkdelta/4034137 下載本文所需文件.

1,在Linux上安裝Redis服務.

下面的操作的base dir爲 /root/4setup

tar xzf redis-2.4.6.tar

cd redis-2.4.6

make

安裝完後啓動

nohup src/redis-server &
下面是從http://tech.it168.com/a2011/0830/1239/000001239923.shtml 拷貝過來的詳細步驟

步驟一: 下載Redis http://download.csdn.net/detail/kkdelta/4034137

步驟二: 編譯源程序

  1. [root@localhost 4setup]# ll  
  2.   
  3. 總計 29168  
  4.   
  5. -rw-r--r--1 root root455240 2011-07-22 redis-2.2.12.tar.gz  
  6.   
  7. [root@localhost 4setup]# tar xzf redis-2.2.12.tar.gz  
  8.   
  9. [root@localhost 4setup]# cd redis-2.2.12  
  10.   
  11. [root@localhost redis-2.2.12]# make  
  12.   
  13. cd src && make all  
  14.   
  15. make[1]: Entering directory `/root/4setup/redis-2.2.12/src'  

步驟三: 啓動Redis服務

  1. src/redis-server  
  2.   
  3. [root@localhost redis-2.2.12]# src/redis-server  
  4.   
  5. [6246] 05 Aug 19:17:22 # Warning: no config file specified, using the default config. In order to specify a config file use'redis-server /path/to/redis.conf'  
  6.   
  7. [6246] 05 Aug 19:17:22* Server started, Redis version2.2.12  
  8.   
  9. [6246] 05 Aug 19:17:22 # WARNING overcommit_memory isset to 0! Background save may fail under low memory condition.To fix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.  
  10.   
  11. [6246] 05 Aug 19:17:22* The serveris now readyto accept connectionson port 6379  
  12.   
  13. [6246] 05 Aug 19:17:22- 0 clients connected (0 slaves),539544 bytes in use  

Redis 服務端的默認連接端口是 6379。

步驟四: 將Redis作爲 Linux 服務隨機啓動

vi /etc/rc.local, 使用vi編輯器打開隨機啓動配置文件,並在其中加入下面一行代碼。

  1. /root/4setup/redis-2.2.12/src/redis-server  

步驟五: 客戶端連接驗證

新打開一個Session輸入:src/redis-cli,如果出現下面提示,那麼您就可以開始Redis之旅了。

  1. [root@localhost redis-2.2.12]# src/redis-cli  
  2. redis 127.0.0.1:6379>  

步驟六: 查看Redis日誌

查看服務器端session,即可對Redis的運行狀況進行查看或分析了。

  1. [6246]05 Aug 19:24:330 clients connected (0 slaves),539544 bytes in use  
  2. [624605 Aug 19:24:37- Accepted 127.0.0.1:51381  
  3. [624605 Aug 19:24:381 clients connected (0 slaves),547372 bytes in use  

以上的幾個步驟就OK了!!這樣一個簡單的Redis數據庫就可以暢通無阻地運行起來了。

步驟七: 停止Redis實例

最簡單的方法是在啓動實例的session中,直接使用Control-C來將實例停止。

我們還可以用客戶端來停止服務,如可以用shutdown來停止Redis實例, 具體如下:

[root@localhost redis-2.2.12]# src/redis-cli shutdown2,

2,開發客戶端JAVA程序:

在Eclipse裏新建一個JAVAproject,把上面的jar包導入.

下面是一個簡單的示例代碼:

  1. public static void main(String[] args) {  
  2.     Jedis jedis = new Jedis("147.151.240.234",6379);  
  3.     jedis.set("foo""bar");  
  4.     String value = jedis.get("foo");  
  5.     System.out.println(value);  
  6. }  
鏈接一個在windows上安裝redis的介紹 http://cardyn.iteye.com/blog/794194
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章