1 RedisClient Applications (Jedis),JedisShards1.1 What isJedis?
Jedis is a client-side technology for Java to connect to a Redis server, which is equivalent to connecting a database with JDBC . ( JDBC is required to connect to MySQL, and Jedis is required to connect with Redis)
1.2 JedisClient ConnectionsRedisCaching Steps1.2.1 The first way to create a project:
a) Download Jedis jar Package: The official website Www.redis.io is based on maven's simplest configuration to obtain a redis dependent jar package.
The official website provides the following methods:
1) Configure maven configuration in Pom.xml: Find dependent jar packages based on configuration
<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> |
2) use Java code to manipulate Redis server
Jedis Jedis = new Jedis ("192.168.13.129", 6379); Jedis.set ("String_key01", "Today is a good day"); String s = jedis.get ("String_key01"); System.out.print (s); Jedis.close (); |
II)Jedis Connection Pool application
1) Create Jedis Connection Pool Configuration object:jedispoolconfigconfig = new Jedispoolconfig ();
2) Set configuration parameters:
Config.setmaxidle (1000*60); Config.setmaxtotal (8); Config.settestonborrow (ture); |
3) Create a connection pool object from the Configuration object:
Jedispool pool = new Jedispool ( config, "192.168.13.129", 6379 ) ; |
4) Get the Jedis object through the pool object and manipulate the Redis
Jedis JDS = Pool.getresource (); Jds.set ("name", "Zhangsan"); String name = Jds.get ("name"); System.out.print (name); |
1.2.2 The second way to create a project:
1) Import the dependent jar Package
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BF/wKiom1WEINuAHOYQAAB9iL7GGrM796.jpg "title=" 36 ' 1l7d5f) e0xig1l~{k{oq.png "alt=" Wkiom1weinuahoyqaab9il7ggrm796.jpg "/>
2) use Java code to operate Redis Server (IBID.)
Jedis Jedis = new Jedis ("192.168.13.129", 6379); Jedis.set ("String_key01", "Today is a good day"); String s = jedis.get ("String_key01"); System.out.print (s); Jedis.close (); |
1.3 JedisShards1.3.1 What is a shard:
when a redis server is unable to load high-concurrency pressures, we need multiple servers to share the appropriate pressure.
Figure 1.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/BB/wKioL1WEIrPiJbt9AAEOZThxt4k313.jpg "title=" 11.png "alt=" Wkiol1weirpijbt9aaeozthxt4k313.jpg "/>
Figure 2.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/BF/wKiom1WEITizA28vAAF7daugThk225.jpg "title=" 22.png "alt=" Wkiom1weitiza28vaaf7daugthk225.jpg "/>
1.3.2 JedisShard Operations
The first step: Configure more than one Redis Server: Because it is inconvenient to configure multiple Redis servers, use the copy configuration file, modify the port to template the second redis server;
There's already one , copy redis.conf Modify the port number 6380 as the second server
Step Two: Create Shards Jedis Connection Pool object:shardedjedispool
1) It requires three parameters:1. configuration parameters, 2. multiple Redis Server information parameters,3. algorithm parameters.
jedispoolconfig poolconfig = newjedispoolconfig(); Poolconfig.setmaxtotal (500); Poolconfig.setmaxidle (1000 * 60); Poolconfig.settestonborrow (true); list<jedisshardinfo> shards = new arraylist<jedisshardinfo> (2); Jedisshardinfo A = new jedisshardinfo ("192.168.13.111", 6379); Jedisshardinfo B = new jedisshardinfo ("192.168.13.111", 6380); Shards.add (A); Shards.add (B); ShardedjedispoolPool = new shardedjedispool (Poolconfig, Shards, Hashing. Murmur_hash); |
2) Get the Jedis object and simulate the request to test how the resource is allocated.
Private Static int Index = 1; &NBSP; public static void main (string[] args) { for(int i=0; i<100; i++) { String key =string. valueOf (Index+ +); Shardedjedis JDS = NULL ; Try { JDS = Pool . getresource (); System. out . println (key+":"+jds.getshard (key). Getclient (). Getport ()); system. out .println (Jds.set (Key, " 1111111111111111111111111111111 " )) ; }catch (Exception e) { E.printstacktrace (); } finally{ Pool. Returnresource (JDS); } } } |
3) Summary: No need to care about which server the data is saved to, get the data without specifying the server, all by Hashing. Murmur_hash to be responsible.
1.4 JedisClient cannot connectRedisServer
l Check that the Redis server IP address and port number are correct;
l Check whether the Redis server is setting the client access password:jedis.auth ("123456");
l Check If the Inux firewall is off
l Linux Shutdown Firewall command step:
Permanent, no recovery after reboot
¨ Open:chkconfig iptables on
¨ off:chkconfig iptables off
Immediate effect, recovery after reboot
¨ Open:service iptables start
¨ Close:service iptables stop
Redis 2.8 Client Application (Jedis), Jedis Shard