Redis Series
Author Mr.chen, reprint please indicate the source of the blog: http://www.cnblogs.com/cjh-notes/
First step: Download and install Redis
Windows version of:https://github.com/MSOpenTech/redis/releases
File Directory Description:
Service side: Redis-server.exe
Client: Redis-cli.exe
Configuration file: redis.windows.conf
Cluster Tool: REDIS-TRIB.RB (the installation file for Windows does not have this and needs to be copied from the Linux version)
Step Two: Install Ruby
Download and install Railsinstaller, a packaged one-stop installation package that contains components such as Ruby, Ralis, etc.
Official address: http://railsinstaller.org/en
Baidu Network disk: Https://pan.baidu.com/s/1eUje2L8
Execute command gem install redis in CMD window after installation is complete,
Step Three: Configure Redis nodes
The way to configure the cluster nodes is basically the same as in the previous section of the Linux environment, which is not described here.
Fourth step: Create a cluster
Enter the directory where the redis-trib.rb file is located and execute the following command, where the example is 3 Master 0 from:
127.0. 0.1:7000127.0. 0.1:7001127.0. 0.1:7002
Last step: Calling Redis in Java
注:在非集群和集群环境中调用的方式有所不同,非集群中使用是Jedis对象,而集群中使用的是JedisCluster对象。
Attached code:
PackageCom.cjh;ImportCom.sun.org.apache.regexp.internal.RE;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;ImportRedis.clients.jedis.HostAndPort;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisCluster;ImportJava.util.HashMap;ImportJava.util.HashSet;ImportJava.util.Set;/*** Created by 20628_000 on 2018/1/24.*/ Public classRedistest { Public Static voidMain (String arges[]) {Newredistest (). Init (); } Private Static FinalLogger Logger = Logmanager.getlogger (redistest.class); Private StaticJediscluster JC =NULL; Public voidinit () {//Connect to a local Redis service /*Jedis = new Jedis ("127.0.0.1", 7002); SYSTEM.OUT.PRINTLN ("Connection succeeded"); SYSTEM.OUT.PRINTLN ("Service is running:" +jedis.ping ());*/Set<HostAndPort> jedisclusternodes =NewHashset();//Jedis Cluster would attempt to discover Cluster nodes automaticallyJedisclusternodes.add (NewHostandport ("127.0.0.1", 7000)); Jedisclusternodes.add (NewHostandport ("127.0.0.1", 7001)); Jedisclusternodes.add (NewHostandport ("127.0.0.1", 7002)); JC=NewJediscluster (jedisclusternodes); Test (); } Public voidTest () {HASHMAP map=NewHashMap (); Map.put ("Name", "CJH"); Map.put ("Age", "25"); Map.put ("Sex", "male"); Jc.hmset ("MyInfo", map); Logger.info (Jc.hget ("MyInfo", "Age")); }}
Run the program to view printing information:
2018-02-19 17:13:42 INFO com.cjh.redistest–25
Test Pass ~
Redis cluster Distribution (Windows edition)