11. Java connection Redis_Jedis_Test Unicom

When using Redis when developing projects in Java, there are currently some open source APIs that can be used.
The most commonly used is jedis, which provides many Java-based objects and methods to call Redis instructions.

The jar package download address of jedis is http://download.csdn.net/detail/teamlet/8914387 ( jedis-2.7.2.jar )

Let's build a basic Java project environment containing Jedis to test the relationship between Jedis and Redis connect.

1. Create a project

First open our Eclipse or MyEclipse and create a WebProject named "RedisTest":

Created:

For the convenience of explanation, we do not use Maven and other technologies to manage jars for the time being. We directly put jedis-2.7.2.jar
into the WebRoot/WEB-INF/lib folder, and then add the BuildPath:

2. Create a test class

We create a cn.com.redis.test package, and then create the "TestPing" class under it:

3. Start the redis service

To test the connection, we first need to start redis. I installed a Linux system on Windows using a VMware virtual machine, and
installed Redis in it.

So, we start the Linux system of the VMware virtual machine:

Open the console and start the Redis service:

See how many keys there are currently:

After the Redis service starts, we can connect.

4. Write code to test the connection

When we use JDBC to connect to the Mysql database, we need to know the ip and port number of the database. The same is true for connecting to redis,
we also need to know the ip and port number of the server where redis is located.

Through the ifconfig -a command on linux, we know that the ip of our current host is "192.168.248.129":

The port number where redis is located is "6379" (the default port number for installing redis).

Write the test class:

[java] view plain copy

  1. package cn.com.redis;  
  2.   
  3. import redis.clients.jedis.Jedis;  
  4.   
  5. public class TestPing {  
  6.     public static void main(String[] args) {  
  7.         Jedis jedis = new Jedis("192.168.248.129",6379);  
  8.         //Ping shows PONG  
  9.         System.out.println(jedis.ping());//Go to ping the ip and port of our redis host  
  10. 10.     }  

11. }  


After execution:

It means that we successfully connected to the redis service.

For specific development, please refer to the subsequent summary articles.


Note: If the ping fails, this error may be reported:

First check whether the firewall of linux is closed:

The above is temporarily closed, if you want to close it permanently, use:
command: #chkconfig --level 2345 iptables off
or #chkconfig iptables off
where 2345 represents "execution level"

Note: I am using the Linux version of CentOS 6.5, and the instructions for other versions will be different.


If there is no Unicom, we need to check whether redis has "remote login" enabled.
It turns out that redis can only log in to localhost by default, so it is necessary to enable remote login. Log in. The solution is as follows:  
In the redis configuration file redis.conf, find the bind keyword and match the ip we want to bind
(or just comment out band 127.0.0.1) 

(Note: Band 127.0.0.1 can only be accessed by the local machine, and computers in the LAN cannot be accessed. The  
    bind LAN IP can only be accessed by machines with IP in the LAN, and the local localhost cannot be accessed.)

Another reason is that we have not configured "port forwarding" ". Because the network configuration of the virtual machine is in the NAT mode,
its address is in the same network segment as the local machine, but it can only communicate with the local machine, and other machines
in the local area network cannot be pinged. So how do we configure port forwarding?
(1) Select the virtual network editor under the editing of the virtual machine

(2) To ensure that the nat mode adopted by the Linux system,
check the contents in the red box shown in the following figure in turn, and they must be consistent:

(3) Perform NAT settings
After checking the above configuration, we need to perform NAT settings, and configure a port forwarding here.
Add a port used by the host, the ip address of the redis service in the added virtual machine, and the port number.

After that, the program can access the Redis service in the virtual machine.

 

Please indicate the source for reprint: http://blog.csdn.net/acmman/article/details/53462034

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324683794&siteId=291194637