從JAVA用戶端訪問Redis樣本(入門)

來源:互聯網
上載者:User

本文記錄了安裝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

步驟二: 編譯來源程式

  [root@localhost 4setup]# ll  總計 29168  -rw-r--r--1 root root455240 2011-07-22 redis-2.2.12.tar.gz  [root@localhost 4setup]# tar xzf redis-2.2.12.tar.gz  [root@localhost 4setup]# cd redis-2.2.12  [root@localhost redis-2.2.12]# make  cd src && make all  make[1]: Entering directory `/root/4setup/redis-2.2.12/src'

步驟三: 啟動Redis服務

  src/redis-server  [root@localhost redis-2.2.12]# src/redis-server  [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'  [6246] 05 Aug 19:17:22* Server started, Redis version2.2.12  [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.  [6246] 05 Aug 19:17:22* The serveris now readyto accept connectionson port 6379  [6246] 05 Aug 19:17:22- 0 clients connected (0 slaves),539544 bytes in use

Redis 服務端的預設串連連接埠是 6379。

步驟四: 將Redis作為 Linux 服務隨機啟動

vi /etc/rc.local, 使用vi編輯器開啟隨機啟動設定檔,並在其中加入下面一行代碼。

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

步驟五: 用戶端串連驗證

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

[root@localhost redis-2.2.12]# src/redis-cliredis 127.0.0.1:6379>

步驟六: 查看Redis日誌

查看伺服器端session,即可對Redis的健全狀態進行查看或分析了。

[6246]05 Aug 19:24:33- 0 clients connected (0 slaves),539544 bytes in use[6246] 05 Aug 19:24:37- Accepted 127.0.0.1:51381[6246] 05 Aug 19:24:38- 1 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包匯入.

下面是一個簡單的範例程式碼:

    public static void main(String[] args) {        Jedis jedis = new Jedis("147.151.240.234",6379);        jedis.set("foo", "bar");        String value = jedis.get("foo");        System.out.println(value);    }

連結一個在windows上安裝redis的介紹 http://cardyn.iteye.com/blog/794194

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.