First, create the redis-cluster folder:
Because redis requires at least 6 nodes (three masters and three slaves), for a better understanding, I have created two virtual machines here (192.168.0.109 192.168.0.110), and created three node folders under /opt/redis-4.0.1/redis-cluster of the two virtual machines respectively.
192.168.0.109:
192.168.0.110:
All the above 6 nodes are created, and then the redis.conf configuration file is created in these six folders, and the configuration is as shown in the figure:
port 7000bind 192.168.0.109daemonize yespidfile /var/run/redis_7000.pidcluster-enabled yescluster-config-file nodes_7000.confcluster-node-timeout 10000appendonly yes
Among them, you need to modify the port pidfile cluster-config-file to the node port number consistent, and bind to the IP for remote access. After all the modifications are completed, you can start the redis service:
Start the command:
Command under 192.168.0.109: "for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done"
Command under 192.168.0.110: "for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done"
You can see that the log printing is successfully started on the background mode, and the machine needs to start all nodes in turn for two days. After the node is started, you can create a cluster service:
Run the following command on one of the virtual machines "/opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.0.109:7000 192.168.0.109:7001 192.168.0.109:7002 192.168.0.110:7003 192.168.0.110:7004 192.168.0.110:7005"
Remember to only execute on one machine. If it is stuck at the join and cannot be executed downward, it is generally caused by the firewall port being banned. There are two ways to solve it:
1. Not only do you need to open the 7000 external port, but you also need to open 17000 (because the redis bus port needs to be added 1000).
2. Turn off all firewalls directly (because I am my own environment here, I turned off the firewall service directly).
The above picture run log appears, and the cluster service is basically successfully built, and you can clearly see the master-slave relationship of each node. After the environment is built, we will use it in conjunction with the SSM architecture I wrote about in the previous article.
The mybaits Level 2 cache that was integrated last time is a stand-alone version. Since this method does not support clusters, we will use jedis-cluster to integrate redis clusters and java.
First, add the configuration of the cluster machine in the redis.properties file, and add the 6 nodes to the configuration in turn:
#cluster cluster1.host.port=192.168.0.109:7000cluster2.host.port=192.168.0.109:7001cluster3.host.port=192.168.0.109:7002cluster4.host.port=192.168.0.110:7003cluster5.host.port=192.168.0.110:7004cluster6.host.port=192.168.0.110:7005
The redis configuration file has also changed a lot from the previous one. I listed it directly and can be copied and used directly.
spring-redis.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- Basic parameter configuration of connection pool, similar to database connection pool--> <context:property-placeholder location="classpath*:redis.properties" /> <bean name="genericObjectPoolConfig" > <property name="maxWaitMillis" value="-1" /> <property name="maxTotal" value="1000" /> <property name="minIdle" value="8" /> <property name="maxIdle" value="100" /> </bean> <!-- Connection pool configuration, similar to database connection pool--> <!-- <bean id="jedisConnectionFactory" > <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <property name="password" value="${redis.pass}"></property> <property name="poolConfig" ref="poolConfig"></property> </bean> --> <!-- Call connection pool factory configuration--> <!-- <bean id="redisTemplate"> <property name="jedisConnectionFactory" ref="jedisConnectionFactory"></property> If Serializer is not configured, then use String intelligently when storing. If you use the User type to store, it will prompt an error User can't cast to String! ! ! <property name="keySerializer"> <bean /> </property> <property name="valueSerializer"> <bean /> </property> </bean> --> <bean id="jedisCluster"> <property name="addressConfig"> <value>classpath:redis.properties</value> </property> <property name="addressKeyPrefix" value="cluster" /> <property name="timeout" value="300000" /> <property name="maxRedirections" value="6" /> <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" /> </bean></beans>Disable all the cache configurations of mybatis in the previous SSM+redis integration to start the service test
First, directly inject jedisCluster to get a cluster object.
For convenience, I wrote a simple idea in Java to synchronize data, and other methods can also be implemented, such as Spring AOP implementation, using third-party plug-ins, or database-level implementation.
After successful startup, the method is called repeatedly. You can see that the console does not print the SQL statement, but directly obtains the data in the redis cluster. The above simple redis cluster instance has been completed. Due to time constraints, I have not written out some pitfalls in Linux in detail.
Summarize
The above is the integrated usage method of Redis cluster and SSM introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!