redisCluster部署整合SSM
更新日期:2020-3-18
chen
一、软件环境
centos6
redis-3.2.8-chen-ok.tar(已经将7000-7008配置文件封装)
二、部署redisCluster
-
上传
redis-3.2.8-chen-ok.tar
,此包已经将集群配置文件修改ok,只需修改IP。其他安装包,需要配置7000-7008文件和配置。
-
解压安装
tar -xzf redis-3.2.8_chen.gz cd redis-3.2.8 #下载后编译,过程稍长 make #进行安装 make install
-
上传集群配置文件
doRedis.tar.gz #自己的封装的,有7000-7008配置文件,并对用到的启动指令进行了封装。
下边步骤可以执行do1,do2,do3分别执行。
#如果没有封装,就手动新建7000和里边的redis.conf文件 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7000 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7001 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7002 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7003 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7004 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7005 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7006 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7007 drwxrwxrwx. 2 root root 4096 Mar 17 05:55 7008
-
打开访问端口7000-7008
/sbin/iptables -I INPUT -p tcp --dport 7000 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7001 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7002 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7003 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7004 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7005 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7006 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7007 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 7008 -j ACCEPT /etc/rc.d/init.d/iptables save #修改生效 /etc/init.d/iptables status #查看配置
-
启动dedis-server
redis-server 7000/redis.conf & redis-server 7001/redis.conf & redis-server 7002/redis.conf & redis-server 7003/redis.conf & redis-server 7004/redis.conf & redis-server 7005/redis.conf & redis-server 7006/redis.conf & redis-server 7007/redis.conf & redis-server 7008/redis.conf &
-
池化
./src/redis-trib.rb create --replicas 1 192.168.8.211:7000 192.168.8.211:7001 192.168.8.211:7002 192.168.8.211:7003 192.168.8.211:7004 192.168.8.211:7005 192.168.8.211:7006 192.168.8.211:7007 192.168.8.211:7008
-
部署完成,简单测试
#登录节点 redis-cli -c -p 7000 info Replication cluster info cluster nodes keys * exists name get name
三、spring SSM整合调用 redis集群
-
redis.properties
##---------------------- # 集群的配置 #redis cluster redis.cluster0.host.port=192.168.8.211:7000 redis.cluster1.host.port=192.168.8.211:7001 redis.cluster2.host.port=192.168.8.211:7002 redis.cluster3.host.port=192.168.8.211:7003 redis.cluster4.host.port=192.168.8.211:7004 redis.cluster5.host.port=192.168.8.211:7005 redis.cluster6.host.port=192.168.8.211:7006 redis.cluster7.host.port=192.168.8.211:7007 redis.cluster8.host.port=192.168.8.211:7008 ##---------------- #最小空闲数 redis.minIdle=100 #最大空闲数 redis.maxIdle=300 redis.maxActive=300 #最大连接数 redis.maxTotal=1000 #客户端超时时间单位是毫秒 redis.timeout=1000 #最大建立连接等待时间 redis.maxWait=1000 #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 redis.testOnBorrow=true
-
applicationContext-rediscluster.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- jedis 配置--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" > <!--最大空闲数--> <property name="maxIdle" value="${redis.maxIdle}" /> <!--最大建立连接等待时间--> <property name="maxWaitMillis" value="${redis.maxWait}" /> <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="minIdle" value="${redis.minIdle}" /> </bean> <bean id="jedisCluster" class="com.chen.common.util.RedisCluster" > <property name="addressConfig"> <value>classpath:redis.properties</value> </property> <property name="addressKeyPrefix" value="redis.cluster" /> <!-- 属性文件里 key的前缀 --> <property name="timeout" value="${redis.timeout}" /> <property name="maxRedirections" value="6" /> <property name="genericObjectPoolConfig" ref="poolConfig" /> </bean> </beans>
-
RedisCluster.class
package com.chen.common.util; import java.util.HashSet; import java.util.Properties; import java.util.Set; import java.util.regex.Pattern; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; public class RedisCluster implements FactoryBean<JedisCluster>, InitializingBean { private Resource addressConfig; private String addressKeyPrefix; private JedisCluster jedisCluster; private Integer timeout; private Integer maxRedirections; private GenericObjectPoolConfig poolConfig; private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$"); public JedisCluster getObject() throws Exception { return jedisCluster; } public Class<? extends JedisCluster> getObjectType() { return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class); } public boolean isSingleton() { return true; } private Set<HostAndPort> parseHostAndPort() { try { Properties prop = new Properties(); prop.load(this.addressConfig.getInputStream()); Set<HostAndPort> haps = new HashSet<HostAndPort>(); for (Object key : prop.keySet()) { if (!((String) key).startsWith(addressKeyPrefix)) { continue; } String val = (String) prop.get(key); boolean isIpPort = p.matcher(val).matches(); if (!isIpPort) { throw new IllegalArgumentException("ip 或 port 不合法"); } String[] ipAndPort = val.split(":"); HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1])); haps.add(hap); } return haps; } catch (IllegalArgumentException ex) { ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } return null; } public void afterPropertiesSet() throws Exception { Set<HostAndPort> haps = this.parseHostAndPort(); jedisCluster = new JedisCluster(haps, timeout, maxRedirections, poolConfig); } public void setAddressConfig(Resource addressConfig) { this.addressConfig = addressConfig; } public void setTimeout(int timeout) { this.timeout = timeout; } public void setMaxRedirections(int maxRedirections) { this.maxRedirections = maxRedirections; } public void setAddressKeyPrefix(String addressKeyPrefix) { this.addressKeyPrefix = addressKeyPrefix; } public void setGenericObjectPoolConfig(GenericObjectPoolConfig poolConfig) { this.poolConfig = poolConfig; } }
-
Service层中,直接调用
@Service public class RabbitItemService { @Autowired private JedisCluster redisService; public void updateItem(Long itemId){ //delete redis data redisService.del("ITEM_"+itemId); } }
-
Service层中,直接调用
@Service public class RabbitItemService { @Autowired private JedisCluster redisService; public void updateItem(Long itemId){ //delete redis data redisService.del("ITEM_"+itemId); } }
end