Implementation of Redis cluster and Java

Source: Internet
Author: User
Tags redis cluster

Starting with Redis's first Redis installation

1. Create the Redis-cluster directory in the redis.conf directory mkdir redis-cluster

2. Enter redis-cluster and create several clusters 6001-6006

mkdir 6001 mkdir 6001 mkdir 6003 mkdir 6004 mkdir 6005 mkdir 6006

3. Then create the data package

Mkdir-p 6001/data 6002/data 6003/data 6004/data 6005/data 6006/data

4. Copy the redis.conf and go to 6001-6006.

CP redis.conf./redis-cluster/6001. CP redis.conf./redis-cluster/6006

5. Modify each redis.conf to change the 6001 to the appropriate port

Port 6001 (port number per node) daemonize Yesbind 192.168.119.131 (bind current machine IP) dir/usr/local/redis-cluster/6001/data/ (data file storage location) Pidfile/var/run/redis_6001.pid (PID 6001 and port to correspond) cluster-enabled Yes (boot cluster mode) Cluster-config-file Nodes6001.conf (6001 and port to correspond) Cluster-node-timeout 15000appendonly Yes

Change all:%s/6001/6002/g

6. Check if startup is successful Ps-el | grep Redis

7. Because the Redis cluster needs to use Ruby commands, we need to install Ruby and related interfaces

8.192.168.31.245 is the virtual machine address 7000 refers to the port number

REDIS-TRIB.RB  Create  --replicas  1  192.168.31.245:7000 192.168.31.245:7001  192.168.31.245:7002 192.168.31.210:7003  192.168.31.210:7004  192.168.31.210:7005

And then start the cluster, even if it's done, be aware that Redis has to be used after 3.0.0 versions.

Java Connect Redis Cluster

public static void Main (string[] args) {jedispoolconfig poolconfig = new Jedispoolconfig ();    Maximum number of connections poolconfig.setmaxtotal (1);    Maximum idle number poolconfig.setmaxidle (1); The maximum allowable wait time, if the connection is not acquired over this time, jedisexception exception is reported://Could not get a resource from the pool Poolconfig.setmaxwaitmill    is (1000);    set

Spring Configuring a Redis cluster

<context:property-placeholder ignore-unresolvable= "true" location= "classpath:yonyou.properties"/> <bean i          D= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "maxtotal" value= "$"/> <property name= "Maxidle" value= "ten"/> <property name= "Minidle" value= "1"/> <propert Y name= "Maxwaitmillis" value= "30000"/> <property name= "Testonborrow" value= "true"/> <property Name= "Testonreturn" value= "true"/> <property name= "Testwhileidle" value= "true"/> <!--<prope Rty name= "Testwhileidle" value= "true"/>-</bean> <bean id= "Shardedjedispool" class= "redis.c         Lients.jedis.ShardedJedisPool "destroy-method=" destroy "> <constructor-arg ref=" Jedispoolconfig "/> <constructor-arg> <!--If you need to expand your cluster later, you only need to copy a Redis, modify the ports, and configure it here--<list> <b Ean class= "redis.clients.Jedis. Jedisshardinfo "> <constructor-arg index=" 0 "value=" 127.0.0.1 "/> <constructor-ar G index= "1" value= "6379"/> <constructor-arg index= "2" value= "instance:01"/> &LT;/BEAN&G            T <bean class= "Redis.clients.jedis.JedisShardInfo" > <constructor-arg index= "0" value= "127.0.0.1"/&gt                ;            <constructor-arg index= "1" value= "6380"/> <constructor-arg index= "2" value= "instance:02"/> </bean> <bean class= "Redis.clients.jedis.JedisShardInfo" > <constructor-ar G index= "0" value= "127.0.0.1"/> <constructor-arg index= "1" value= "6381"/> <const Ructor-arg index= "2" value= "instance:03"/> </bean> </list> </constructor-ar G> </bean> <!--Java helps us synchronize Sentinel information, synchronizing the master-slave information to the client--<bean class= "redis.clients. Jedis.             Jedissentinelpool "> <constructor-arg index=" 0 "value=" MyMaster "/> <constructor-arg index=" 1 "> <set> <value>127.0.0.1:26379</value> </set> </cons Tructor-arg> <constructor-arg index= "2" ref= "Jedispoolconfig"/> </bean>

  

Implementation of Redis cluster and Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.