The vmware 11 used in this article is the system installed in centos 6.7 and the redis version is 3..0.2. For how to install, please refer to the previous article "Installing Redis and setting up related services under Linux" .
After installing Redis, do we have to wait to use it? So if we want to operate redis in the program, there are several plug-ins for operating redis, and jedis is used here.
After we create a new java program, add Jedis.jar, and add junit class library. Create a java class for testing. The project structure is shown in the figure.
The code of the Java test class is as follows.
package com.tgb.redis; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; public class RedisClient { private Jedis jedis; @Before public void setup() { //Connect the redis server, the ip address of the virtual machine 192.168.20.128:6379 jedis = new Jedis("192.168.20.128",5000); //Permission authentication// jedis.auth("root"); } /** * redis storage string*/ @Test public void testString() { //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //Set multiple key-value pairs jedis.mset("name","liuling","age","23","qq","476777XXX"); jedis.incr("age"); //Please add 1 System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq")); } }After configuration, we test it. The following error was found. It can be easily concluded that because the network is not connected, this kind of link timeout occurs.
According to common sense, we will check whether the network is the same, so we will ping the address of the virtual machine and we find that it can be connected. Is that a firewall problem? After closing the firewall, I found that it is still not possible. Then we asked Baidu and found no similar answer. So go to the group to ask Daniu, one of them said if it is necessary to configure a port forwarding.
Yes, this thing needs to be configured. Because the virtual machine's network configuration is in the same network segment as the local machine, 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! We want to select the Virtual Network Editor under Editing of the Virtual Machine.
We must ensure that our Linux system adopts the nat mode and check the contents in the red box shown in the figure below in turn. Be sure to be consistent.
After the above configuration check is completed, we need to perform NAT settings and configure a port forwarding here. Add a port used by the host, add the IP address of the Redis service in the virtual machine, and the port number.
After the configuration is complete, click Apply and the network card of the Linux system will restart. In this way, we are testing in the program and found that the console outputs the following information.
This shows that our program can access services in our virtual machine. Isn’t it very simple? I hope it will inspire everyone’s learning.