Example of accessing redis from a Java client (Getting Started)

Source: Internet
Author: User
Tags install redis

This document describes how to install redis and access redis from Java.

Download the files required for this article from http://download.csdn.net/detail/kkdelta/4034137.

1. Install the redis service on Linux.

The base Dir of the following operation is/root/4 setup

Tar xzf redis-2.4.6.tar

CD redis-2.4.6

Make

Start after installation

Nohup src/redis-Server &
Below are detailed steps to copy from the http://tech.it168.com/a2011/0830/1239/000001239923.shtml

Step 1: Download The redis http://download.csdn.net/detail/kkdelta/4034137

Step 2: Compile the source program

[Root @ localhost 4 setup] # ll total 29168-RW-r -- 1 root root455240 2011-07-22 redis-2.2.12.tar.gz [root @ localhost 4 setup] # tar xzf redis-2.2.12.tar.gz [root @ localhost 4 setup] # cd redis-2.2.12 [root @ localhost redis-2.2.12] # Make cd src & make all make [1]: entering directory '/root/4 Setup/redis-2.2.12/src'

Step 3: Start the redis Service

  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

The default connection port of the redis server is 6379.

Step 4: randomly start redis as a Linux Service

VI/etc/rc. Local, use the VI editor to open the random STARTUP configuration file, and add the following line of code.

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

Step 5: Verify the Client Connection

Open a new session and enter src/redis-cli. If the following prompt appears, you can start your redis journey.

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

Step 6: View redis logs

View the server session to view or analyze the redis running status.

[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

The above steps are OK !! Such a simple redis database can run smoothly.

Step 7: Stop a redis instance

The simplest way is to stop an instance directly using control-C in the session of the instance.

We can also use a client to stop the service. For example, we can use shutdown to stop redis instances, as shown below:

[Root @ localhost redis-2.2.12] # src/redis-cli
Shutdown2,

2. Develop the client Java program:

Create a javaproject in eclipse and import the jar package above.

The following is a simple sample code:

    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);    }

Link to an introduction http://cardyn.iteye.com/blog/794194 for installing redis on Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.