The previous article explains how to install and start the redis service and test it with the redis client. This article uses the Jedis method to use the services provided by redis.
First, Telnet the redis service port first. At this moment, problems may occur. If the Linux firewall of the redis service fails to enable telnet
The procedure is as follows:
Run the following command:
[root@localhost redis-2.4.14]# /etc/init.d/iptables status
If the following information appears:
Table: filterchain input (Policy accept) num target prot opt source destination 1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0 chain forward (Policy accept) num target prot opt source destination 1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0 chain output (Policy accept) num target prot opt source destination chain RH-Firewall-1-INPUT (2 references) num target prot opt source destination 1 accept all -- 0.0.0.0/0 0.0.0.0/0 2 accept ICMP -- 0.0.0.0/0 0.0.0.0/0 ICMP Type 255 3 accept Esp -- 0.0.0.0/0 0.0.0.0/0 4 accept ah -- 0.0.0.0/0 0.0.0.0/0 5 accept UDP -- 0.0.0.0/0 224.0.0.20.udp DPT: 5353 6 accept UDP -- 0.0.0.0/0 0.0.0.0/0 udp dpt: 631 7 accept TCP -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 631 8 accept all -- 0.0.0.0/0 0.0.0.0/0 state related, established 9 accept TCP -- 0.0.0.0/0 0.0.0.0/0 state New tcp dpt: 22 10 reject all -- 0.0.0.0/0 0.0.0.0/0 reject-with ICMP-host-prohibited 11 accept TCP -- 0.0.0.0/0 0.0.0.0/0 state New tcp dpt: 6379
Indicates that the firewall is on.
Solution:
[Root @ localhost redis-2.4.14] #/etc/rc. d/init. d/iptables stop clear firewall rules: [OK] Set chains to accept policy: Filter [OK] uninstalling iiptables module: [OK] [root @ localhost redis-2.4.14] #
Then Telnet.
Note: This method is only used in testing. If this method is used in the production process, the consequences will be borne by you.
Then use Jedis for testing.
/*** Redistest. java * copyright (c) 2012 * created: cuiran 14:31:48 */package COM. wpndemo. redis; import redis. clients. jedis. jedis; import Java. util. iterator; import Java. util. map;/*** todo * @ author cuiran * @ version todo */public class redistest {public static void main (string ARGs []) {// define and instantiate a Jedis object Jedis = new Jedis ("192.168.0.138"); // Jedis. hset ("Weibo", "nickname", "admin"); // set the nickname // Jedis. hset ("Weibo", "password", "admin"); // set the password // Jedis. hset ("Weibo", "fans", "200"); // set the number of fans // Jedis. hset ("Weibo", "Sweets", "555"); // set the number of Weibo posts // return a map object // map weibomap = Jedis. hgetall ("Weibo"); // iterator weiboiterator = weibomap. entryset ()//. iterator (); ///// view the output result in iteration // while (weiboiterator. hasnext () {// map. entry weiboentry = (map. entry) weiboiterator. next (); // system. out. println ("Key =" + weiboentry. getkey (); // system. out. println ("value =" + weiboentry. getvalue (); // Jedis. lpush ("cuiran1", "1"); // Jedis. lpush ("cuiran1", "2"); // Jedis. lpush ("cuiran1", "3"); // Jedis. lpush ("cuiran1", "4"); // string temp = Jedis. rpop ("cuiran1"); // Jedis. del ("cuiran1"); // system. out. println (temp);/** store **/Jedis. setex ("0001", 1800, "Cui ran");/*** retrieve */string temp = Jedis. get ("0001"); system. out. println (temp); // system. out. println (Jedis. exists ("cuiran"); system. out. println ("cuiran1:" + Jedis. lrange ("cuiran1", 0,-1 ). size ());}}
So far, the use of redis and Jedis has ended. If you have any questions, please leave a message. Thank you.